-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_start.go
More file actions
99 lines (83 loc) · 1.83 KB
/
Copy pathcmd_start.go
File metadata and controls
99 lines (83 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"fmt"
"os"
"os/exec"
"os/signal"
"syscall"
"github.com/spf13/cobra"
)
var startOutput string
var startCmd = &cobra.Command{
Use: "start",
Short: "Build and start the application binary",
Long: bold(
"start",
) + ` compiles the application and runs the resulting binary.
` + colorGray + `Examples:` + colorReset + `
grove start
grove start -o ./bin/my-api`,
RunE: runStart,
}
func init() {
startCmd.Flags().StringVarP(
&startOutput,
"output", "o", "./bin/app",
"Output path for the compiled binary",
)
}
func runStart(_ *cobra.Command, _ []string) error {
fmt.Println()
fmt.Printf(
" %s %s\n",
badge(colorBgBlue, "BUILDING"),
gray("go build -o "+startOutput+" ./cmd/api/"),
)
fmt.Println()
elapsed, err := buildBinary(startOutput)
if err != nil {
fmt.Println()
fmt.Printf(" %s\n", badge(colorBgRed, "BUILD FAILED"))
fmt.Println()
return fmt.Errorf("")
}
fmt.Println()
fmt.Println(done(
"Binary compiled to " + colorCyan + startOutput + colorReset +
" " + gray("("+fmtDuration(elapsed)+")"),
))
fmt.Println()
fmt.Printf(
" %s %s\n",
badge(colorBgBlue, "STARTING"),
gray(startOutput),
)
fmt.Println()
c := exec.Command(startOutput)
c.Stdout = os.Stdout
c.Stderr = os.Stderr
c.Stdin = os.Stdin
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
if err := c.Start(); err != nil {
return fmt.Errorf("failed to start binary: %w", err)
}
go func() {
sig := <-sigCh
if c.Process != nil {
_ = c.Process.Signal(sig)
}
}()
if err := c.Wait(); err != nil {
if c.ProcessState != nil && !c.ProcessState.Success() {
if isSignalError(err) {
fmt.Println()
fmt.Println(gray(" Server stopped."))
fmt.Println()
return nil
}
}
return fmt.Errorf("binary exited with error: %w", err)
}
return nil
}