Skip to content

Add support for -on-build= and -on-fail= #7

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 41 additions & 0 deletions daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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()
}
}
}
Expand Down Expand Up @@ -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()

Expand Down