Skip to content

Commit

Permalink
Added watch mode on apply command
Browse files Browse the repository at this point in the history
  • Loading branch information
dkapanidis committed May 27, 2016
1 parent a86fa6c commit 331a00a
Showing 1 changed file with 52 additions and 4 deletions.
56 changes: 52 additions & 4 deletions cmd/tide/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io/ioutil"
"log"

"github.com/howeyc/fsnotify"
"github.com/spf13/cobra"
)

Expand All @@ -23,13 +24,60 @@ var applyCmd = &cobra.Command{
}

func runApply(cmd *cobra.Command, args []string) error {
log.SetOutput(ioutil.Discard)
setupInstallEnv(args)
manifest, _ := readManifest(installArg)
execute("apply", manifest)
setupApplyEnv(args)
if watch {
// log.SetOutput(ioutil.Discard)
manifest, _ := readManifest(installArg)
execute("apply", manifest)
watchForApply()
} else {
log.SetOutput(ioutil.Discard)
manifest, _ := readManifest(installArg)
execute("apply", manifest)
}
return nil
}

func watchForApply() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}

done := make(chan bool)

// Process events
go func() {
for {
select {
case ev := <-watcher.Event:
log.Println("event:", ev)
log.SetOutput(ioutil.Discard)
manifest, _ := readManifest(installArg)
execute("apply", manifest)
case err := <-watcher.Error:
log.Println("error:", err)
}
}
}()
log.Printf("dir %s", installArg)
err = watcher.Watch(installArg)
if err != nil {
log.Fatal(err)
}

err = watcher.Watch(installArg + "/templates")
if err != nil {
log.Fatal(err)
}

// Hang so program doesn't exit
<-done

/* ... do stuff ... */
watcher.Close()
}

func setupApplyEnv(args []string) {
if len(args) > 0 {
installArg = args[0]
Expand Down

0 comments on commit 331a00a

Please sign in to comment.