Skip to content

Instantly share code, notes, and snippets.

@mmirolim
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save mmirolim/fb5991a370393825fba9 to your computer and use it in GitHub Desktop.

Select an option

Save mmirolim/fb5991a370393825fba9 to your computer and use it in GitHub Desktop.
Watcher to automate some tasks (modify as you wish)
// temp solution to run browserify with watch
package main
import (
"flag"
"log"
"os"
"os/exec"
"strings"
"time"
"gopkg.in/fsnotify.v1"
)
var (
dir = flag.String("d", "", "dir to watch")
com = flag.String("c", "", "comand to exec")
)
func init() {
flag.Parse()
//flag.Usage()
}
func main() {
d := "C:/www/scala/simple-blog/src/main/webapp/WEB-INF/templates/js"
dae := "browserify"
excludeFile := "bundle.js"
c := dae + " " + d + "/app.js" + " -o " + d + "/" + excludeFile
w, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer w.Close()
// prepare slice
args := strings.Split(c, " ")
done := make(chan bool)
go func() {
for {
select {
case e := <-w.Events:
if e.Op&fsnotify.Write == fsnotify.Write {
// ignore exclude file
if strings.HasSuffix(e.Name, excludeFile) || !strings.HasSuffix(e.Name, ".js") {
continue
}
log.Println("File modified:", e.Name)
// execute command
cmd := exec.Command(args[0])
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Args = args
cmd.Env = os.Environ()
cmd.Run()
// wait
time.Sleep(2 * time.Second)
}
case err := <-w.Errors:
log.Println("Error:", err)
}
}
}()
err = w.Add(d)
if err != nil {
log.Fatalln(err)
}
<-done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment