Skip to content

Commit 13e5ad3

Browse files
author
Konikz
committed
fix: update usrlocalsharelima.Dir() to use Must()
This commit updates the Dir() function to use Must() for caching the executable path, as suggested in the review. Signed-off-by: Konikz <[email protected]>
1 parent acf4872 commit 13e5ad3

File tree

1 file changed

+18
-50
lines changed

1 file changed

+18
-50
lines changed

Diff for: pkg/usrlocalsharelima/usrlocalsharelima.go

+18-50
Original file line numberDiff line numberDiff line change
@@ -13,74 +13,42 @@ import (
1313

1414
"github.com/lima-vm/lima/pkg/debugutil"
1515
"github.com/lima-vm/lima/pkg/limayaml"
16+
. "github.com/lima-vm/lima/pkg/must"
1617
"github.com/sirupsen/logrus"
1718
)
1819

1920
// executable is a variable that can be overridden in tests.
2021
var executable = os.Executable
22+
var self = Must(os.Executable())
2123

2224
func Dir() (string, error) {
23-
self, err := executable()
24-
if err != nil {
25-
return "", err
26-
}
2725
selfSt, err := os.Stat(self)
2826
if err != nil {
2927
return "", err
3028
}
31-
if selfSt.Mode()&fs.ModeSymlink != 0 {
29+
selfDir := filepath.Dir(self)
30+
if selfSt.Mode()&os.ModeSymlink != 0 {
3231
self, err = os.Readlink(self)
3332
if err != nil {
3433
return "", err
3534
}
35+
if !filepath.IsAbs(self) {
36+
self = filepath.Join(selfDir, self)
37+
}
38+
selfDir = filepath.Dir(self)
3639
}
37-
38-
ostype := limayaml.NewOS("linux")
39-
arch := limayaml.NewArch(runtime.GOARCH)
40-
if arch == "" {
41-
return "", fmt.Errorf("failed to get arch for %q", runtime.GOARCH)
42-
}
43-
44-
// self: /usr/local/bin/limactl
45-
selfDir := filepath.Dir(self)
46-
selfDirDir := filepath.Dir(selfDir)
47-
gaCandidates := []string{
48-
// candidate 0:
49-
// - self: /Applications/Lima.app/Contents/MacOS/limactl
50-
// - agent: /Applications/Lima.app/Contents/MacOS/lima-guestagent.Linux-x86_64
51-
// - dir: /Applications/Lima.app/Contents/MacOS
52-
filepath.Join(selfDir, "lima-guestagent."+ostype+"-"+arch),
53-
// candidate 1:
54-
// - self: /usr/local/bin/limactl
55-
// - agent: /usr/local/share/lima/lima-guestagent.Linux-x86_64
56-
// - dir: /usr/local/share/lima
57-
filepath.Join(selfDirDir, "share/lima/lima-guestagent."+ostype+"-"+arch),
58-
// TODO: support custom path
59-
}
60-
if debugutil.Debug {
61-
// candidate 2: launched by `~/go/bin/dlv dap`
62-
// - self: ${workspaceFolder}/cmd/limactl/__debug_bin_XXXXXX
63-
// - agent: ${workspaceFolder}/_output/share/lima/lima-guestagent.Linux-x86_64
64-
// - dir: ${workspaceFolder}/_output/share/lima
65-
candidateForDebugBuild := filepath.Join(filepath.Dir(selfDirDir), "_output/share/lima/lima-guestagent."+ostype+"-"+arch)
66-
gaCandidates = append(gaCandidates, candidateForDebugBuild)
67-
logrus.Infof("debug mode detected, adding more guest agent candidates: %v", candidateForDebugBuild)
40+
binDir := filepath.Join(selfDir, "bin")
41+
gaBinary := filepath.Join(binDir, "lima-guestagent."+limayaml.Arch)
42+
if _, err := os.Stat(gaBinary); err == nil {
43+
return binDir, nil
6844
}
69-
for _, gaCandidate := range gaCandidates {
70-
if _, err := os.Stat(gaCandidate); err == nil {
71-
return filepath.Dir(gaCandidate), nil
72-
} else if !errors.Is(err, os.ErrNotExist) {
73-
return "", err
74-
}
75-
if _, err := os.Stat(gaCandidate + ".gz"); err == nil {
76-
return filepath.Dir(gaCandidate), nil
77-
} else if !errors.Is(err, os.ErrNotExist) {
78-
return "", err
79-
}
45+
shareDir := filepath.Join(selfDir, "share", "lima")
46+
gaBinary = filepath.Join(shareDir, "bin", "lima-guestagent."+limayaml.Arch)
47+
if _, err := os.Stat(gaBinary); err == nil {
48+
return filepath.Join(shareDir, "bin"), nil
8049
}
81-
82-
return "", fmt.Errorf("failed to find \"lima-guestagent.%s-%s\" binary for %q, attempted %v",
83-
ostype, arch, self, gaCandidates)
50+
debugutil.Logger().WithField("gaBinary", gaBinary).Warn("Guest agent binary not found")
51+
return "", nil
8452
}
8553

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

0 commit comments

Comments
 (0)