-
Notifications
You must be signed in to change notification settings - Fork 0
/
f5.go
244 lines (221 loc) · 5.04 KB
/
f5.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package f5
import (
"context"
"fmt"
"io/fs"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"github.com/fsnotify/fsnotify"
"github.com/pkg/term"
"github.com/tj/go-terminput"
)
var (
// extension of top langauges
supportedExtensionMap = map[string]bool{}
supportedExtensions = []string{
".py", ".js", ".java", ".ts", ".go",
".cpp", ".rb", ".php", ".cs", ".c",
}
)
const (
colorReset = "\033[0m"
colorRed = "\033[31m"
colorGreen = "\033[32m"
colorYellow = "\033[33m"
colorBlue = "\033[34m"
colorPurple = "\033[35m"
colorCyan = "\033[36m"
colorWhite = "\033[37m"
separator = "------------------------------------------------------------------"
)
func init() {
for _, s := range supportedExtensions {
supportedExtensionMap[s] = true
}
}
func (r *Run) printf(color string, format string, a ...any) {
f := color + format + colorReset
r.logger.Printf(f, a...)
}
func (r *Run) usagef(color string, format string, a ...any) {
f := color + format + colorReset
r.usage.Printf(f, a...)
}
type Run struct {
args []string
process *os.Process
watcher *fsnotify.Watcher
term *term.Term
restart chan bool
logger *log.Logger
usage *log.Logger
}
func New(args ...string) (*Run, error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
t, err := term.Open("/dev/tty")
if err != nil {
return nil, err
}
fn := filepath.Base(args[0])
prefix := fmt.Sprintf("%s[Press F5 to refresh %q] %s", colorGreen, fn, colorReset)
logger := log.New(os.Stderr, prefix, log.LstdFlags)
usage := log.New(os.Stderr, prefix, 0)
r := Run{
args: args,
restart: make(chan bool, 100),
watcher: watcher,
term: t,
logger: logger,
usage: usage,
}
return &r, nil
}
func (r *Run) kill() {
if r.process != nil {
pid := r.process.Pid
err := syscall.Kill(-pid, syscall.SIGINT)
if err != nil && !strings.Contains(err.Error(), "no such process") {
r.printf(colorRed, "Process %d: cannot interrupt: %v", pid, err)
r.printf(colorPurple, "Process %d: sending sigkill", pid)
err := syscall.Kill(-pid, syscall.SIGKILL)
if err != nil {
r.printf(colorRed, "Process %d: cannot be killed: %v", pid, err)
}
}
r.process = nil
}
}
func (r *Run) Close() {
r.term.Restore()
r.watcher.Close()
r.kill()
}
func (r *Run) Restart(ctx context.Context) {
r.kill()
cmd := exec.Command(r.args[0], r.args[1:]...)
// set process group, so we can kill all of the spawned processes.
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Start()
if err != nil {
r.printf(colorRed, "Cannot run command: %v", err)
return
}
r.process = cmd.Process
fmt.Printf("%s%s\n", colorGreen, separator)
r.printf(colorWhite, "Process %d started for command: %s%s", cmd.Process.Pid, colorCyan, cmd)
fmt.Printf("%s%s%s\n", colorGreen, separator, colorReset)
go cmd.Wait()
}
func (r *Run) Start(ctx context.Context) error {
fmt.Printf("%s%s\n", colorGreen, separator)
r.usagef(colorWhite, "To restart the running program, press F5 or SPACE or Ctrl-R, or just make file changes.")
go func() {
for {
select {
case <-r.restart:
r.Restart(ctx)
case <-ctx.Done():
return
}
}
}()
defer func() {
r.restart <- true
}()
return r.watch(ctx)
}
func (r *Run) ListenForKeys(ctx context.Context) {
r.term.SetCbreak()
defer r.term.Restore()
for {
if ctx.Err() != nil {
return
}
e, _ := terminput.Read(r.term)
// log.Printf("got: %s", e.String())
switch e.String() {
case "DC2":
fallthrough
case " ":
fallthrough
case "F5":
r.Restart(ctx)
}
}
}
func (r *Run) watch(ctx context.Context) error {
wd, err := os.Getwd()
if err != nil {
return err
}
dirs := []string{}
filepath.WalkDir(wd, func(s string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if !d.IsDir() {
return nil
}
// skip hidden directories with . as prefix
if strings.HasPrefix(filepath.Base(s), ".") {
return filepath.SkipDir
}
// check if the directory has go code.
files, err := ioutil.ReadDir(s)
if err != nil {
return err
}
for _, f := range files {
if supportedExtensionMap[filepath.Ext(f.Name())] {
dirs = append(dirs, s)
return nil
}
}
return nil
})
r.usagef(colorWhite, "The following directories are being monitored")
for i, d := range dirs {
r.usagef(colorWhite, "%3d. %s", i+1, d)
r.watcher.Add(d)
}
// watch until error or cancelled.
go func() {
defer r.watcher.Close()
for {
select {
case <-ctx.Done():
return
case event, ok := <-r.watcher.Events:
if !ok {
r.printf(colorRed, "Unknown event, halting.")
return
}
if event.Op&fsnotify.Write != fsnotify.Write {
continue
}
if !supportedExtensionMap[filepath.Ext(event.Name)] {
continue
}
r.printf(colorGreen, "Modified file: %s", event.Name)
r.restart <- true
case err, ok := <-r.watcher.Errors:
if !ok {
r.printf(colorRed, "Unknown error, halting.")
return
}
r.printf(colorRed, "Error:", err)
}
}
}()
return nil
}