-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
92 lines (80 loc) · 2.18 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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"github.com/alecthomas/kong"
"github.com/alecthomas/repr"
"github.com/fsnotify/fsnotify"
)
var CLI struct {
Watch struct {
GlobPattern string `arg:"" required:"" name:"glob" help:"Glob pattern to watch files." type:"string"`
Command string `arg:"" required:"" name:"command" help:"Command to run upon file changes." type:"string"`
Args []string `arg:"" required:"" name:"args" help:"Additional arguments for the command."`
} `cmd:"" help:"Watch files and run a command upon changes."`
}
func main() {
ctx := kong.Parse(&CLI)
// fmt.Printf("Command: %s\n", ctx.Command())
switch ctx.Command() {
case "watch <glob> <command> <args>":
globPattern := CLI.Watch.GlobPattern
cmdArgs := append([]string{CLI.Watch.Command}, CLI.Watch.Args...)
watcher, err := fsnotify.NewWatcher()
if err != nil {
fmt.Printf("Error creating watcher: %v\n", err)
return
}
defer watcher.Close()
files, err := filepath.Glob(globPattern)
if err != nil {
fmt.Printf("Error matching files: %v\n", err)
return
}
if len(files) == 0 {
fmt.Printf("No files found matching %s\n", globPattern)
return
}
for _, file := range files {
err = watcher.Add(file)
if err != nil {
fmt.Printf("Error watching file %s: %v\n", file, err)
continue
}
}
fmt.Printf("Watching %d files matching %s\n", len(files), globPattern)
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create {
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
repr.Print(cmd)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
fmt.Printf("Error running command: %v\n", err)
}
}
newFiles, err := filepath.Glob(globPattern)
for _, file := range newFiles {
if err = watcher.Add(file); err != nil {
fmt.Printf("Error watching new file %s: %v\n", file, err)
}
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
fmt.Printf("Watcher error: %v\n", err)
}
}
default:
panic(ctx.Command())
}
}