feedback from PR on --watch

This commit is contained in:
Casey Lee 2019-02-12 23:32:54 -05:00
parent e27d5fa3af
commit 2e52ef61c8
No known key found for this signature in database
GPG key ID: 1899120ECD0A1784

View file

@ -56,21 +56,16 @@ func newRunCommand(runnerConfig *actions.RunnerConfig) func(*cobra.Command, []st
runnerConfig.EventName = args[0] runnerConfig.EventName = args[0]
} }
err := parseAndRun(cmd, runnerConfig)
if err != nil {
return err
}
watch, err := cmd.Flags().GetBool("watch") watch, err := cmd.Flags().GetBool("watch")
if err != nil { if err != nil {
return err return err
} }
if watch { if watch {
return watchAndRun(runnerConfig.Ctx, func() { return watchAndRun(runnerConfig.Ctx, func() error {
err = parseAndRun(cmd, runnerConfig) return parseAndRun(cmd, runnerConfig)
}) })
} }
return parseAndRun(cmd, runnerConfig)
return err
} }
} }
@ -104,7 +99,7 @@ func parseAndRun(cmd *cobra.Command, runnerConfig *actions.RunnerConfig) error {
return runner.RunEvent() return runner.RunEvent()
} }
func watchAndRun(ctx context.Context, fn func()) error { func watchAndRun(ctx context.Context, fn func() error) error {
recurse := true recurse := true
checkIntervalInSeconds := 2 checkIntervalInSeconds := 2
dir, err := os.Getwd() dir, err := os.Getwd()
@ -127,20 +122,25 @@ func watchAndRun(ctx context.Context, fn func()) error {
) )
folderWatcher.Start() folderWatcher.Start()
log.Debugf("Watching %s for changes", dir)
go func() { go func() {
for folderWatcher.IsRunning() { for folderWatcher.IsRunning() {
if err = fn(); err != nil {
break
}
log.Debugf("Watching %s for changes", dir)
for changes := range folderWatcher.ChangeDetails() { for changes := range folderWatcher.ChangeDetails() {
log.Debugf("%s", changes.String()) log.Debugf("%s", changes.String())
fn() if err = fn(); err != nil {
break
}
log.Debugf("Watching %s for changes", dir) log.Debugf("Watching %s for changes", dir)
} }
} }
}() }()
<-ctx.Done() <-ctx.Done()
folderWatcher.Stop() folderWatcher.Stop()
return nil return err
} }
func drawGraph(runner actions.Runner) error { func drawGraph(runner actions.Runner) error {