-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_dev_air.go
More file actions
106 lines (90 loc) · 2.43 KB
/
Copy pathcmd_dev_air.go
File metadata and controls
106 lines (90 loc) · 2.43 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
100
101
102
103
104
105
106
package main
import (
"fmt"
"os"
"os/exec"
"os/signal"
"syscall"
"github.com/spf13/cobra"
)
var devAirCmd = &cobra.Command{
Use: "dev:air",
Short: "Start the development server using Air for hot-reload",
Long: bold(
"dev:air",
) + ` starts the development HTTP server using ` + colorCyan + `air` + colorReset + ` for hot-reload.
If ` + colorCyan + `air` + colorReset + ` is not installed it falls back to ` + colorCyan + `go run ./cmd/api/main.go` + colorReset + `.
For a zero-dependency hot-reload experience use ` + colorGreen + `grove dev` + colorReset + ` instead —
it has a built-in watcher that requires no external tools.
` + colorGray + `Examples:` + colorReset + `
grove dev:air`,
RunE: runDevAir,
}
func runDevAir(_ *cobra.Command, _ []string) error {
fmt.Println()
var c *exec.Cmd
if _, err := exec.LookPath("air"); err == nil {
fmt.Printf(
" %s AIR %s Starting server with %s (hot-reload enabled)\n",
colorBgGreen, colorReset,
bold("air"),
)
fmt.Println()
c = exec.Command("air")
} else {
fmt.Printf(
" %s INFO %s %s not found — starting with %s\n",
colorBgBlue, colorReset,
colorCyan+"air"+colorReset,
bold("go run ./cmd/api/main.go"),
)
fmt.Printf(
" %sTip: install air for hot-reload → %s\n",
colorGray,
colorCyan+"go install github.com/air-verse/air@latest"+colorReset,
)
fmt.Printf(
" %sTip: use %s for built-in hot-reload with no external tools\n",
colorGray,
colorGreen+"grove dev"+colorReset,
)
fmt.Println()
c = exec.Command("go", "run", "./cmd/api/main.go")
}
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 server: %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("server exited with error: %w", err)
}
return nil
}
// isSignalError reports whether err was caused by a signal (e.g. SIGINT).
func isSignalError(err error) bool {
if err == nil {
return false
}
msg := err.Error()
return msg == "signal: interrupt" ||
msg == "signal: terminated" ||
msg == "signal: killed"
}