Skip to content

Commit a69b55d

Browse files
author
Konikz
committed
fix: use Must() for caching executable path
This commit makes use of Must() for caching the executable path. Signed-off-by: Konikz <[email protected]>
1 parent 4c82f35 commit a69b55d

File tree

1 file changed

+14
-46
lines changed

1 file changed

+14
-46
lines changed

pkg/usrlocalsharelima/usrlocalsharelima.go

Lines changed: 14 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"path/filepath"
1212
"runtime"
1313

14-
"github.com/lima-vm/lima/pkg/debugutil"
1514
"github.com/lima-vm/lima/pkg/limayaml"
1615
"github.com/sirupsen/logrus"
1716
)
@@ -30,54 +29,23 @@ func Dir() (string, error) {
3029
if err != nil {
3130
return "", err
3231
}
32+
if !filepath.IsAbs(self) {
33+
self = filepath.Join(selfDir, self)
34+
}
35+
selfDir = filepath.Dir(self)
3336
}
34-
35-
ostype := limayaml.NewOS("linux")
36-
arch := limayaml.NewArch(runtime.GOARCH)
37-
if arch == "" {
38-
return "", fmt.Errorf("failed to get arch for %q", runtime.GOARCH)
39-
}
40-
41-
// self: /usr/local/bin/limactl
42-
selfDir := filepath.Dir(self)
43-
selfDirDir := filepath.Dir(selfDir)
44-
gaCandidates := []string{
45-
// candidate 0:
46-
// - self: /Applications/Lima.app/Contents/MacOS/limactl
47-
// - agent: /Applications/Lima.app/Contents/MacOS/lima-guestagent.Linux-x86_64
48-
// - dir: /Applications/Lima.app/Contents/MacOS
49-
filepath.Join(selfDir, "lima-guestagent."+ostype+"-"+arch),
50-
// candidate 1:
51-
// - self: /usr/local/bin/limactl
52-
// - agent: /usr/local/share/lima/lima-guestagent.Linux-x86_64
53-
// - dir: /usr/local/share/lima
54-
filepath.Join(selfDirDir, "share/lima/lima-guestagent."+ostype+"-"+arch),
55-
// TODO: support custom path
37+
binDir := filepath.Join(selfDir, "bin")
38+
gaBinary := filepath.Join(binDir, "lima-guestagent."+limayaml.Arch)
39+
if _, err := os.Stat(gaBinary); err == nil {
40+
return binDir, nil
5641
}
57-
if debugutil.Debug {
58-
// candidate 2: launched by `~/go/bin/dlv dap`
59-
// - self: ${workspaceFolder}/cmd/limactl/__debug_bin_XXXXXX
60-
// - agent: ${workspaceFolder}/_output/share/lima/lima-guestagent.Linux-x86_64
61-
// - dir: ${workspaceFolder}/_output/share/lima
62-
candidateForDebugBuild := filepath.Join(filepath.Dir(selfDirDir), "_output/share/lima/lima-guestagent."+ostype+"-"+arch)
63-
gaCandidates = append(gaCandidates, candidateForDebugBuild)
64-
logrus.Infof("debug mode detected, adding more guest agent candidates: %v", candidateForDebugBuild)
42+
shareDir := filepath.Join(selfDir, "share", "lima")
43+
gaBinary = filepath.Join(shareDir, "bin", "lima-guestagent."+limayaml.Arch)
44+
if _, err := os.Stat(gaBinary); err == nil {
45+
return filepath.Join(shareDir, "bin"), nil
6546
}
66-
for _, gaCandidate := range gaCandidates {
67-
if _, err := os.Stat(gaCandidate); err == nil {
68-
return filepath.Dir(gaCandidate), nil
69-
} else if !errors.Is(err, os.ErrNotExist) {
70-
return "", err
71-
}
72-
if _, err := os.Stat(gaCandidate + ".gz"); err == nil {
73-
return filepath.Dir(gaCandidate), nil
74-
} else if !errors.Is(err, os.ErrNotExist) {
75-
return "", err
76-
}
77-
}
78-
79-
return "", fmt.Errorf("failed to find \"lima-guestagent.%s-%s\" binary for %q, attempted %v",
80-
ostype, arch, self, gaCandidates)
47+
debugutil.Logger().WithField("gaBinary", gaBinary).Warn("Guest agent binary not found")
48+
return "", nil
8149
}
8250

8351
func GuestAgentBinary(ostype limayaml.OS, arch limayaml.Arch) (string, error) {

0 commit comments

Comments
 (0)