From e23118fdadb3cd6046e39658e35bc324ef939f79 Mon Sep 17 00:00:00 2001 From: Ric Featherstone Date: Thu, 9 Nov 2023 14:49:16 +0000 Subject: [PATCH] feat: enable commands to specify output writer --- controlplane/commands/runner.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/controlplane/commands/runner.go b/controlplane/commands/runner.go index 732fd325..b936649a 100644 --- a/controlplane/commands/runner.go +++ b/controlplane/commands/runner.go @@ -3,6 +3,7 @@ package commands import ( "context" "fmt" + "io" "log/slog" "os" "os/exec" @@ -12,7 +13,7 @@ import ( type Executable string type Runnable interface { - Run(ctx context.Context) error + Run(ctx context.Context, output ...io.Writer) error } type command struct { @@ -22,14 +23,22 @@ type command struct { Arguments []string } -func (c command) Run(ctx context.Context) error { +func (c command) Run(ctx context.Context, output ...io.Writer) error { slog.Info("running", "command", c) + // Default to writing to stdout unless an alternative writer is provided + var writer io.Writer + if len(output) == 0 { + writer = os.Stdout + } else { + writer = output[0] + } + cmd := exec.CommandContext(ctx, string(c.Executable), c.Arguments...) cmd.Dir = c.WorkingDir cmd.Env = c.Environment - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr + cmd.Stdout = writer + cmd.Stderr = writer // TODO: Ensure ctrl-c stops the command err := cmd.Run()