diff --git a/main.go b/main.go index 1dea9dd..cbdbac4 100644 --- a/main.go +++ b/main.go @@ -13,22 +13,25 @@ Traffic (https://github.com/pilu/traffic) already has a middleware that shows th package main import ( - "flag" "fmt" - "github.com/pilu/fresh/runner" + "github.com/icattlecoder/fresh/runner" "os" ) func main() { - configPath := flag.String("c", "", "config file path") - flag.Parse() - if *configPath != "" { - if _, err := os.Stat(*configPath); err != nil { - fmt.Printf("Can't find config file `%s`\n", *configPath) + configPath := "" + args := os.Args + if len(args) > 2 && args[1] == "-c" { + configPath = args[2] + } + + if configPath != "" { + if _, err := os.Stat(configPath); err != nil { + fmt.Printf("Can't find config file `%s`\n", configPath) os.Exit(1) } else { - os.Setenv("RUNNER_CONFIG_PATH", *configPath) + os.Setenv("RUNNER_CONFIG_PATH", configPath) } } diff --git a/runner.conf.sample b/runner.conf.sample index 5ac3c05..23a4beb 100644 --- a/runner.conf.sample +++ b/runner.conf.sample @@ -1,4 +1,5 @@ root: . +watch_path: . tmp_path: ./tmp build_name: runner-build build_log: runner-build-errors.log diff --git a/runner/runner.go b/runner/runner.go index f15f89a..8d60911 100644 --- a/runner/runner.go +++ b/runner/runner.go @@ -2,6 +2,7 @@ package runner import ( "io" + "os" "os/exec" ) @@ -10,6 +11,10 @@ func run() bool { cmd := exec.Command(buildPath()) + if len(os.Args) > 2 && os.Args[1] == "-c" { + cmd.Args = append([]string{buildPath()}, os.Args[3:]...) + } + stderr, err := cmd.StderrPipe() if err != nil { fatal(err) diff --git a/runner/settings.go b/runner/settings.go index 61e2412..0f2fbcc 100644 --- a/runner/settings.go +++ b/runner/settings.go @@ -18,6 +18,7 @@ const ( var settings = map[string]string{ "config_path": "./runner.conf", "root": ".", + "watch_path": ".", "tmp_path": "./tmp", "build_name": "runner-build", "build_log": "runner-build-errors.log", @@ -108,6 +109,10 @@ func root() string { return settings["root"] } +func watchPath() string { + return settings["watch_path"] +} + func tmpPath() string { return settings["tmp_path"] } @@ -133,6 +138,5 @@ func configPath() string { func buildDelay() time.Duration { value, _ := strconv.Atoi(settings["build_delay"]) - - return time.Duration(value) + return time.Duration(value) * time.Millisecond } diff --git a/runner/start.go b/runner/start.go index b382dc3..c1c1175 100644 --- a/runner/start.go +++ b/runner/start.go @@ -19,12 +19,16 @@ var ( appLog logFunc ) -func flushEvents() { +func flushEvents(delay time.Duration) { + for { + t := time.NewTicker(delay) select { case eventName := <-startChannel: mainLog("receiving event %s", eventName) - default: + t.Stop() + case _ = <-t.C: + t.Stop() return } } @@ -43,11 +47,10 @@ func start() { eventName := <-startChannel mainLog("receiving first event %s", eventName) - mainLog("sleeping for %d milliseconds", buildDelay) - time.Sleep(buildDelay * time.Millisecond) + mainLog("sleeping for %d milliseconds", buildDelay/1e6) mainLog("flushing events") - flushEvents() + flushEvents(buildDelay) mainLog("Started! (%d Goroutines)", runtime.NumGoroutine()) err := removeBuildErrorsLog() diff --git a/runner/watcher.go b/runner/watcher.go index 7df2706..ec4a583 100644 --- a/runner/watcher.go +++ b/runner/watcher.go @@ -1,7 +1,7 @@ package runner import ( - "github.com/howeyc/fsnotify" + "github.com/fsnotify/fsnotify" "os" "path/filepath" "strings" @@ -16,19 +16,19 @@ func watchFolder(path string) { go func() { for { select { - case ev := <-watcher.Event: + case ev := <-watcher.Events: if isWatchedFile(ev.Name) { watcherLog("sending event %s", ev) startChannel <- ev.String() } - case err := <-watcher.Error: + case err := <-watcher.Errors: watcherLog("error: %s", err) } } }() watcherLog("Watching %s", path) - err = watcher.Watch(path) + err = watcher.Add(path) if err != nil { fatal(err) @@ -36,8 +36,13 @@ func watchFolder(path string) { } func watch() { - root := root() - filepath.Walk(root, func(path string, info os.FileInfo, err error) error { + watchPath := watchPath() + + if !filepath.IsAbs(watchPath) { + watchPath, _ = filepath.Abs(watchPath) + } + + filepath.Walk(watchPath, func(path string, info os.FileInfo, err error) error { if info.IsDir() && !isTmpDir(path) { if len(path) > 1 && strings.HasPrefix(filepath.Base(path), ".") { return filepath.SkipDir