Skip to content

Commit 86b7a57

Browse files
committed
Add log passthrough for lock-exec
Creates multiwriter with a single buffer (combined output) and std IO file descriptors
1 parent 507ad74 commit 86b7a57

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

cmd/run.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ func (c *cli) newRunCmd() *cobra.Command {
2020
locker := c.newLocker()
2121

2222
log.Info("running command")
23-
out, err := locker.Run(c.cmd.Context(), args[0], args[1])
23+
err := locker.Run(c.cmd.Context(), args[0], args[1])
2424
if err != nil {
2525
if errors.Is(err, lock.ErrLocked) {
2626
log.Info("did not run command. key is locked")
2727
return
2828
}
2929

30-
log.Fatalw("command failed", "error", err, "output", out)
30+
log.Fatalw("command failed", "error", err)
3131
}
3232

33-
log.Infow("command succeeded", "output", out)
33+
log.Infow("command succeeded")
3434
},
3535
}
3636

lock/run.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package lock
33
import (
44
"context"
55
"fmt"
6+
"os"
67
"os/exec"
78
"strings"
89
"time"
@@ -11,17 +12,22 @@ import (
1112
// Run acquires a lock under the specified key, executes the command, and then unlocks the key.
1213
// Returns ErrLocked if the key is already locked. Otherwise returns combined stdout and stderr
1314
// of the command and the command error. If the unlock step fails the lock expires after 24 hours.
14-
func (c *Client) Run(ctx context.Context, key, command string) (string, error) {
15+
func (c *Client) Run(ctx context.Context, key, command string) error {
1516
// use context.Background here so that unlock runs even if the context is cancelled
1617
defer c.Unlock(context.Background(), key) //nolint:errcheck
1718

1819
err := c.Lock(ctx, key, time.Hour*24) //nolint:gomnd
1920
if err != nil {
20-
return "", fmt.Errorf("lock failed: %w", err)
21+
return fmt.Errorf("lock failed: %w", err)
2122
}
2223

24+
// Build command
2325
fields := strings.Fields(command)
24-
cmdout, cmderr := exec.CommandContext(ctx, fields[0], fields[1:]...).CombinedOutput() //nolint:gosec
26+
cmd := exec.CommandContext(ctx, fields[0], fields[1:]...) //nolint:gosec
2527

26-
return strings.TrimSpace(string(cmdout)), cmderr
28+
// Write to std outputs
29+
cmd.Stdout = os.Stdout
30+
cmd.Stderr = os.Stderr
31+
32+
return cmd.Run()
2733
}

lock/run_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,17 @@ func TestRun(t *testing.T) {
1818

1919
wg.Add(1)
2020
go func() {
21-
out, err := tc.Run(ctx, "locktest", "sleep 1")
22-
assert.Empty(t, out)
21+
err := tc.Run(ctx, "locktest", "sleep 1")
2322
assert.NoError(t, err)
2423
wg.Done()
2524
}()
2625
time.Sleep(time.Millisecond * 500)
2726

28-
out, err := tc.Run(ctx, "locktest", "sleep 5")
29-
assert.Empty(t, out)
27+
err := tc.Run(ctx, "locktest", "sleep 5")
3028
assert.ErrorIs(t, err, ErrLocked)
3129

32-
out, err = tc.Run(ctx, "locktest", "echo hello test")
30+
err = tc.Run(ctx, "locktest", "echo hello test")
3331
assert.NoError(t, err)
34-
assert.Equal(t, "hello test", out)
3532

3633
wg.Wait()
3734
}

0 commit comments

Comments
 (0)