Skip to content

Commit 2bca6c7

Browse files
committed
filesystem: add 'GetLocalExecutable()'
Add a function 'GetLocalExecutable()' to 'FileSystem'. This function, given the identifier of one of the executables installed with this package ('git-bundle-server' or 'git-bundle-web-server'), will look for the file in the same directory as the current executing program, and return its absolute path. This function is then used to replace the custom logic in 'getDaemonConfig()', which locates the 'git-bundle-web-server' executable to run in a daemon process. This is a functional change; previously, 'getDaemonConfig()' would first lookup the executable with 'exec.LookPath()' before falling back on the local directory. Removing the 'exec.LookPath()' seems like the right thing to do here - if we're running a local version of 'git-bundle-server', we'd want to find the executable corresponding to that local build. Signed-off-by: Victoria Dye <[email protected]>
1 parent 830bcfa commit 2bca6c7

File tree

3 files changed

+32
-31
lines changed

3 files changed

+32
-31
lines changed

cmd/git-bundle-server/web-server.go

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@ package main
22

33
import (
44
"context"
5-
"errors"
65
"flag"
76
"fmt"
8-
"os"
9-
"os/exec"
107
"path/filepath"
118

129
"github.com/github/git-bundle-server/cmd/utils"
@@ -38,35 +35,10 @@ func (webServerCmd) Description() string {
3835

3936
func (w *webServerCmd) getDaemonConfig(ctx context.Context) (*daemon.DaemonConfig, error) {
4037
// Find git-bundle-web-server
41-
// First, search for it on the path
42-
programPath, err := exec.LookPath("git-bundle-web-server")
38+
fileSystem := utils.GetDependency[common.FileSystem](ctx, w.container)
39+
programPath, err := fileSystem.GetLocalExecutable("git-bundle-web-server")
4340
if err != nil {
44-
if errors.Is(err, exec.ErrDot) {
45-
// Result is a relative path
46-
programPath, err = filepath.Abs(programPath)
47-
if err != nil {
48-
return nil, w.logger.Errorf(ctx, "could not get absolute path to program: %w", err)
49-
}
50-
} else {
51-
// Fall back on looking for it in the same directory as the currently-running executable
52-
exePath, err := os.Executable()
53-
if err != nil {
54-
return nil, w.logger.Errorf(ctx, "failed to get path to current executable: %w", err)
55-
}
56-
exeDir := filepath.Dir(exePath)
57-
if err != nil {
58-
return nil, w.logger.Errorf(ctx, "failed to get parent dir of current executable: %w", err)
59-
}
60-
61-
programPath = filepath.Join(exeDir, "git-bundle-web-server")
62-
fileSystem := utils.GetDependency[common.FileSystem](ctx, w.container)
63-
programExists, err := fileSystem.FileExists(programPath)
64-
if err != nil {
65-
return nil, w.logger.Errorf(ctx, "could not determine whether path to 'git-bundle-web-server' exists: %w", err)
66-
} else if !programExists {
67-
return nil, w.logger.Errorf(ctx, "could not find path to 'git-bundle-web-server'")
68-
}
69-
}
41+
return nil, w.logger.Error(ctx, err)
7042
}
7143

7244
return &daemon.DaemonConfig{

internal/common/filesystem.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ import (
66
"fmt"
77
"os"
88
"path"
9+
"path/filepath"
910
"syscall"
1011
)
1112

1213
type FileSystem interface {
14+
GetLocalExecutable(name string) (string, error)
15+
1316
FileExists(filename string) (bool, error)
1417
WriteFile(filename string, content []byte) error
1518
DeleteFile(filename string) (bool, error)
@@ -22,6 +25,27 @@ func NewFileSystem() FileSystem {
2225
return &fileSystem{}
2326
}
2427

28+
func (f *fileSystem) GetLocalExecutable(name string) (string, error) {
29+
thisExePath, err := os.Executable()
30+
if err != nil {
31+
return "", fmt.Errorf("failed to get path to current executable: %w", err)
32+
}
33+
exeDir := filepath.Dir(thisExePath)
34+
if err != nil {
35+
return "", fmt.Errorf("failed to get parent dir of current executable: %w", err)
36+
}
37+
38+
programPath := filepath.Join(exeDir, name)
39+
programExists, err := f.FileExists(programPath)
40+
if err != nil {
41+
return "", fmt.Errorf("could not determine whether path to '%s' exists: %w", name, err)
42+
} else if !programExists {
43+
return "", fmt.Errorf("could not find path to '%s'", name)
44+
}
45+
46+
return programPath, nil
47+
}
48+
2549
func (f *fileSystem) FileExists(filename string) (bool, error) {
2650
_, err := os.Stat(filename)
2751
if err == nil {

internal/testhelpers/mocks.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ type MockFileSystem struct {
150150
mock.Mock
151151
}
152152

153+
func (m *MockFileSystem) GetLocalExecutable(name string) (string, error) {
154+
fnArgs := m.Called(name)
155+
return fnArgs.String(0), fnArgs.Error(1)
156+
}
157+
153158
func (m *MockFileSystem) FileExists(filename string) (bool, error) {
154159
fnArgs := m.Called(filename)
155160
return fnArgs.Bool(0), fnArgs.Error(1)

0 commit comments

Comments
 (0)