commands working, need to change to directory watch as directed by fsnotify doc
This commit is contained in:
parent
d7e1c5c59b
commit
d264029cfd
1 changed files with 48 additions and 13 deletions
61
main.go
61
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() {
|
||||
|
|
Loading…
Add table
Reference in a new issue