Skip to content

Commit 65669f4

Browse files
committed
pkg/cidata: split pkg/usrlocalsharelima.Dir()
Signed-off-by: Akihiro Suda <[email protected]>
1 parent ee32d35 commit 65669f4

File tree

2 files changed

+65
-37
lines changed

2 files changed

+65
-37
lines changed

pkg/cidata/cidata.go

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"errors"
55
"fmt"
66
"io"
7-
"io/fs"
87
"os"
98
"path/filepath"
109
"regexp"
@@ -19,6 +18,7 @@ import (
1918
qemu "github.com/lima-vm/lima/pkg/qemu/const"
2019
"github.com/lima-vm/lima/pkg/sshutil"
2120
"github.com/lima-vm/lima/pkg/store/filenames"
21+
"github.com/lima-vm/lima/pkg/usrlocalsharelima"
2222
"github.com/sirupsen/logrus"
2323
)
2424

@@ -204,43 +204,10 @@ func GuestAgentBinary(arch string) (io.ReadCloser, error) {
204204
if arch == "" {
205205
return nil, errors.New("arch must be set")
206206
}
207-
self, err := os.Executable()
207+
dir, err := usrlocalsharelima.Dir()
208208
if err != nil {
209209
return nil, err
210210
}
211-
selfSt, err := os.Stat(self)
212-
if err != nil {
213-
return nil, err
214-
}
215-
if selfSt.Mode()&fs.ModeSymlink != 0 {
216-
self, err = os.Readlink(self)
217-
if err != nil {
218-
return nil, err
219-
}
220-
}
221-
222-
// self: /usr/local/bin/limactl
223-
selfDir := filepath.Dir(self)
224-
selfDirDir := filepath.Dir(selfDir)
225-
candidates := []string{
226-
// candidate 0:
227-
// - self: /Applications/Lima.app/Contents/MacOS/limactl
228-
// - agent: /Applications/Lima.app/Contents/MacOS/lima-guestagent.Linux-x86_64
229-
filepath.Join(selfDir, "lima-guestagent.Linux-"+arch),
230-
// candidate 1:
231-
// - self: /usr/local/bin/limactl
232-
// - agent: /usr/local/share/lima/lima-guestagent.Linux-x86_64
233-
filepath.Join(selfDirDir, "share/lima/lima-guestagent.Linux-"+arch),
234-
// TODO: support custom path
235-
}
236-
for _, candidate := range candidates {
237-
if f, err := os.Open(candidate); err == nil {
238-
return f, nil
239-
} else if !errors.Is(err, os.ErrNotExist) {
240-
return nil, err
241-
}
242-
}
243-
244-
return nil, fmt.Errorf("failed to find \"lima-guestagent.Linux-%s\" binary for %q, attempted %v",
245-
arch, self, candidates)
211+
gaPath := filepath.Join(dir, "lima-guestagent.Linux-"+arch)
212+
return os.Open(gaPath)
246213
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package usrlocalsharelima
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"io/fs"
7+
"os"
8+
"path/filepath"
9+
"runtime"
10+
11+
"github.com/lima-vm/lima/pkg/limayaml"
12+
)
13+
14+
func Dir() (string, error) {
15+
self, err := os.Executable()
16+
if err != nil {
17+
return "", err
18+
}
19+
selfSt, err := os.Stat(self)
20+
if err != nil {
21+
return "", err
22+
}
23+
if selfSt.Mode()&fs.ModeSymlink != 0 {
24+
self, err = os.Readlink(self)
25+
if err != nil {
26+
return "", err
27+
}
28+
}
29+
30+
arch := limayaml.NewArch(runtime.GOARCH)
31+
if arch == "" {
32+
return "", fmt.Errorf("failed to get arch for %q", runtime.GOARCH)
33+
}
34+
35+
// self: /usr/local/bin/limactl
36+
selfDir := filepath.Dir(self)
37+
selfDirDir := filepath.Dir(selfDir)
38+
gaCandidates := []string{
39+
// candidate 0:
40+
// - self: /Applications/Lima.app/Contents/MacOS/limactl
41+
// - agent: /Applications/Lima.app/Contents/MacOS/lima-guestagent.Linux-x86_64
42+
// - dir: /Applications/Lima.app/Contents/MacOS
43+
filepath.Join(selfDir, "lima-guestagent.Linux-"+arch),
44+
// candidate 1:
45+
// - self: /usr/local/bin/limactl
46+
// - agent: /usr/local/share/lima/lima-guestagent.Linux-x86_64
47+
// - dir: /usr/local/share/lima
48+
filepath.Join(selfDirDir, "share/lima/lima-guestagent.Linux-"+arch),
49+
// TODO: support custom path
50+
}
51+
for _, gaCandidate := range gaCandidates {
52+
if _, err := os.Stat(gaCandidate); err == nil {
53+
return filepath.Dir(gaCandidate), nil
54+
} else if !errors.Is(err, os.ErrNotExist) {
55+
return "", err
56+
}
57+
}
58+
59+
return "", fmt.Errorf("failed to find \"lima-guestagent.Linux-%s\" binary for %q, attempted %v",
60+
arch, self, gaCandidates)
61+
}

0 commit comments

Comments
 (0)