Skip to content

Commit a8fdc7a

Browse files
committed
update-all: use common infrastructure
Update 'update-all.go' to use 'common.FileSystem' to get the path to the 'git-bundle-server' executbale, and use 'cmd.CommandExecutor' to call 'git-bundle-server update'. The use of the common structures eliminate some duplicate code as well as set up the command to be unit testable in the future. Signed-off-by: Victoria Dye <[email protected]>
1 parent ae0f7b4 commit a8fdc7a

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

cmd/git-bundle-server/update-all.go

+11-14
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package main
22

33
import (
44
"context"
5-
"os"
6-
"os/exec"
5+
"fmt"
76

87
"github.com/github/git-bundle-server/cmd/utils"
98
"github.com/github/git-bundle-server/internal/argparse"
9+
"github.com/github/git-bundle-server/internal/cmd"
10+
"github.com/github/git-bundle-server/internal/common"
1011
"github.com/github/git-bundle-server/internal/core"
1112
"github.com/github/git-bundle-server/internal/log"
1213
)
@@ -37,13 +38,15 @@ func (u *updateAllCmd) Run(ctx context.Context, args []string) error {
3738
parser.Parse(ctx, args)
3839

3940
repoProvider := utils.GetDependency[core.RepositoryProvider](ctx, u.container)
41+
fileSystem := utils.GetDependency[common.FileSystem](ctx, u.container)
42+
commandExecutor := utils.GetDependency[cmd.CommandExecutor](ctx, u.container)
4043

4144
repos, err := repoProvider.GetRepositories(ctx)
4245
if err != nil {
4346
return u.logger.Error(ctx, err)
4447
}
4548

46-
exe, err := os.Executable()
49+
exe, err := fileSystem.GetLocalExecutable("git-bundle-server")
4750
if err != nil {
4851
return u.logger.Errorf(ctx, "failed to get path to execuable: %w", err)
4952
}
@@ -53,19 +56,13 @@ func (u *updateAllCmd) Run(ctx context.Context, args []string) error {
5356

5457
for route := range repos {
5558
subargs[1] = route
56-
cmd := exec.Command(exe, subargs...)
57-
cmd.Stderr = os.Stderr
58-
cmd.Stdout = os.Stdout
59-
60-
err := cmd.Start()
61-
if err != nil {
62-
return u.logger.Errorf(ctx, "git command failed to start: %w", err)
63-
}
64-
65-
err = cmd.Wait()
59+
exitCode, err := commandExecutor.RunStdout(ctx, exe, subargs...)
6660
if err != nil {
67-
return u.logger.Errorf(ctx, "git command returned a failure: %w", err)
61+
return u.logger.Error(ctx, err)
62+
} else if exitCode != 0 {
63+
return u.logger.Errorf(ctx, "git-bundle-server update exited with status %d", exitCode)
6864
}
65+
fmt.Print("\n")
6966
}
7067

7168
return nil

0 commit comments

Comments
 (0)