-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
82 lines (72 loc) · 1.68 KB
/
main.go
File metadata and controls
82 lines (72 loc) · 1.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
package main
import (
"context"
"log"
"os"
"os/signal"
"path/filepath"
"syscall"
"github.com/kitproj/sim/internal"
"github.com/fsnotify/fsnotify"
)
func init() {
log.SetOutput(os.Stdout)
}
func main() {
// Parse command-line flags
if len(os.Args) <= 1 {
os.Args = []string{"."}
}
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
// Find OpenAPI spec files in directory
sim := internal.NewSim()
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatalf("Error creating watcher: %s\n", err)
}
defer watcher.Close()
for _, path := range os.Args[1:] {
log.Printf("Adding path: %s\n", path)
if err := watcher.Add(path); err != nil {
log.Fatalf("Error watching directory: %s\n", err)
}
stat, err := os.Stat(path)
if err != nil {
log.Fatal(err)
}
if stat.IsDir() {
dir, err := os.ReadDir(path)
if err != nil {
log.Fatalf("Error reading directory: %s\n", err)
}
for _, file := range dir {
if filepath.Ext(file.Name()) != ".yaml" {
continue
}
if err := sim.Add(filepath.Join(path, file.Name())); err != nil {
log.Fatalf("Error adding spec: %s\n", err)
}
}
} else {
if err := sim.Add(path); err != nil {
log.Fatalf("Error adding spec: %s\n", err)
}
}
}
log.Println("Press Ctrl+C to exit")
for {
select {
case <-ctx.Done():
log.Println("Exiting...")
return
case event := <-watcher.Events:
log.Println("event:", event)
if filepath.Ext(event.Name) == ".yaml" && (event.Has(fsnotify.Write) || event.Has(fsnotify.Create)) {
if err := sim.Add(event.Name); err != nil {
log.Printf("Error adding spec: %s\n", err)
}
}
}
}
}