-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
156 lines (135 loc) · 3.68 KB
/
main.go
File metadata and controls
156 lines (135 loc) · 3.68 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
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
package main
import (
"flag"
"github.com/cloudimpl/next-gen/lib"
"github.com/fsnotify/fsnotify"
"log"
"os"
"os/exec"
"os/signal"
"path/filepath"
"syscall"
)
func watch(appPath string, onChange func()) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatalf("Failed to create watcher: %v", err)
}
defer watcher.Close()
// Handle OS signals for graceful shutdown
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
go func() {
<-sigChan
log.Println("Received termination signal, shutting down watcher...")
watcher.Close()
}()
done := make(chan struct{})
go func() {
defer close(done)
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Op&fsnotify.Create == fsnotify.Create {
info, err := os.Stat(event.Name)
if err == nil && info.IsDir() {
log.Printf("New directory detected: %s, adding to watcher", event.Name)
if err := watcher.Add(event.Name); err != nil {
log.Printf("Failed to watch new directory: %s, error: %v", event.Name, err)
}
}
}
if event.Op&fsnotify.Write == fsnotify.Write {
if lib.IsGoFile(event.Name) {
if err := lib.CheckFileCompilable(event.Name); err == nil {
log.Printf("Change detected in: %s, triggering onChange", event.Name)
onChange()
} else {
log.Printf("File not compilable: %s, error: %v", event.Name, err)
}
}
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Printf("Watcher error: %v", err)
}
}
}()
err = filepath.Walk(appPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Printf("Error walking path: %s, error: %v", path, err)
return err
}
if info.IsDir() {
log.Printf("Adding directory to watcher: %s", path)
return watcher.Add(path)
}
return nil
})
if err != nil {
log.Fatalf("Failed to walk path: %v", err)
}
<-done
}
func generate(appPath string) {
err := lib.GenerateServices(appPath, true)
if err != nil {
log.Fatalf("Error generating services: %s\n", err.Error())
}
}
func watchAndGenerate(appPath string) {
// Ensure the directory exists
if _, err := os.Stat(appPath); os.IsNotExist(err) {
log.Fatalf("APP_PATH does not exist: %s", appPath)
}
servicesPath := filepath.Join(appPath, "services")
log.Printf("Starting watcher on: %s", servicesPath)
watch(servicesPath, func() {
err := lib.GenerateServices(appPath, true)
if err != nil {
log.Printf("Error generating services: %v", err)
}
})
}
// isGoImportsAvailable checks if the `goimports` command is available
func isGoImportsAvailable() bool {
_, err := exec.LookPath("goimports")
return err == nil
}
// installGoImports installs the `goimports` tool using `go install`
func installGoImports() error {
cmd := exec.Command("go", "install", "golang.org/x/tools/cmd/goimports@latest")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func main() {
cwd, err := os.Getwd()
if err != nil {
log.Fatalf("Failed to get current working directory: %v", err)
}
var appPath string
watch := flag.Bool("w", false, "watch for changes")
flag.StringVar(&appPath, "f", cwd, "app path")
flag.Parse()
// Check if `goimports` is installed
if !isGoImportsAvailable() {
log.Println("goimports is not installed. Installing now...")
// Attempt to install `goimports`
err := installGoImports()
if err != nil {
log.Fatalf("Failed to install goimports: %v. Please install it manually by running:\n\tgo install golang.org/x/tools/cmd/goimports@latest", err)
}
log.Println("goimports successfully installed.")
}
if *watch {
watchAndGenerate(appPath)
} else {
generate(appPath)
}
}