跳到主要内容

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

浏览文件

linux_template.go

122 行 · 3.7 KB历史下载
1package hostimage
2
3import (
4 "encoding/json"
5 "errors"
6 "os"
7 "path/filepath"
8)
9
10func linuxTemplateData(plan Plan) ([]byte, error) {
11 // These supported cloud images need no distribution-specific template
12 // settings. Keep one pinned local image, with no network image fallback.
13 value := map[string]any{
14 "minimumLimaVersion": "2.0.0",
15 "images": []map[string]string{{"location": plan.Path, "arch": map[string]string{"amd64": "x86_64", "arm64": "aarch64"}[plan.Host.Arch], "digest": plan.Digest}},
16 }
17 return json.Marshal(value)
18}
19
20func WriteLinuxTemplate(state string, plan Plan) error {
21 if err := plan.Validate(); err != nil {
22 return err
23 }
24 if !IsLinux(plan.Host.System) || plan.Path == "" || plan.Digest == "" {
25 return errors.New("Linux 系统材料尚未准备好")
26 }
27 data, err := linuxTemplateData(plan)
28 if err != nil {
29 return err
30 }
31 dir := filepath.Join(state, "guest-media")
32 if err = os.MkdirAll(dir, 0700); err != nil {
33 return err
34 }
35 path := filepath.Join(dir, "host-system.yaml")
36 if current, err := os.ReadFile(path); err == nil {
37 if string(current) != string(data) {
38 return errors.New("Linux 安装已绑定另一份系统材料")
39 }
40 return Save(dir, plan)
41 } else if !errors.Is(err, os.ErrNotExist) {
42 return err
43 }
44 file, err := os.CreateTemp(dir, ".host-system-*.yaml")
45 if err != nil {
46 return err
47 }
48 defer os.Remove(file.Name())
49 _, err = file.Write(data)
50 if err = errors.Join(err, file.Close()); err != nil {
51 return err
52 }
53 if err = os.Rename(file.Name(), path); err != nil {
54 return err
55 }
56 return Save(dir, plan)
57}
58
59// ClearLinuxTemplate releases an unstarted choice while retaining its media.
60func ClearLinuxTemplate(state string, plan Plan) error {
61 dir := filepath.Join(state, "guest-media")
62 path := filepath.Join(dir, "host-system.yaml")
63 data, err := os.ReadFile(path)
64 if errors.Is(err, os.ErrNotExist) {
65 return nil
66 }
67 if err != nil {
68 return err
69 }
70 expected, err := linuxTemplateData(plan)
71 if err != nil || string(expected) != string(data) {
72 return errors.New("本机系统模板归属已变化")
73 }
74 if err := os.Remove(path); err != nil {
75 return err
76 }
77 if err := os.Remove(filepath.Join(dir, sourceRecord)); !errors.Is(err, os.ErrNotExist) {
78 return err
79 }
80 return nil
81}
82
83func LinuxTemplate(state string) (string, error) {
84 path := filepath.Join(state, "guest-media", "host-system.yaml")
85 info, err := os.Lstat(path)
86 if errors.Is(err, os.ErrNotExist) {
87 if _, recordErr := os.Lstat(filepath.Join(state, "guest-media", sourceRecord)); !errors.Is(recordErr, os.ErrNotExist) {
88 return "", errors.New("所选 Linux 系统的安装模板缺失,请恢复原环境资料;不会改用其他系统")
89 }
90 return "template:debian-13", nil
91 }
92 if err != nil {
93 return "", err
94 }
95 if !info.Mode().IsRegular() || info.Size() > 32<<10 {
96 return "", errors.New("本机系统安装模板无效")
97 }
98 plan, err := Load(filepath.Join(state, "guest-media"))
99 if err != nil || !IsLinux(plan.Host.System) {
100 return "", errors.New("所选 Linux 系统来源记录无效,请恢复原环境资料")
101 }
102 expected, err := linuxTemplateData(plan)
103 data, readErr := os.ReadFile(path)
104 if err != nil || readErr != nil || string(data) != string(expected) {
105 return "", errors.New("所选 Linux 系统的模板与来源不一致,请核对原环境资料")
106 }
107 return path, nil
108}
109
110// LinuxSystem reads the durable image choice, including after installation
111// has finished and its temporary installation record has been removed.
112func LinuxSystem(state string) (string, error) {
113 path, err := LinuxTemplate(state)
114 if err != nil {
115 return "", err
116 }
117 if path == "template:debian-13" {
118 return "debian-13", nil
119 }
120 plan, err := Load(filepath.Join(state, "guest-media"))
121 return plan.Host.System, err
122}