func main() { config := config.Get() configureLogger() printInfo() for { killing := task.New() restarting := task.New() watcher, err := fsnotify.NewWatcher() if err != nil { logger.PrintFatalError("fsnotify:", err) } addWatcher(watcher, config.Directory) defer watcher.Close() restart := fungo.Debounce(config.Delay, func() { restarting.Complete() }) task.Do(func(t *task.Task) { go func() { for { select { case <-restarting.Awaiter(): killing.Complete() t.Complete() return case e := <-watcher.Events: if e.Op&fsnotify.Write == fsnotify.Write { ext := path.Ext(e.Name) if len(ext) > 0 && ext[0] == '.' { ext = ext[1:] } for i := range config.Extensions { if config.Extensions[i] == ext { restart() break } } } case err := <-watcher.Errors: logger.PrintError("fsnotify: ", err) } } }() logger.PrintStatus("starting '" + config.Target + "'") binName := build() defer os.Remove(binName) bin := command(binName, config.Arguments) bin.Start() task.Do(func(t *task.Task) { go func() { killing.Await() if bin.Process != nil { bin.Process.Kill() } t.Complete() }() bin.Wait() killing.Complete() }) fmt.Println() logger.PrintStatus("waiting for changes...") }) } }
func configureLogger() { config := config.Get() logger.SetQuiet(config.Quiet) logger.SetColor(config.Color) logger.SetPrefix("gomon") }
func printInfo() { config := config.Get() logger.PrintInfo("target:", config.Target) logger.PrintInfo("extensions:", strings.Join(config.Extensions, ",")) logger.PrintInfo("directory:", config.Directory) if len(config.Arguments) > 0 { logger.PrintInfo("argumets:", config.Arguments) } }
func build() string { os.Remove(".temp") config := config.Get() var fileName string file, err := ioutil.TempFile("", "gomon_") if err != nil { fileName = "./.temp" } else { fileName = file.Name() } command("go", []string{"build", "-o", fileName, config.Target}).Run() return fileName }