diff --git a/main.go b/main.go index 22f6fa2..8d78216 100644 --- a/main.go +++ b/main.go @@ -66,29 +66,53 @@ func listen(files map[string]bool, condition string, cksum bool, interval string log.Fatal(err) } + defer watcher.Close() for key := range files { watcher.Add(key) + fmt.Println(watcher.WatchList()) } go func() { for { + var renameAdd []string = []string{} + // hasher := sha256.New() trigger := false select { case event, ok := <-watcher.Events: if !ok { return } - fmt.Println(event) + // if _, err := io.Copy(hasher, event.); err != nil { + + // } + // fmt.Printf("%x", event) for key := range files { - if event.Has(fsnotify.Write) && event.Name == key { - if condition == "any" { - trigger = true - break + if event.Name == key { + if event.Has(fsnotify.Write) { + if condition == "any" { + trigger = true + break + } + + files[key] = true + } + + if event.Has(fsnotify.Rename) { + // we need to rewatch file + // sleeping small amount to allow CREATE event to propogate + time.Sleep(10 * time.Millisecond) + // ... then adding to a list to allow additional time + renameAdd = append(renameAdd, key) + if condition == "any" { + trigger = true + break + } + + files[key] = true } - files[key] = true } - } + } // end for if condition == "all" { trigger = true @@ -98,23 +122,34 @@ func listen(files map[string]bool, condition string, cksum bool, interval string break } } - } - + } // end if condition + // end case event case _, ok := <-watcher.Errors: if !ok { return } - } + } // end switch + // end case errors if trigger { if len(command) >= 1 { - exec.Command(command[0], command[1:]...) + cmd := exec.Command(command[0], command[1:]...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + cmd.Run() } else { quit <- true } + } // end if trigger + + for _, value := range renameAdd { + err := watcher.Add(value) + if err != nil { + fmt.Println(err) + } } - } - }() + } // end for + }() // end go func } else { go func() {