Skip to content
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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()`.

Expand Down
43 changes: 29 additions & 14 deletions reload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return
return result

}

// Exec replaces the current process with a new copy of itself.
func Exec() {
execName := binSelf
Expand Down Expand Up @@ -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
}