Ejemplo n.º 1
1
func fromFile() *configurationTemp {
	data, err := ioutil.ReadFile("gomon.json")
	if err != nil {
		return nil
	}

	config := &configurationTemp{}
	err = json.Unmarshal(data, config)
	if err != nil {
		logger.PrintError("gomon.json:", err)
		return nil
	}

	if config.DelayStr != nil {
		if d, err := time.ParseDuration(*config.DelayStr); err != nil {
			logger.PrintError("gomon.json duration:", err)
			return nil
		} else {
			config.Delay = &d
		}
	}

	return config
}
Ejemplo n.º 2
1
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...")
		})

	}
}