From 9900dd7015ce5096db9ca59d93c8b1f8f0a3d795 Mon Sep 17 00:00:00 2001 From: Jim Studt Date: Mon, 1 Dec 2014 15:22:31 -0600 Subject: [PATCH] Add support for -on-build= and -on-fail= --- README.md | 4 +++- daemon.go | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a431ccb..5a97f4d 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,9 @@ Usage: |`-exclude=…` | none | Exclude files matching this glob pattern, e.g. ".#*" ignores emacs temporary files. You may have multiples of this flag.| |`-include=…` | none | Include files whose last path component matches this glob pattern. You may have multiples of this flag.| |`-pattern=…` | (.+\\.go|.+\\.c)$ | A regular expression which matches the files to watch. The default watches *.go* and *.c* files.| - +|`-on-build=…` | *none* | Execute this command after a successful build. You may have multiples of this flag. Unlike `-command` these commands are expected to terminate.| +|`-on-fail=…` | *none* | Execute this command after a failed build. You may have multiples of this flag.| + ## Examples In its simplest form, the defaults will do. With the current working directory set diff --git a/daemon.go b/daemon.go index 4b82bc7..ec7e2f4 100644 --- a/daemon.go +++ b/daemon.go @@ -44,6 +44,8 @@ There are command line options. ACTIONS -build=CCC – Execute CCC to rebuild when a file changes -command=CCC – Run command CCC after a successful build, stops previous command first + -on-build=CCC - Run command CCC after a successful build, this should terminate. + -on-fail=CCC - Run command CCC after a failed build, this should terminate. */ package main @@ -94,6 +96,40 @@ func (g *globList) Matches(value string) bool { return false } +type cmdList []string + +var onBuilds cmdList +var onFails cmdList + +func (c *cmdList) String() string { + return fmt.Sprintf("%@", *c) +} +func (c *cmdList) Set(value string) error { + *c = append(*c, value) + return nil +} +func (c *cmdList) Notify() bool { + allGood := true + + shell := os.Getenv("SHELL") + if shell == "" { + shell = "sh" + } + + for _, cmd := range *c { + p := exec.Command(shell, "-c", cmd) + p.Stdout = os.Stdout + p.Stderr = os.Stderr + if err := p.Run(); err != nil { + allGood = false + log.Printf("Notification failed") + log.Printf(" %s", cmd) + log.Printf(" %s", err.Error()) + } + } + return allGood +} + var ( flag_directory = flag.String("directory", ".", "Directory to watch for changes") flag_pattern = flag.String("pattern", FilePattern, "Pattern of watched files") @@ -147,7 +183,10 @@ func builder(jobs <-chan string, buildDone chan<- struct{}) { threshold = createThreshold() case <-threshold: if build() { + onBuilds.Notify() buildDone <- struct{}{} + } else { + onFails.Notify() } } } @@ -248,6 +287,8 @@ func main() { flag.Var(&excludedDirs, "exclude-dir", " Don't watch directories matching this name") flag.Var(&excludedFiles, "exclude", " Don't watch files matching this name") flag.Var(&includedFiles, "include", " Watch files matching this name") + flag.Var(&onBuilds, "on-build", "Execute this command after a successful build") + flag.Var(&onFails, "on-fail", "Execute this command after a failed build") flag.Parse()