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

Support multiple -directory flags #32

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
136 changes: 93 additions & 43 deletions daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,34 @@ func (g *globList) Matches(value string) bool {
return false
}

type dirList map[string]struct{}

func (d *dirList) String() string {
keys := make([]string, 0, len(*d))
for dir := range *d {
keys = append(keys, dir)
}
return fmt.Sprint(keys)
}
func (d *dirList) Set(value string) error {
var clean_value string
if value != "" {
clean_value = filepath.Clean(value)
}

// dirList must be unique, use a zero-byte struct map as a set.
(*d)[clean_value] = struct{}{}
Copy link
Owner

Choose a reason for hiding this comment

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

I don't see why we would add a directory entry if value == "". Is this intended?

Copy link
Author

Choose a reason for hiding this comment

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

So we can report the error later, which I believe maintains the existing functionality.

Copy link
Owner

Choose a reason for hiding this comment

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

But what's there to report? You'd simply have (*d)[""] = struct{}{}, which would also mean that the length of the dir list would be non-zero (although there was an invalid directory supplied) and .First() will return "".

Copy link
Author

Choose a reason for hiding this comment

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

I'm reporting what was previously on 371 and is now on 408 (although I should have put that in validateFlags). Not adding it to the list would be ignoring it.

Copy link
Author

Choose a reason for hiding this comment

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

I've moved directory validation into validateFlags to be more clear.


return nil
}
func (d *dirList) First() string {
for dir := range *d {
return dir
}
return ""
}

var (
flag_directory = flag.String("directory", ".", "Directory to watch for changes")
flag_pattern = flag.String("pattern", FilePattern, "Pattern of watched files")
flag_command = flag.String("command", "", "Command to run and restart after build")
flag_command_stop = flag.Bool("command-stop", false, "Stop command before building")
Expand All @@ -121,6 +147,7 @@ var (
flag_verbose = flag.Bool("verbose", false, "Be verbose about which directories are watched.")

// initialized in main() due to custom type.
flag_directories dirList
flag_excludedDirs globList
flag_excludedFiles globList
flag_includedFiles globList
Expand Down Expand Up @@ -153,12 +180,7 @@ func build() bool {
}

cmd := exec.Command(args[0], args[1:]...)

if *flag_build_dir != "" {
cmd.Dir = *flag_build_dir
} else {
cmd.Dir = *flag_directory
}
cmd.Dir = *flag_build_dir

output, err := cmd.CombinedOutput()

Expand Down Expand Up @@ -356,7 +378,69 @@ func flusher(buildStarted <-chan string, buildSuccess <-chan bool) {
}
}

func watchDirectories(watcher *fsnotify.Watcher) {
for dir := range flag_directories {
if !*flag_recursive {
if err := watcher.Add(dir); err != nil {
log.Fatal("watcher.Add():", err)
}

return
}

err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err == nil && info.IsDir() {
if flag_excludedDirs.Matches(path) {
return filepath.SkipDir
}

if *flag_verbose {
log.Printf("Watching directory '%s' for changes.\n", path)
}

return watcher.Add(path)
}

return err
})

if os.IsNotExist(err) {
log.Fatalf("-directory=%s does not exist", dir)
}

if err != nil {
log.Fatal("filepath.Walk():", err)
}

if err := watcher.Add(dir); err != nil {
log.Fatal("watcher.Add():", err)
}
}
}

func validateFlags() {
if len(flag_directories) == 0 {
flag_directories.Set(".")
}

if *flag_build_dir == "" {
if len(flag_directories) == 1 {
default_build_dir := flag_directories.First()
flag_build_dir = &default_build_dir
} else {
fmt.Fprintf(os.Stderr, "-build-dir is required when specifying multiple watch directeries.\n")
os.Exit(1)
Copy link
Owner

Choose a reason for hiding this comment

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

You might want to use log.Fatal() here which does both of these things in one call.

Copy link
Author

Choose a reason for hiding this comment

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

No problem. I cut+pasted this from somewhere else so I was just following what existied.

}
}

if *flag_gracefulkill && !gracefulTerminationPossible() {
log.Fatal("Graceful termination is not supported on your platform.")
}
}

func main() {
flag_directories = make(dirList)
flag.Var(&flag_directories, "directory", "Directory to watch for changes")
flag.Var(&flag_excludedDirs, "exclude-dir", " Don't watch directories matching this name")
flag.Var(&flag_excludedFiles, "exclude", " Don't watch files matching this name")
flag.Var(&flag_includedFiles, "include", " Watch files matching this name")
Expand All @@ -367,14 +451,7 @@ func main() {
log.SetFlags(0)
}

if *flag_directory == "" {
fmt.Fprintf(os.Stderr, "-directory=... is required.\n")
os.Exit(1)
}

if *flag_gracefulkill && !gracefulTerminationPossible() {
log.Fatal("Graceful termination is not supported on your platform.")
}
validateFlags()

watcher, err := fsnotify.NewWatcher()

Expand All @@ -384,34 +461,7 @@ func main() {

defer watcher.Close()

if *flag_recursive == true {
err = filepath.Walk(*flag_directory, func(path string, info os.FileInfo, err error) error {
if err == nil && info.IsDir() {
if flag_excludedDirs.Matches(path) {
return filepath.SkipDir
} else {
if *flag_verbose {
log.Printf("Watching directory '%s' for changes.\n", path)
}
return watcher.Add(path)
}
}
return err
})

if err != nil {
log.Fatal("filepath.Walk():", err)
}

if err := watcher.Add(*flag_directory); err != nil {
log.Fatal("watcher.Add():", err)
}

} else {
if err := watcher.Add(*flag_directory); err != nil {
log.Fatal("watcher.Add():", err)
}
}
watchDirectories(watcher)
Copy link
Owner

Choose a reason for hiding this comment

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

I think this is not the best way to refactor this. How about this:

func watchDirectory(dir string, watcher *fsnotify.Watcher) {
// ...
}

func main() {
// ...
    for dir := range flag_directories { 
        watchDirectory(dir, watcher)
    }
}

Copy link
Author

Choose a reason for hiding this comment

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

I structured it this way (along with validateFlags) with an eye on cleaning up the main function since it's doing a lot directly (many things could stand to be broken out into more functions). However, it's not that big of a deal so I'm happy to make the change.


pattern := regexp.MustCompile(*flag_pattern)
jobs := make(chan string)
Expand Down