Skip to content

Commit

Permalink
feat: enable commands to specify output writer
Browse files Browse the repository at this point in the history
  • Loading branch information
Ric Featherstone authored and 06kellyjac committed Dec 21, 2023
1 parent 85bc36e commit e23118f
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions controlplane/commands/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commands
import (
"context"
"fmt"
"io"
"log/slog"
"os"
"os/exec"
Expand All @@ -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 {
Expand All @@ -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()
Expand Down

0 comments on commit e23118f

Please sign in to comment.