Skip to content

Commit 3bba87c

Browse files
committed
cmd: add logger to 'CommandExecutor'
Initialize the 'CommandExecutor' with an instance of 'log.TraceLogger', consistent with other structures (e.g. 'core.RepoProvider'). This will be used to log the setup and run operations of the executor in later commits. Signed-off-by: Victoria Dye <[email protected]>
1 parent 23448c7 commit 3bba87c

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

cmd/utils/container-helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func BuildGitBundleServerContainer(logger log.TraceLogger) *DependencyContainer
1717
return common.NewUserProvider()
1818
})
1919
registerDependency(container, func(ctx context.Context) cmd.CommandExecutor {
20-
return cmd.NewCommandExecutor()
20+
return cmd.NewCommandExecutor(logger)
2121
})
2222
registerDependency(container, func(ctx context.Context) common.FileSystem {
2323
return common.NewFileSystem()

internal/cmd/command.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,36 @@ package cmd
22

33
import (
44
"context"
5-
"fmt"
65
"os/exec"
6+
7+
"github.com/github/git-bundle-server/internal/log"
78
)
89

910
type CommandExecutor interface {
1011
Run(ctx context.Context, command string, args ...string) (int, error)
1112
}
1213

13-
type commandExecutor struct{}
14+
type commandExecutor struct {
15+
logger log.TraceLogger
16+
}
1417

15-
func NewCommandExecutor() CommandExecutor {
16-
return &commandExecutor{}
18+
func NewCommandExecutor(l log.TraceLogger) CommandExecutor {
19+
return &commandExecutor{
20+
logger: l,
21+
}
1722
}
1823

1924
func (c *commandExecutor) Run(ctx context.Context, command string, args ...string) (int, error) {
2025
exe, err := exec.LookPath(command)
2126
if err != nil {
22-
return -1, fmt.Errorf("failed to find '%s' on the path: %w", command, err)
27+
return -1, c.logger.Errorf(ctx, "failed to find '%s' on the path: %w", command, err)
2328
}
2429

2530
cmd := exec.Command(exe, args...)
2631

2732
err = cmd.Start()
2833
if err != nil {
29-
return -1, fmt.Errorf("command failed to start: %w", err)
34+
return -1, c.logger.Errorf(ctx, "command failed to start: %w", err)
3035
}
3136

3237
err = cmd.Wait()
@@ -37,6 +42,6 @@ func (c *commandExecutor) Run(ctx context.Context, command string, args ...strin
3742
if err == nil || isExitError {
3843
return cmd.ProcessState.ExitCode(), nil
3944
} else {
40-
return -1, err
45+
return -1, c.logger.Error(ctx, err)
4146
}
4247
}

0 commit comments

Comments
 (0)