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 all commits
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()` ends with /... or \..., the directory will be evaluated recursively.
Copy link
Contributor

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.


You can also use `reload.Exec()` to manually restart your process without
calling `reload.Do()`.

Expand Down
56 changes: 44 additions & 12 deletions reload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
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