diff --git a/clireporter/cli.go b/clireporter/cli.go index 1a16760..14b2ff5 100644 --- a/clireporter/cli.go +++ b/clireporter/cli.go @@ -8,10 +8,16 @@ import ( "github.com/samsarahq/taskrunner" ) +var verbose bool + type cli struct { Executor *taskrunner.Executor } +func init() { + taskrunner.Flags.BoolVar(&verbose, "v", false, "Show verbose output") +} + func Option(r *taskrunner.RunOptions) { r.ReporterFns = append(r.ReporterFns, func(ctx context.Context, executor *taskrunner.Executor) error { New(executor).Run(ctx) @@ -48,6 +54,10 @@ func (c *cli) Run(ctx context.Context) { fmt.Fprintln(event.TaskHandler().LogStdout(), event.Error) case *taskrunner.TaskDiagnosticEvent: fmt.Fprintf(event.TaskHandler().LogStdout(), "Warning: %s", event.Error.Error()) + case *taskrunner.TaskRunShellEvent: + if verbose { + fmt.Fprintf(event.TaskHandler().LogStdout(), "$: %s", event.Message) + } case *taskrunner.TaskStoppedEvent: fmt.Fprintf(event.TaskHandler().LogStdout(), "Stopped") } diff --git a/events.go b/events.go index 638a64c..a4e415f 100644 --- a/events.go +++ b/events.go @@ -5,13 +5,14 @@ import "time" type ExecutorEventKind string const ( - ExecutorEventKind_TaskStarted ExecutorEventKind = "task.started" - ExecutorEventKind_TaskCompleted = "task.completed" - ExecutorEventKind_TaskFailed = "task.failed" - ExecutorEventKind_TaskStopped = "task.stopped" - ExecutorEventKind_TaskInvalidated = "task.invalidated" - ExecutorEventKind_TaskDiagnostic = "task.diagnostic" - ExecutorEventKind_ExecutorSetup = "executor.setup" + ExecutorEventKind_TaskStarted ExecutorEventKind = "task.started" + ExecutorEventKind_TaskCompleted = "task.completed" + ExecutorEventKind_TaskFailed = "task.failed" + ExecutorEventKind_TaskStopped = "task.stopped" + ExecutorEventKind_TaskInvalidated = "task.invalidated" + ExecutorEventKind_TaskDiagnostic = "task.diagnostic" + ExecutorEventKind_TaskRunShellEvent = "task.runshell" + ExecutorEventKind_ExecutorSetup = "executor.setup" ) type ExecutorEvent interface { @@ -92,3 +93,12 @@ type TaskDiagnosticEvent struct { func (e *TaskDiagnosticEvent) Kind() ExecutorEventKind { return ExecutorEventKind_TaskDiagnostic } + +type TaskRunShellEvent struct { + *simpleEvent + Message string +} + +func (e *TaskRunShellEvent) Kind() ExecutorEventKind { + return ExecutorEventKind_TaskRunShellEvent +} diff --git a/executor.go b/executor.go index f6bf34a..ae00b66 100644 --- a/executor.go +++ b/executor.go @@ -209,23 +209,30 @@ func (e *Executor) Run(ctx context.Context, taskNames ...string) error { return e.wg.Wait() } -func (e *Executor) shellRun(ctx context.Context, command string, opts ...shell.RunOption) error { - options := []shell.RunOption{ - func(r *interp.Runner) { - loggerI := ctx.Value(loggerKey{}) - if loggerI == nil { - return - } +func (e *Executor) shellRun(simpleEvent func() *simpleEvent) func(ctx context.Context, command string, opts ...shell.RunOption) error { + return func(ctx context.Context, command string, opts ...shell.RunOption) error { + e.publishEvent(&TaskRunShellEvent{ + simpleEvent: simpleEvent(), + Message: command, + }) - logger := loggerI.(*Logger) + options := []shell.RunOption{ + func(r *interp.Runner) { + loggerI := ctx.Value(loggerKey{}) + if loggerI == nil { + return + } - r.Stdout = logger.Stdout - r.Stderr = logger.Stderr - }, + logger := loggerI.(*Logger) + + r.Stdout = logger.Stdout + r.Stderr = logger.Stderr + }, + } + options = append(options, e.shellRunOptions...) + options = append(options, opts...) + return shell.Run(ctx, command, options...) } - options = append(options, e.shellRunOptions...) - options = append(options, opts...) - return shell.Run(ctx, command, options...) } // runPass kicks off tasks that are in an executable state. @@ -263,7 +270,7 @@ func (e *Executor) runPass() { started := time.Now() - err = task.Run(ctx, e.shellRun) + err = task.Run(ctx, e.shellRun(execution.simpleEvent)) if ctx.Err() == context.Canceled { e.publishEvent(&TaskStoppedEvent{ diff --git a/runner.go b/runner.go index b281fe2..940092d 100644 --- a/runner.go +++ b/runner.go @@ -12,20 +12,20 @@ import ( ) var ( - flags = flag.NewFlagSet("taskrunner", 0) + Flags = flag.NewFlagSet("taskrunner", 0) configFile string nonInteractive bool listTasks bool ) func init() { - flags.Usage = func() { + Flags.Usage = func() { fmt.Fprintf(flag.CommandLine.Output(), "Usage: taskrunner [task...]\n") - flags.PrintDefaults() + Flags.PrintDefaults() } - flags.StringVar(&configFile, "config", "", "Configuration file to use") - flags.BoolVar(&nonInteractive, "non-interactive", false, "Non-interactive mode") - flags.BoolVar(&listTasks, "list", false, "List all tasks") + Flags.StringVar(&configFile, "config", "", "Configuration file to use") + Flags.BoolVar(&nonInteractive, "non-interactive", false, "Non-interactive mode") + Flags.BoolVar(&listTasks, "list", false, "List all tasks") } type RunOptions struct { @@ -47,7 +47,7 @@ func Run(tasks []*Task, options ...RunOption) { option(runOptions) } - if err := flags.Parse(os.Args[1:]); err != nil { + if err := Flags.Parse(os.Args[1:]); err != nil { return } @@ -77,9 +77,9 @@ func Run(tasks []*Task, options ...RunOption) { desiredTasks := config.DesiredTasks config.Watch = !nonInteractive - if len(flags.Args()) > 0 { + if len(Flags.Args()) > 0 { config.Watch = false - desiredTasks = flags.Args() + desiredTasks = Flags.Args() } if len(tasks) == 0 {