-
Notifications
You must be signed in to change notification settings - Fork 560
/
usrlocalsharelima.go
62 lines (56 loc) · 1.56 KB
/
usrlocalsharelima.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package usrlocalsharelima
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"runtime"
"github.com/lima-vm/lima/pkg/limayaml"
)
func Dir() (string, error) {
self, err := os.Executable()
if err != nil {
return "", err
}
selfSt, err := os.Stat(self)
if err != nil {
return "", err
}
if selfSt.Mode()&fs.ModeSymlink != 0 {
self, err = os.Readlink(self)
if err != nil {
return "", err
}
}
ostype := limayaml.NewOS("linux")
arch := limayaml.NewArch(runtime.GOARCH)
if arch == "" {
return "", fmt.Errorf("failed to get arch for %q", runtime.GOARCH)
}
// self: /usr/local/bin/limactl
selfDir := filepath.Dir(self)
selfDirDir := filepath.Dir(selfDir)
gaCandidates := []string{
// candidate 0:
// - self: /Applications/Lima.app/Contents/MacOS/limactl
// - agent: /Applications/Lima.app/Contents/MacOS/lima-guestagent.Linux-x86_64
// - dir: /Applications/Lima.app/Contents/MacOS
filepath.Join(selfDir, "lima-guestagent."+ostype+"-"+arch),
// candidate 1:
// - self: /usr/local/bin/limactl
// - agent: /usr/local/share/lima/lima-guestagent.Linux-x86_64
// - dir: /usr/local/share/lima
filepath.Join(selfDirDir, "share/lima/lima-guestagent."+ostype+"-"+arch),
// TODO: support custom path
}
for _, gaCandidate := range gaCandidates {
if _, err := os.Stat(gaCandidate); err == nil {
return filepath.Dir(gaCandidate), nil
} else if !errors.Is(err, os.ErrNotExist) {
return "", err
}
}
return "", fmt.Errorf("failed to find \"lima-guestagent.%s-%s\" binary for %q, attempted %v",
ostype, arch, self, gaCandidates)
}