跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

usrlocal.go

217 行 · 6.6 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package usrlocal
5
6import (
7 "errors"
8 "fmt"
9 "io/fs"
10 "os"
11 "os/exec"
12 "path/filepath"
13 "strings"
14 "sync"
15
16 "github.com/sirupsen/logrus"
17
18 "github.com/lima-vm/lima/v2/pkg/debugutil"
19 "github.com/lima-vm/lima/v2/pkg/limatype"
20)
21
22// ExecutableViaArgs0 returns the absolute path to the executable used to start this process.
23// It will also append the file extension on Windows, if necessary.
24// This function is different from os.Executable(), which will use /proc/self/exe on Linux
25// and therefore will resolve any symlink used to locate the executable. This function will
26// return the symlink instead because we want to be able to locate ../share/lima relative
27// to the location of the symlink, and not the actual executable. This is important when
28// using Homebrew.
29var ExecutableViaArgs0 = sync.OnceValues(func() (string, error) {
30 if os.Args[0] == "" {
31 return "", errors.New("os.Args[0] has not been set")
32 }
33 executable, err := exec.LookPath(os.Args[0])
34 if err == nil {
35 executable, err = filepath.Abs(executable)
36 }
37 if err != nil {
38 return "", fmt.Errorf("os.Args[0] is invalid: %w", err)
39 }
40
41 return executable, nil
42})
43
44// SelfDirs returns a list of directory paths where the current executable might be located.
45// It checks both os.Args[0] and os.Executable() methods and returns directories containing
46// the executable, resolving symlinks as needed.
47func SelfDirs() []string {
48 var selfPaths []string
49
50 selfViaArgs0, err := ExecutableViaArgs0()
51 if err != nil {
52 logrus.WithError(err).Warn("failed to find executable from os.Args[0]")
53 } else {
54 selfPaths = append(selfPaths, filepath.Dir(selfViaArgs0))
55 }
56
57 selfViaOS, err := os.Executable()
58 if err != nil {
59 logrus.WithError(err).Warn("failed to find os.Executable()")
60 } else {
61 selfFinalPathViaOS, err := filepath.EvalSymlinks(selfViaOS)
62 if err != nil {
63 logrus.WithError(err).Warn("failed to resolve symlinks")
64 selfFinalPathViaOS = selfViaOS // fallback to the original path
65 }
66
67 selfDir := filepath.Dir(selfFinalPathViaOS)
68 if len(selfPaths) == 0 || selfDir != selfPaths[0] {
69 selfPaths = append(selfPaths, selfDir)
70 }
71 }
72
73 return selfPaths
74}
75
76func delveDebugExe() string {
77 exe, err := os.Executable()
78 if err != nil {
79 return ""
80 }
81 exeBase := filepath.Base(exe)
82 if strings.HasPrefix(exeBase, "__debug_bin") {
83 return exe
84 }
85 return ""
86}
87
88func delveWorkspace() string {
89 self := delveDebugExe()
90 if self == "" {
91 return ""
92 }
93 // https://github.com/lima-vm/lima/pull/2651/commits/644c11373cb79aaebd8520706f7d51bd3ee5fbe4
94 // launched by `~/go/bin/dlv dap`
95 // - self: ${workspaceFolder}/cmd/limactl/__debug_bin_XXXXXX
96 return filepath.Dir(filepath.Dir(filepath.Dir(self)))
97}
98
99// ShareLima returns the <PREFIX>/share/lima directories.
100func ShareLima() ([]string, error) {
101 var candidates []string
102 selfDirs := SelfDirs()
103 for _, selfDir := range selfDirs {
104 // selfDir: /usr/local/bin
105 // prefix: /usr/local
106 // candidate: /usr/local/share/lima
107 prefix := filepath.Dir(selfDir)
108 candidate := filepath.Join(prefix, "share", "lima")
109 if ents, err := os.ReadDir(candidate); err == nil && len(ents) > 0 {
110 candidates = append(candidates, candidate)
111 }
112 }
113 if debugutil.Debug {
114 if workspace := delveWorkspace(); workspace != "" {
115 // https://github.com/lima-vm/lima/pull/2651/commits/644c11373cb79aaebd8520706f7d51bd3ee5fbe4
116 // launched by `~/go/bin/dlv dap`
117 // - self: ${workspaceFolder}/cmd/limactl/__debug_bin_XXXXXX
118 // - agent: ${workspaceFolder}/_output/share/lima/lima-guestagent.Linux-x86_64
119 // - dir: ${workspaceFolder}/_output/share/lima
120 candidate := filepath.Join(workspace, "_output", "share", "lima")
121 if ents, err := os.ReadDir(candidate); err == nil && len(ents) > 0 {
122 candidates = append(candidates, candidate)
123 }
124 }
125 }
126 return candidates, nil
127}
128
129// GuestAgentBinary returns the absolute path of the guest agent binary, possibly with ".gz" suffix.
130func GuestAgentBinary(ostype limatype.OS, arch limatype.Arch) (string, error) {
131 if ostype == "" {
132 return "", errors.New("os must be set")
133 }
134 if arch == "" {
135 return "", errors.New("arch must be set")
136 }
137 shareLimaDirs, err := ShareLima()
138 if err != nil {
139 return "", err
140 }
141 for _, dir := range shareLimaDirs {
142 uncomp := filepath.Join(dir, "lima-guestagent."+ostype+"-"+arch)
143 comp := uncomp + ".gz"
144 var res string
145 res, err = chooseGABinary([]string{comp, uncomp})
146 if err != nil {
147 logrus.Debug(err)
148 continue
149 }
150 return res, nil
151 }
152 if err == nil {
153 // caller expects err to be comparable to fs.ErrNotExist
154 err = fs.ErrNotExist
155 }
156 return "", fmt.Errorf("guest agent binary could not be found for %s-%s: %w (Hint: try installing `lima-additional-guestagents` package)", ostype, arch, err)
157}
158
159func chooseGABinary(candidates []string) (string, error) {
160 var entries []string
161 for _, f := range candidates {
162 if _, err := os.Stat(f); err != nil {
163 if !errors.Is(err, fs.ErrNotExist) {
164 logrus.WithError(err).Warnf("failed to stat %#q", f)
165 }
166 continue
167 }
168 entries = append(entries, f)
169 }
170 switch len(entries) {
171 case 0:
172 return "", fmt.Errorf("%w: attempted %v", fs.ErrNotExist, candidates)
173 case 1:
174 return entries[0], nil
175 default:
176 logrus.Warnf("multiple files found, choosing %#q from %v; consider removing the other ones",
177 entries[0], candidates)
178 return entries[0], nil
179 }
180}
181
182// LibexecLima returns the <PREFIX>/libexec/lima directories.
183// For Homebrew compatibility, it also checks <PREFIX>/lib/lima.
184func LibexecLima() ([]string, error) {
185 var candidates []string
186 selfDirs := SelfDirs()
187 for _, selfDir := range selfDirs {
188 // selfDir: /usr/local/bin
189 // prefix: /usr/local
190 // candidate: /usr/local/libexec/lima
191 prefix := filepath.Dir(selfDir)
192 candidate := filepath.Join(prefix, "libexec", "lima")
193 if ents, err := os.ReadDir(candidate); err == nil && len(ents) > 0 {
194 candidates = append(candidates, candidate)
195 }
196 // selfDir: /opt/homebrew/bin
197 // prefix: /opt/homebrew
198 // candidate: /opt/homebrew/lib/lima
199 //
200 // Note that there is no /opt/homebrew/libexec directory,
201 // as Homebrew reserves libexec for private use.
202 // https://github.com/lima-vm/lima/issues/4295#issuecomment-3490680651
203 candidate = filepath.Join(prefix, "lib", "lima")
204 if ents, err := os.ReadDir(candidate); err == nil && len(ents) > 0 {
205 candidates = append(candidates, candidate)
206 }
207 }
208 if debugutil.Debug {
209 if workspace := delveWorkspace(); workspace != "" {
210 candidate := filepath.Join(workspace, "_output", "libexec", "lima")
211 if ents, err := os.ReadDir(candidate); err == nil && len(ents) > 0 {
212 candidates = append(candidates, candidate)
213 }
214 }
215 }
216 return candidates, nil
217}