From 3a6b94f57eaa933c0b3ac1540e2c9257e90c4a8d Mon Sep 17 00:00:00 2001 From: Andy Davis Date: Thu, 7 Sep 2023 14:57:45 -0500 Subject: [PATCH 1/3] Create a ListDirs method for convienient recorsive scan of directories --- reload.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/reload.go b/reload.go index 80c3a6d..839cfe7 100644 --- a/reload.go +++ b/reload.go @@ -27,6 +27,7 @@ package reload // import "github.com/teamwork/reload" import ( "fmt" + "io/fs" "math" "os" "path/filepath" @@ -206,3 +207,17 @@ func relpath(p string) string { return p } + +func ListDirs(rootPath string, cb func()) (result []dir) { + filepath.WalkDir(rootPath, func(dirPath string, d fs.DirEntry, err error) error { + if err != nil { + return fs.SkipDir + } + if d.IsDir() { + result = append(result, Dir(dirPath, cb)) + } + return nil + }) + + return +} From 57c590c8306d14c19ee3a6b9483e3286bb978161 Mon Sep 17 00:00:00 2001 From: Andy Davis Date: Fri, 8 Sep 2023 10:58:21 -0500 Subject: [PATCH 2/3] Implement directory recursion in reload.Dir() - just look for a trailing slash --- README.md | 2 ++ reload.go | 43 +++++++++++++++++++++++++++++-------------- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index cad2c8a..d7db558 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,8 @@ func main() { } ``` +If the path argument to `reload.Dir()` contains a trailing slash, the directory will be evaluated recursively. + You can also use `reload.Exec()` to manually restart your process without calling `reload.Do()`. diff --git a/reload.go b/reload.go index 839cfe7..d0f851f 100644 --- a/reload.go +++ b/reload.go @@ -80,6 +80,8 @@ func Do(log func(string, ...interface{}), additional ...dir) error { } timers[binSelf] = stoppedTimer(Exec) + additional = expandAdditional(log, additional) + // Watch the directory, because a recompile renames the existing // file (rather than rewriting it), so we won't get events for that. dirs := make([]string, len(additional)+1) @@ -152,6 +154,33 @@ func Do(log func(string, ...interface{}), additional ...dir) error { return nil } +func expandAdditional(log func(string, ...interface{}), additional []dir) []dir { + result := make([]dir, 0, len(additional)) + for _, a := range additional { + if lastChar := a.path[len(a.path)-1]; lastChar == '\\' || lastChar == '/' { + result = append(result, listDirs(log, a.path, a.cb)...) + } else { + result = append(result, a) + } + } + return result +} + +func listDirs(log func(string, ...interface{}), rootPath string, cb func()) (result []dir) { + filepath.WalkDir(rootPath, func(dirPath string, d fs.DirEntry, err error) error { + if err != nil { + log("Error reading %s, skipped", dirPath) + return fs.SkipDir + } + if d.IsDir() { + result = append(result, Dir(dirPath, cb)) + } + return nil + }) + + return +} + // Exec replaces the current process with a new copy of itself. func Exec() { execName := binSelf @@ -207,17 +236,3 @@ func relpath(p string) string { return p } - -func ListDirs(rootPath string, cb func()) (result []dir) { - filepath.WalkDir(rootPath, func(dirPath string, d fs.DirEntry, err error) error { - if err != nil { - return fs.SkipDir - } - if d.IsDir() { - result = append(result, Dir(dirPath, cb)) - } - return nil - }) - - return -} From 61ff2bb3968bf9928901abfc8f5e5bc023195671 Mon Sep 17 00:00:00 2001 From: Andy Davis Date: Mon, 11 Sep 2023 12:06:08 -0500 Subject: [PATCH 3/3] Use /... to indicate recursion instead of just a trailing slash --- README.md | 2 +- reload.go | 30 ++++++++++++++++-------------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index d7db558..387a9be 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ func main() { } ``` -If the path argument to `reload.Dir()` contains a trailing slash, the directory will be evaluated recursively. +If the path argument to `reload.Dir()` ends with /... or \..., the directory will be evaluated recursively. You can also use `reload.Exec()` to manually restart your process without calling `reload.Do()`. diff --git a/reload.go b/reload.go index d0f851f..9ba5378 100644 --- a/reload.go +++ b/reload.go @@ -5,21 +5,21 @@ // // Example: // -// go func() { -// err := reload.Do(log.Printf) -// if err != nil { -// panic(err) -// } -// }() +// go func() { +// err := reload.Do(log.Printf) +// if err != nil { +// panic(err) +// } +// }() // // A list of additional directories to watch can be added: // -// go func() { -// err := reload.Do(log.Printf, reload.Dir("tpl", reloadTpl) -// if err != nil { -// panic(err) -// } -// }() +// go func() { +// err := reload.Do(log.Printf, reload.Dir("tpl", reloadTpl) +// if err != nil { +// panic(err) +// } +// }() // // Note that this package won't prevent race conditions (e.g. when assigning to // a global templates variable). You'll need to use sync.RWMutex yourself. @@ -157,8 +157,10 @@ func Do(log func(string, ...interface{}), additional ...dir) error { func expandAdditional(log func(string, ...interface{}), additional []dir) []dir { result := make([]dir, 0, len(additional)) for _, a := range additional { - if lastChar := a.path[len(a.path)-1]; lastChar == '\\' || lastChar == '/' { - result = append(result, listDirs(log, a.path, a.cb)...) + if strings.HasSuffix(a.path, "\\...") { + result = append(result, listDirs(log, strings.TrimSuffix(a.path, "\\..."), a.cb)...) + } else if strings.HasSuffix(a.path, "/...") { + result = append(result, listDirs(log, strings.TrimSuffix(a.path, "/..."), a.cb)...) } else { result = append(result, a) }