From d6a01c4ee22a5546fd5b462c4f659720c4c458a5 Mon Sep 17 00:00:00 2001 From: Sam Hoffman Date: Sat, 31 Jan 2026 00:29:33 -0500 Subject: [PATCH] runner: pipe commands together --- internal/runner/runner.go | 31 +++++++++++++++++++++++++++++++ internal/runner/runner_test.go | 11 +++++++++++ 2 files changed, 42 insertions(+) diff --git a/internal/runner/runner.go b/internal/runner/runner.go index 91e1f03..63ea8f0 100644 --- a/internal/runner/runner.go +++ b/internal/runner/runner.go @@ -1,6 +1,7 @@ package runner import ( + "bytes" "context" "fmt" "os/exec" @@ -26,3 +27,33 @@ func (runner *ZRunner) Run(ctx context.Context, command string, formatArgs ...an cmd := exec.Command(runner.Prog, args...) return cmd.CombinedOutput() } + +func Pipeline(cmds ...*exec.Cmd) ([]byte, error) { + for i := 0; i < len(cmds)-1; i++ { + stdout, err := cmds[i].StdoutPipe() + if err != nil { + return nil, err + } + cmds[i+1].Stdin = stdout + } + + out := bytes.Buffer{} + + cmds[len(cmds)-1].Stdout = &out + + for _, cmd := range cmds { + err := cmd.Start() + if err != nil { + return out.Bytes(), err + } + } + + for _, cmd := range cmds { + err := cmd.Wait() + if err != nil { + return out.Bytes(), err + } + } + + return out.Bytes(), nil +} diff --git a/internal/runner/runner_test.go b/internal/runner/runner_test.go index a46f4ad..68fd523 100644 --- a/internal/runner/runner_test.go +++ b/internal/runner/runner_test.go @@ -2,9 +2,11 @@ package runner_test import ( "context" + "os/exec" "testing" "git.gentoo.party/sam/thanks/internal/runner" + "github.com/stretchr/testify/assert" ) func Test_ZCommand(t *testing.T) { @@ -19,3 +21,12 @@ func Test_ZCommand(t *testing.T) { t.Errorf("localRunner failed: %s\n\n%s", err.Error(), out) } } + +func Test_Pipelie(t *testing.T) { + c1 := exec.Command("echo", "-n", "foo") + c2 := exec.Command("rev") + out, err := runner.Pipeline(c1, c2) + + assert.Equal(t, "oof", string(out)) + assert.Nil(t, err) +}