Skip to content

Commit 1d4acbe

Browse files
committed
pkg/usrlocalsharelima: simplify
Signed-off-by: Akihiro Suda <[email protected]>
1 parent 80360ed commit 1d4acbe

File tree

7 files changed

+40
-78
lines changed

7 files changed

+40
-78
lines changed

cmd/limactl/guest-install.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"github.com/lima-vm/lima/v2/pkg/limatype"
2222
"github.com/lima-vm/lima/v2/pkg/limatype/filenames"
2323
"github.com/lima-vm/lima/v2/pkg/store"
24-
"github.com/lima-vm/lima/v2/pkg/usrlocalsharelima"
24+
"github.com/lima-vm/lima/v2/pkg/usrlocal"
2525
)
2626

2727
func newGuestInstallCommand() *cobra.Command {
@@ -81,7 +81,7 @@ func guestInstallAction(cmd *cobra.Command, args []string) error {
8181
prefix := *inst.Config.GuestInstallPrefix
8282

8383
// lima-guestagent
84-
guestAgentBinary, err := usrlocalsharelima.GuestAgentBinary(*inst.Config.OS, *inst.Config.Arch)
84+
guestAgentBinary, err := usrlocal.GuestAgentBinary(*inst.Config.OS, *inst.Config.Arch)
8585
if err != nil {
8686
return err
8787
}

pkg/instance/start.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
"github.com/lima-vm/lima/v2/pkg/limatype/filenames"
3232
"github.com/lima-vm/lima/v2/pkg/registry"
3333
"github.com/lima-vm/lima/v2/pkg/store"
34-
"github.com/lima-vm/lima/v2/pkg/usrlocalsharelima"
34+
"github.com/lima-vm/lima/v2/pkg/usrlocal"
3535
)
3636

3737
// DefaultWatchHostAgentEventsTimeout is the duration to wait for the instance
@@ -48,7 +48,7 @@ type Prepared struct {
4848
func Prepare(ctx context.Context, inst *limatype.Instance, guestAgent string) (*Prepared, error) {
4949
if !*inst.Config.Plain && guestAgent == "" {
5050
var err error
51-
guestAgent, err = usrlocalsharelima.GuestAgentBinary(*inst.Config.OS, *inst.Config.Arch)
51+
guestAgent, err = usrlocal.GuestAgentBinary(*inst.Config.OS, *inst.Config.Arch)
5252
if err != nil {
5353
return nil, err
5454
}

pkg/limainfo/limainfo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"github.com/lima-vm/lima/v2/pkg/plugins"
2121
"github.com/lima-vm/lima/v2/pkg/registry"
2222
"github.com/lima-vm/lima/v2/pkg/templatestore"
23-
"github.com/lima-vm/lima/v2/pkg/usrlocalsharelima"
23+
"github.com/lima-vm/lima/v2/pkg/usrlocal"
2424
"github.com/lima-vm/lima/v2/pkg/version"
2525
)
2626

@@ -97,7 +97,7 @@ func New(ctx context.Context) (*LimaInfo, error) {
9797
}
9898
info.IdentityFile = filepath.Join(configDir, filenames.UserPrivateKey)
9999
for _, arch := range limatype.ArchTypes {
100-
bin, err := usrlocalsharelima.GuestAgentBinary(limatype.LINUX, arch)
100+
bin, err := usrlocal.GuestAgentBinary(limatype.LINUX, arch)
101101
if err != nil {
102102
if errors.Is(err, fs.ErrNotExist) {
103103
logrus.WithError(err).Debugf("Failed to resolve the guest agent binary for %q", arch)

pkg/plugins/plugins.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"github.com/sirupsen/logrus"
1919

2020
"github.com/lima-vm/lima/v2/pkg/osutil"
21-
"github.com/lima-vm/lima/v2/pkg/usrlocalsharelima"
21+
"github.com/lima-vm/lima/v2/pkg/usrlocal"
2222
)
2323

2424
const defaultPathExt = ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL"
@@ -51,15 +51,15 @@ var Discover = sync.OnceValues(func() ([]Plugin, error) {
5151
})
5252

5353
var getPluginDirectories = sync.OnceValue(func() []string {
54-
dirs := usrlocalsharelima.SelfDirs()
54+
dirs := usrlocal.SelfDirs()
5555

5656
pathEnv := os.Getenv("PATH")
5757
if pathEnv != "" {
5858
pathDirs := filepath.SplitList(pathEnv)
5959
dirs = append(dirs, pathDirs...)
6060
}
6161

62-
libexecDirs, err := usrlocalsharelima.LibexecLima()
62+
libexecDirs, err := usrlocal.LibexecLima()
6363
if err == nil {
6464
dirs = append(dirs, libexecDirs...)
6565
}

pkg/registry/registry.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616

1717
"github.com/lima-vm/lima/v2/pkg/driver"
1818
"github.com/lima-vm/lima/v2/pkg/driver/external/client"
19-
"github.com/lima-vm/lima/v2/pkg/usrlocalsharelima"
19+
"github.com/lima-vm/lima/v2/pkg/usrlocal"
2020
)
2121

2222
const (
@@ -124,7 +124,7 @@ func discoverDrivers() error {
124124
}
125125
}
126126

127-
stdDriverDirs, err := usrlocalsharelima.LibexecLima()
127+
stdDriverDirs, err := usrlocal.LibexecLima()
128128
if err != nil {
129129
return err
130130
}

pkg/templatestore/templatestore.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"unicode"
1616

1717
"github.com/lima-vm/lima/v2/pkg/limatype/dirnames"
18-
"github.com/lima-vm/lima/v2/pkg/usrlocalsharelima"
18+
"github.com/lima-vm/lima/v2/pkg/usrlocal"
1919
)
2020

2121
type Template struct {
@@ -31,14 +31,15 @@ func templatesPaths() ([]string, error) {
3131
if err != nil {
3232
return nil, err
3333
}
34-
shareDir, err := usrlocalsharelima.Dir()
34+
shareLimaDirs, err := usrlocal.ShareLima()
3535
if err != nil {
3636
return nil, err
3737
}
38-
return []string{
39-
limaTemplatesDir,
40-
filepath.Join(shareDir, "templates"),
41-
}, nil
38+
res := []string{limaTemplatesDir}
39+
for _, shareLimaDir := range shareLimaDirs {
40+
res = append(res, filepath.Join(shareLimaDir, "templates"))
41+
}
42+
return res, nil
4243
}
4344

4445
// Read searches for template `name` in all template directories and returns the

pkg/usrlocalsharelima/usrlocalsharelima.go renamed to pkg/usrlocal/usrlocal.go

Lines changed: 22 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-FileCopyrightText: Copyright The Lima Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package usrlocalsharelima
4+
package usrlocal
55

66
import (
77
"errors"
@@ -10,12 +10,10 @@ import (
1010
"os"
1111
"os/exec"
1212
"path/filepath"
13-
"runtime"
1413
"sync"
1514

1615
"github.com/sirupsen/logrus"
1716

18-
"github.com/lima-vm/lima/v2/pkg/debugutil"
1917
"github.com/lima-vm/lima/v2/pkg/limatype"
2018
)
2119

@@ -73,61 +71,21 @@ func SelfDirs() []string {
7371
return selfPaths
7472
}
7573

76-
// Dir returns the location of the <PREFIX>/lima/share directory, relative to the location
77-
// of the current executable. It checks for multiple possible filesystem layouts and returns
78-
// the first candidate that contains the native guest agent binary.
79-
func Dir() (string, error) {
74+
// ShareLima returns the <PREFIX>/share/lima directories.
75+
func ShareLima() ([]string, error) {
76+
var candidates []string
8077
selfDirs := SelfDirs()
81-
82-
ostype := limatype.NewOS("linux")
83-
arch := limatype.NewArch(runtime.GOARCH)
84-
if arch == "" {
85-
return "", fmt.Errorf("failed to get arch for %q", runtime.GOARCH)
86-
}
87-
88-
gaCandidates := []string{}
8978
for _, selfDir := range selfDirs {
9079
// selfDir: /usr/local/bin
91-
selfDirDir := filepath.Dir(selfDir)
92-
gaCandidates = append(gaCandidates,
93-
// candidate 0:
94-
// - self: /Applications/Lima.app/Contents/MacOS/limactl
95-
// - agent: /Applications/Lima.app/Contents/MacOS/lima-guestagent.Linux-x86_64
96-
// - dir: /Applications/Lima.app/Contents/MacOS
97-
filepath.Join(selfDir, "lima-guestagent."+ostype+"-"+arch),
98-
// candidate 1:
99-
// - self: /usr/local/bin/limactl
100-
// - agent: /usr/local/share/lima/lima-guestagent.Linux-x86_64
101-
// - dir: /usr/local/share/lima
102-
filepath.Join(selfDirDir, "share/lima/lima-guestagent."+ostype+"-"+arch),
103-
// TODO: support custom path
104-
)
105-
if debugutil.Debug {
106-
// candidate 2: launched by `~/go/bin/dlv dap`
107-
// - self: ${workspaceFolder}/cmd/limactl/__debug_bin_XXXXXX
108-
// - agent: ${workspaceFolder}/_output/share/lima/lima-guestagent.Linux-x86_64
109-
// - dir: ${workspaceFolder}/_output/share/lima
110-
candidateForDebugBuild := filepath.Join(filepath.Dir(selfDirDir), "_output/share/lima/lima-guestagent."+ostype+"-"+arch)
111-
gaCandidates = append(gaCandidates, candidateForDebugBuild)
112-
logrus.Infof("debug mode detected, adding more guest agent candidates: %v", candidateForDebugBuild)
113-
}
114-
}
115-
116-
for _, gaCandidate := range gaCandidates {
117-
if _, err := os.Stat(gaCandidate); err == nil {
118-
return filepath.Dir(gaCandidate), nil
119-
} else if !errors.Is(err, os.ErrNotExist) {
120-
return "", err
121-
}
122-
if _, err := os.Stat(gaCandidate + ".gz"); err == nil {
123-
return filepath.Dir(gaCandidate), nil
124-
} else if !errors.Is(err, os.ErrNotExist) {
125-
return "", err
80+
// prefix: /usr/local
81+
// candidate: /usr/local/share/lima
82+
prefix := filepath.Dir(selfDir)
83+
candidate := filepath.Join(prefix, "share", "lima")
84+
if ents, err := os.ReadDir(candidate); err == nil && len(ents) > 0 {
85+
candidates = append(candidates, candidate)
12686
}
12787
}
128-
129-
return "", fmt.Errorf("failed to find \"lima-guestagent.%s-%s\" binary for %v, attempted %v",
130-
ostype, arch, selfDirs, gaCandidates)
88+
return candidates, nil
13189
}
13290

13391
// GuestAgentBinary returns the absolute path of the guest agent binary, possibly with ".gz" suffix.
@@ -138,18 +96,21 @@ func GuestAgentBinary(ostype limatype.OS, arch limatype.Arch) (string, error) {
13896
if arch == "" {
13997
return "", errors.New("arch must be set")
14098
}
141-
dir, err := Dir()
99+
shareLimaDirs, err := ShareLima()
142100
if err != nil {
143101
return "", err
144102
}
145-
uncomp := filepath.Join(dir, "lima-guestagent."+ostype+"-"+arch)
146-
comp := uncomp + ".gz"
147-
res, err := chooseGABinary([]string{comp, uncomp})
148-
if err != nil {
149-
logrus.Debug(err)
150-
return "", fmt.Errorf("guest agent binary could not be found for %s-%s: %w (Hint: try installing `lima-additional-guestagents` package)", ostype, arch, err)
103+
for _, dir := range shareLimaDirs {
104+
uncomp := filepath.Join(dir, "lima-guestagent."+ostype+"-"+arch)
105+
comp := uncomp + ".gz"
106+
res, err := chooseGABinary([]string{comp, uncomp})
107+
if err != nil {
108+
logrus.Debug(err)
109+
continue
110+
}
111+
return res, nil
151112
}
152-
return res, nil
113+
return "", fmt.Errorf("guest agent binary could not be found for %s-%s (Hint: try installing `lima-additional-guestagents` package)", ostype, arch)
153114
}
154115

155116
func chooseGABinary(candidates []string) (string, error) {

0 commit comments

Comments
 (0)