From 8d4e22c382531b654be074cac558f55914ae8a3e Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Wed, 24 Dec 2025 23:25:11 -0500 Subject: [PATCH] Basic idea is working. It watches a directory and runs a command when you change things. --- go.mod | 4 +++ go.sum | 4 +++ main.go | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 82 insertions(+), 10 deletions(-) create mode 100644 go.sum diff --git a/go.mod b/go.mod index 0d7e0ba..ea5860a 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,7 @@ module lcthw.dev/go/ttarpit go 1.25.3 + +require github.com/fsnotify/fsnotify v1.9.0 + +require golang.org/x/sys v0.13.0 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..c1e3272 --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k= +github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0= +golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= +golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/main.go b/main.go index 1182741..9a252e8 100644 --- a/main.go +++ b/main.go @@ -6,11 +6,14 @@ import ( "log" "io" "os" + "io/fs" + "path/filepath" "errors" "lcthw.dev/go/ttarpit/config" - "sync" + "github.com/fsnotify/fsnotify" ) + func LaunchLogger(in io.Reader, out io.Writer, err error) { if err != nil { log.Fatal(err) } @@ -50,11 +53,7 @@ func LaunchProcess(proc *config.Process) { } } -func main() { - config.Load() - - var wg sync.WaitGroup - +func RunBuild() { fmt.Println("CONFIG:", config.Settings.ConfigPath) fmt.Println("COMMANDs:", config.Settings.Processes) @@ -62,11 +61,76 @@ func main() { fmt.Println("PROCESS:", name) proc.Ready = make(chan string) - wg.Go(func() { - LaunchProcess(&proc) - }) + LaunchProcess(&proc) + } +} + +func AddWatchDir(watcher *fsnotify.Watcher, name string) error { + return filepath.WalkDir(name, + func(path string, d fs.DirEntry, err error) error { + if err != nil { + log.Printf("WATCH ERROR! walking=%s path=%s: err=%v", name, path, err) + return err + } + + if d.IsDir() { + log.Println("WATCHING: ", path) + err = watcher.Add(path) + if err != nil { + log.Printf("failed to watch %s", path) + return err + } + } + + return nil + }) +} + +func WatchDir() { + watcher, err := fsnotify.NewWatcher() + if err != nil { + log.Fatal("Failed to start fsnotify", err) + } + defer watcher.Close() + + go func() { + for { + select { + case event, ok := <-watcher.Events: + if !ok { + return + } + + if event.Has(fsnotify.Create) { + log.Println("---> CREATE", event.Name) + // ADD DIR + } else { + log.Println("event:", event) + } + // check if match then do thing + go RunBuild() + case err, ok := <-watcher.Errors: + if !ok { + return + } + log.Println("error: ", err) + } + } + }() + + err = AddWatchDir(watcher, ".") + + if err != nil { + log.Fatal(err, "Failed to watch .") } + <-make(chan struct{}) +} + + +func main() { + config.Load() + fmt.Println("WAITING....") - wg.Wait() + WatchDir() }