-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
172 lines (154 loc) · 3.41 KB
/
main.go
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"bufio"
"context"
"flag"
"fmt"
"log"
"os"
"os/exec"
"os/signal"
"strconv"
"strings"
"sync"
"syscall"
"time"
"github.com/fatih/color"
"github.com/fsnotify/fsnotify"
)
type RunnerOpts struct {
path string
args []string
}
func runner(ctx context.Context, wg *sync.WaitGroup, runnerOpts RunnerOpts) {
defer wg.Done()
runner_killer()
cmd := exec.Command("go", append([]string{"run"}, runnerOpts.args...)...)
cmd.Dir = runnerOpts.path
stdout, err := cmd.StdoutPipe()
if err != nil {
panic(err)
}
stderr, err := cmd.StderrPipe()
if err != nil {
panic(err)
}
if err := cmd.Start(); err != nil {
panic(err)
}
go func() {
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
os.Stdout.WriteString(color.GreenString("STDOUT: %s", scanner.Text()+"\n"))
}
}()
go func() {
scanner := bufio.NewScanner(stderr)
for scanner.Scan() {
os.Stderr.WriteString(color.RedString("STDERR: %s", scanner.Text()+"\n"))
}
}()
}
func delete_empty(s []string) []string {
var r []string
for _, str := range s {
if str != "" {
r = append(r, str)
}
}
return r
}
func runner_killer() {
mainPID := os.Getpid()
// TODO: Hack
cmd := exec.Command("sh", "-c", fmt.Sprintf("ps aux | grep go-build | grep -v grep | grep -v %s | awk '{ print $2 }'", strconv.Itoa(mainPID)))
output, err := cmd.Output()
if err != nil {
fmt.Println("Error running command:", err)
return
}
outputStr := strings.TrimSpace(string(output))
pids := delete_empty(strings.Split(outputStr, "\n"))
if len(pids) == 0 {
return
}
for _, pidStr := range pids {
if pidStr != "" {
pid, err := strconv.Atoi(pidStr)
if err != nil {
fmt.Printf("Error parsing PID: %s\n", err)
return
}
killCmd := exec.Command("kill", strconv.Itoa(pid))
err = killCmd.Start()
if err != nil {
fmt.Printf("Error killing process %d: %s\n", pid, err)
} else {
err := killCmd.Wait()
if err != nil {
fmt.Printf("Error waiting for process %d to exit: %s\n", pid, err)
} else {
fmt.Println(color.YellowString("Killed process %d", pid))
}
}
}
}
runner_killer()
}
func main() {
path := flag.String("path", ".", "go project path")
pkg := flag.String("package", ".", "go package or file name")
flag.Parse()
log.Println("golive started 👀")
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatalf("failed to create watcher: %s", err)
}
defer watcher.Close()
t := time.Now()
go func() {
wg := sync.WaitGroup{}
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Op.Has(fsnotify.Chmod) || strings.HasSuffix(event.Name, ".swp") {
continue
}
if time.Since(t).Seconds() <= 1.0 {
continue
}
t = time.Now()
wg.Add(1)
go runner(context.Background(), &wg, RunnerOpts{
path: *path,
args: []string{*pkg},
})
wg.Wait()
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Printf("watcher error: %s", err)
}
}
}()
err = watcher.Add(*path)
if err != nil {
log.Fatalf("failed to add path to watcher: %s", err)
}
ctx, cancel := context.WithCancel(context.Background())
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
go func() {
<-sigCh
cancel()
}()
<-ctx.Done()
log.Println("shutting down golive gracefully. Bye 👋")
if err := watcher.Close(); err != nil {
log.Printf("failed to close watcher: %s", err)
}
}