-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable directory recursion in Dir() #15
base: master
Are you sure you want to change the base?
Changes from all commits
3a6b94f
57c590c
61ff2bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -5,28 +5,29 @@ | |||||
// | ||||||
// 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. | ||||||
package reload // import "github.com/teamwork/reload" | ||||||
|
||||||
import ( | ||||||
"fmt" | ||||||
"io/fs" | ||||||
"math" | ||||||
"os" | ||||||
"path/filepath" | ||||||
|
@@ -79,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) | ||||||
|
@@ -151,6 +154,35 @@ 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 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) | ||||||
} | ||||||
} | ||||||
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) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMHO it's better to return an error on this (from reload.Do()), rather than skipping it. It's on startup, and skipping directories (even with a print) is somewhat confusing, IMHO. |
||||||
return fs.SkipDir | ||||||
} | ||||||
if d.IsDir() { | ||||||
result = append(result, Dir(dirPath, cb)) | ||||||
} | ||||||
return nil | ||||||
}) | ||||||
|
||||||
return | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
// Exec replaces the current process with a new copy of itself. | ||||||
func Exec() { | ||||||
execName := binSelf | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'll need to update the comment on the Dir() function as well.