跳到主要内容

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

浏览文件

share.go

121 行 · 3.5 KB历史下载
1package limaembed
2
3import (
4 "crypto/sha256"
5 _ "embed"
6 "errors"
7 "fmt"
8 "io/fs"
9 "os"
10 "path/filepath"
11 "runtime"
12 "strings"
13)
14
15// Share is Lima's share/lima, written out of the binary.
16type Share struct {
17 // Dir holds templates/ and the bundled guest agents.
18 Dir string
19 guestAgents map[string]string
20}
21
22// Cage owns this pin independently of Lima's periodically regenerated templates.
23//
24//go:embed host-images/ubuntu-22.04.yaml
25var ubuntu2204Image []byte
26
27// ReadImageTemplate returns the pinned images bundled with this application.
28func ReadImageTemplate(system string) ([]byte, error) {
29 switch system {
30 case "ubuntu-22.04":
31 return ubuntu2204Image, nil
32 case "debian-12", "debian-13", "ubuntu-24.04":
33 return fs.ReadFile(assets, "assets/templates/_images/"+system+".yaml")
34 default:
35 return nil, fmt.Errorf("unsupported host image template %q", system)
36 }
37}
38
39// Templates is where Lima reads templates from (LIMA_TEMPLATES_PATH).
40func (s *Share) Templates() string {
41 return filepath.Join(s.Dir, "templates")
42}
43
44// GuestAgent returns only helpers that were extracted from this executable.
45// An older installation may have left other binaries in the same directory.
46func (s *Share) GuestAgent(guestOS, arch string) (string, error) {
47 name := "lima-guestagent." + guestOS + "-" + arch + ".gz"
48 if agent, ok := s.guestAgents[name]; ok {
49 return agent, nil
50 }
51 return "", fmt.Errorf("this build does not include the Lima guest agent for %s/%s", guestOS, arch)
52}
53
54// ErrNoAssets is returned by WriteShare when agent-vm was built without
55// the guest agent (`go build` instead of `make`).
56var ErrNoAssets = errors.New("this agent-vm was built without Lima's guest agent: build it with make")
57
58// WriteShare writes the embedded files into dir/share/lima, each one that is
59// missing or differs from the embedded copy, and returns where they are. The
60// files are checked on every call: a damaged guest agent never reaches a VM.
61func WriteShare(dir string) (*Share, error) {
62 return writeShare(dir, assets)
63}
64
65func writeShare(dir string, files fs.FS) (*Share, error) {
66 if _, err := fs.Stat(files, "assets/lima-guestagent.Linux-"+guestArch()+".gz"); err != nil {
67 return nil, ErrNoAssets
68 }
69 share := &Share{Dir: filepath.Join(dir, "share", "lima"), guestAgents: make(map[string]string)}
70 err := fs.WalkDir(files, "assets", func(p string, d fs.DirEntry, err error) error {
71 if err != nil {
72 return err
73 }
74 rel := strings.TrimPrefix(strings.TrimPrefix(p, "assets"), "/")
75 if rel == "README" {
76 return nil
77 }
78 dst := filepath.Join(share.Dir, filepath.FromSlash(rel))
79 if d.IsDir() {
80 return os.MkdirAll(dst, 0o700)
81 }
82 want, err := fs.ReadFile(files, p)
83 if err != nil {
84 return err
85 }
86 if err := writeIfChanged(dst, want); err != nil {
87 return err
88 }
89 if strings.HasPrefix(d.Name(), "lima-guestagent.") && strings.HasSuffix(d.Name(), ".gz") {
90 share.guestAgents[d.Name()] = dst
91 }
92 return nil
93 })
94 if err != nil {
95 return nil, fmt.Errorf("writing Lima's files to %s: %w", share.Dir, err)
96 }
97 return share, nil
98}
99
100func writeIfChanged(dst string, want []byte) error {
101 if have, err := os.ReadFile(dst); err == nil && sha256.Sum256(have) == sha256.Sum256(want) {
102 return nil
103 }
104 tmp := dst + ".tmp"
105 if err := os.WriteFile(tmp, want, 0o600); err != nil {
106 return err
107 }
108 return os.Rename(tmp, dst)
109}
110
111// guestArch is this host's architecture as Lima names guest agents.
112func guestArch() string {
113 switch runtime.GOARCH {
114 case "amd64":
115 return "x86_64"
116 case "arm64":
117 return "aarch64"
118 default:
119 return runtime.GOARCH
120 }
121}