Skip to content

Commit 23448c7

Browse files
committed
cmd: add 'context.Context' to CommandExecutor
Continue the work started in e8713a4 (git-bundle-server: propagate 'context.Context' through call stack, 2023-02-21) by adding 'ctx context.Context' to the 'Run()' method in the 'CommandExecutor'. When logging is added to these methods in later commits, the context will be used to call the appropriate log functions. Signed-off-by: Victoria Dye <[email protected]>
1 parent e1c42ef commit 23448c7

File tree

6 files changed

+30
-12
lines changed

6 files changed

+30
-12
lines changed

internal/cmd/command.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package cmd
22

33
import (
4+
"context"
45
"fmt"
56
"os/exec"
67
)
78

89
type CommandExecutor interface {
9-
Run(command string, args ...string) (int, error)
10+
Run(ctx context.Context, command string, args ...string) (int, error)
1011
}
1112

1213
type commandExecutor struct{}
@@ -15,7 +16,7 @@ func NewCommandExecutor() CommandExecutor {
1516
return &commandExecutor{}
1617
}
1718

18-
func (c *commandExecutor) Run(command string, args ...string) (int, error) {
19+
func (c *commandExecutor) Run(ctx context.Context, command string, args ...string) (int, error) {
1920
exe, err := exec.LookPath(command)
2021
if err != nil {
2122
return -1, fmt.Errorf("failed to find '%s' on the path: %w", command, err)

internal/daemon/launchd.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func NewLaunchdProvider(
115115

116116
func (l *launchd) isBootstrapped(ctx context.Context, serviceTarget string) (bool, error) {
117117
// run 'launchctl print' on given service target to see if it exists
118-
exitCode, err := l.cmdExec.Run("launchctl", "print", serviceTarget)
118+
exitCode, err := l.cmdExec.Run(ctx, "launchctl", "print", serviceTarget)
119119
if err != nil {
120120
return false, l.logger.Error(ctx, err)
121121
}
@@ -132,7 +132,7 @@ func (l *launchd) isBootstrapped(ctx context.Context, serviceTarget string) (boo
132132

133133
func (l *launchd) bootstrapFile(ctx context.Context, domain string, filename string) error {
134134
// run 'launchctl bootstrap' on given domain & file
135-
exitCode, err := l.cmdExec.Run("launchctl", "bootstrap", domain, filename)
135+
exitCode, err := l.cmdExec.Run(ctx, "launchctl", "bootstrap", domain, filename)
136136
if err != nil {
137137
return l.logger.Error(ctx, err)
138138
}
@@ -146,7 +146,7 @@ func (l *launchd) bootstrapFile(ctx context.Context, domain string, filename str
146146

147147
func (l *launchd) bootout(ctx context.Context, serviceTarget string) (bool, error) {
148148
// run 'launchctl bootout' on given service target
149-
exitCode, err := l.cmdExec.Run("launchctl", "bootout", serviceTarget)
149+
exitCode, err := l.cmdExec.Run(ctx, "launchctl", "bootout", serviceTarget)
150150
if err != nil {
151151
return false, l.logger.Error(ctx, err)
152152
}
@@ -239,7 +239,7 @@ func (l *launchd) Start(ctx context.Context, label string) error {
239239

240240
domainTarget := fmt.Sprintf(domainFormat, user.Uid)
241241
serviceTarget := fmt.Sprintf("%s/%s", domainTarget, label)
242-
exitCode, err := l.cmdExec.Run("launchctl", "kickstart", serviceTarget)
242+
exitCode, err := l.cmdExec.Run(ctx, "launchctl", "kickstart", serviceTarget)
243243
if err != nil {
244244
return l.logger.Error(ctx, err)
245245
}
@@ -259,7 +259,7 @@ func (l *launchd) Stop(ctx context.Context, label string) error {
259259

260260
domainTarget := fmt.Sprintf(domainFormat, user.Uid)
261261
serviceTarget := fmt.Sprintf("%s/%s", domainTarget, label)
262-
exitCode, err := l.cmdExec.Run("launchctl", "kill", "SIGINT", serviceTarget)
262+
exitCode, err := l.cmdExec.Run(ctx, "launchctl", "kill", "SIGINT", serviceTarget)
263263
if err != nil {
264264
return l.logger.Error(ctx, err)
265265
}

internal/daemon/launchd_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,18 +251,21 @@ func TestLaunchd_Create(t *testing.T) {
251251
// Mock responses
252252
for _, retVal := range tt.launchctlPrint {
253253
testCommandExecutor.On("Run",
254+
ctx,
254255
"launchctl",
255256
mock.MatchedBy(func(args []string) bool { return args[0] == "print" }),
256257
).Return(retVal.First, retVal.Second).Once()
257258
}
258259
for _, retVal := range tt.launchctlBootstrap {
259260
testCommandExecutor.On("Run",
261+
ctx,
260262
"launchctl",
261263
mock.MatchedBy(func(args []string) bool { return args[0] == "bootstrap" }),
262264
).Return(retVal.First, retVal.Second).Once()
263265
}
264266
for _, retVal := range tt.launchctlBootout {
265267
testCommandExecutor.On("Run",
268+
ctx,
266269
"launchctl",
267270
mock.MatchedBy(func(args []string) bool { return args[0] == "bootout" }),
268271
).Return(retVal.First, retVal.Second).Once()
@@ -305,10 +308,12 @@ func TestLaunchd_Create(t *testing.T) {
305308

306309
// Mock responses for successful fresh write
307310
testCommandExecutor.On("Run",
311+
ctx,
308312
"launchctl",
309313
mock.MatchedBy(func(args []string) bool { return args[0] == "print" }),
310314
).Return(daemon.LaunchdServiceNotFoundErrorCode, nil).Once()
311315
testCommandExecutor.On("Run",
316+
ctx,
312317
"launchctl",
313318
mock.MatchedBy(func(args []string) bool { return args[0] == "bootstrap" }),
314319
).Return(0, nil).Once()
@@ -374,6 +379,7 @@ func TestLaunchd_Start(t *testing.T) {
374379
// Test #1: launchctl succeeds
375380
t.Run("Calls correct launchctl command", func(t *testing.T) {
376381
testCommandExecutor.On("Run",
382+
ctx,
377383
"launchctl",
378384
[]string{"kickstart", fmt.Sprintf("user/123/%s", basicDaemonConfig.Label)},
379385
).Return(0, nil).Once()
@@ -389,6 +395,7 @@ func TestLaunchd_Start(t *testing.T) {
389395
// Test #2: launchctl fails
390396
t.Run("Returns error when launchctl fails", func(t *testing.T) {
391397
testCommandExecutor.On("Run",
398+
ctx,
392399
mock.AnythingOfType("string"),
393400
mock.AnythingOfType("[]string"),
394401
).Return(1, nil).Once()
@@ -458,6 +465,7 @@ func TestLaunchd_Stop(t *testing.T) {
458465
// Mock responses
459466
if tt.launchctlKill != nil {
460467
testCommandExecutor.On("Run",
468+
ctx,
461469
"launchctl",
462470
[]string{"kill", "SIGINT", fmt.Sprintf("user/123/%s", tt.label)},
463471
).Return(tt.launchctlKill.First, tt.launchctlKill.Second).Once()
@@ -554,6 +562,7 @@ func TestLaunchd_Remove(t *testing.T) {
554562
// Mock responses
555563
if tt.launchctlBootout != nil {
556564
testCommandExecutor.On("Run",
565+
ctx,
557566
"launchctl",
558567
[]string{"bootout", fmt.Sprintf("user/123/%s", tt.label)},
559568
).Return(tt.launchctlBootout.First, tt.launchctlBootout.Second).Once()

internal/daemon/systemd.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func NewSystemdProvider(
4545
}
4646

4747
func (s *systemd) reloadDaemon(ctx context.Context) error {
48-
exitCode, err := s.cmdExec.Run("systemctl", "--user", "daemon-reload")
48+
exitCode, err := s.cmdExec.Run(ctx, "systemctl", "--user", "daemon-reload")
4949
if err != nil {
5050
return s.logger.Error(ctx, err)
5151
}
@@ -105,7 +105,7 @@ func (s *systemd) Create(ctx context.Context, config *DaemonConfig, force bool)
105105

106106
func (s *systemd) Start(ctx context.Context, label string) error {
107107
// TODO: warn user if already running
108-
exitCode, err := s.cmdExec.Run("systemctl", "--user", "start", label)
108+
exitCode, err := s.cmdExec.Run(ctx, "systemctl", "--user", "start", label)
109109
if err != nil {
110110
return s.logger.Error(ctx, err)
111111
}
@@ -119,7 +119,7 @@ func (s *systemd) Start(ctx context.Context, label string) error {
119119

120120
func (s *systemd) Stop(ctx context.Context, label string) error {
121121
// TODO: warn user if already stopped
122-
exitCode, err := s.cmdExec.Run("systemctl", "--user", "stop", label)
122+
exitCode, err := s.cmdExec.Run(ctx, "systemctl", "--user", "stop", label)
123123
if err != nil {
124124
return s.logger.Error(ctx, err)
125125
}

internal/daemon/systemd_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ func TestSystemd_Create(t *testing.T) {
161161
}
162162
for _, retVal := range tt.systemctlDaemonReload {
163163
testCommandExecutor.On("Run",
164+
ctx,
164165
"systemctl",
165166
[]string{"--user", "daemon-reload"},
166167
).Return(retVal.First, retVal.Second).Once()
@@ -192,6 +193,7 @@ func TestSystemd_Create(t *testing.T) {
192193

193194
// Mock responses for successful fresh write
194195
testCommandExecutor.On("Run",
196+
ctx,
195197
"systemctl",
196198
[]string{"--user", "daemon-reload"},
197199
).Return(0, nil).Once()
@@ -255,6 +257,7 @@ func TestSystemd_Start(t *testing.T) {
255257
// Test #1: systemctl succeeds
256258
t.Run("Calls correct systemctl command", func(t *testing.T) {
257259
testCommandExecutor.On("Run",
260+
ctx,
258261
"systemctl",
259262
[]string{"--user", "start", basicDaemonConfig.Label},
260263
).Return(0, nil).Once()
@@ -270,6 +273,7 @@ func TestSystemd_Start(t *testing.T) {
270273
// Test #2: systemctl fails
271274
t.Run("Returns error when systemctl fails", func(t *testing.T) {
272275
testCommandExecutor.On("Run",
276+
ctx,
273277
mock.AnythingOfType("string"),
274278
mock.AnythingOfType("[]string"),
275279
).Return(1, nil).Once()
@@ -300,6 +304,7 @@ func TestSystemd_Stop(t *testing.T) {
300304
// Test #1: systemctl succeeds
301305
t.Run("Calls correct systemctl command", func(t *testing.T) {
302306
testCommandExecutor.On("Run",
307+
ctx,
303308
"systemctl",
304309
[]string{"--user", "stop", basicDaemonConfig.Label},
305310
).Return(0, nil).Once()
@@ -315,6 +320,7 @@ func TestSystemd_Stop(t *testing.T) {
315320
// Test #2: systemctl fails with uncaught error
316321
t.Run("Returns error when systemctl fails", func(t *testing.T) {
317322
testCommandExecutor.On("Run",
323+
ctx,
318324
mock.AnythingOfType("string"),
319325
mock.AnythingOfType("[]string"),
320326
).Return(1, nil).Once()
@@ -330,6 +336,7 @@ func TestSystemd_Stop(t *testing.T) {
330336
// Test #3: service unit not found still succeeds
331337
t.Run("Succeeds if service unit not installed", func(t *testing.T) {
332338
testCommandExecutor.On("Run",
339+
ctx,
333340
mock.AnythingOfType("string"),
334341
mock.AnythingOfType("[]string"),
335342
).Return(daemon.SystemdUnitNotInstalledErrorCode, nil).Once()
@@ -407,6 +414,7 @@ func TestSystemd_Remove(t *testing.T) {
407414
}
408415
if tt.systemctlDaemonReload != nil {
409416
testCommandExecutor.On("Run",
417+
ctx,
410418
"systemctl",
411419
[]string{"--user", "daemon-reload"},
412420
).Return(tt.systemctlDaemonReload.First, tt.systemctlDaemonReload.Second).Once()

internal/testhelpers/mocks.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ type MockCommandExecutor struct {
121121
mock.Mock
122122
}
123123

124-
func (m *MockCommandExecutor) Run(command string, args ...string) (int, error) {
125-
fnArgs := m.Called(command, args)
124+
func (m *MockCommandExecutor) Run(ctx context.Context, command string, args ...string) (int, error) {
125+
fnArgs := m.Called(ctx, command, args)
126126
return fnArgs.Int(0), fnArgs.Error(1)
127127
}
128128

0 commit comments

Comments
 (0)