跳到主要内容

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

浏览文件

lima_instance.go

110 行 · 3.6 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package limatype
5
6import (
7 "encoding/json"
8 "errors"
9 "os"
10 "path/filepath"
11
12 "github.com/lima-vm/lima/v2/pkg/limatype/filenames"
13)
14
15type Status = string
16
17const (
18 StatusUnknown Status = ""
19 StatusUninitialized Status = "Uninitialized"
20 StatusInstalling Status = "Installing"
21 StatusBroken Status = "Broken"
22 StatusStopped Status = "Stopped"
23 StatusRunning Status = "Running"
24)
25
26type Instance struct {
27 Name string `json:"name"`
28 // Hostname, not HostName (corresponds to SSH's naming convention)
29 Hostname string `json:"hostname"`
30 Status Status `json:"status"`
31 Dir string `json:"dir"`
32 VMType VMType `json:"vmType"`
33 Arch Arch `json:"arch"`
34 CPUs int `json:"cpus,omitempty"`
35 Memory int64 `json:"memory,omitempty"` // bytes
36 Disk int64 `json:"disk,omitempty"` // bytes
37 Message string `json:"message,omitempty"`
38 AdditionalDisks []Disk `json:"additionalDisks,omitempty"`
39 Networks []Network `json:"network,omitempty"`
40 SSHLocalPort int `json:"sshLocalPort,omitempty"`
41 SSHConfigFile string `json:"sshConfigFile,omitempty"`
42 HostAgentPID int `json:"hostAgentPID,omitempty"`
43 DriverPID int `json:"driverPID,omitempty"`
44 Errors []error `json:"errors,omitempty"`
45 Config *LimaYAML `json:"config,omitempty"`
46 SSHAddress string `json:"sshAddress,omitempty"`
47 Protected bool `json:"protected"`
48 LimaVersion string `json:"limaVersion"`
49 Param map[string]string `json:"param,omitempty"`
50 AutoStartedIdentifier string `json:"autoStartedIdentifier,omitempty"`
51}
52
53// Protect protects the instance to prohibit accidental removal.
54// Protect does not return an error even when the instance is already protected.
55func (inst *Instance) Protect() error {
56 protected := filepath.Join(inst.Dir, filenames.Protected)
57 // TODO: Do an equivalent of `chmod +a "everyone deny delete,delete_child,file_inherit,directory_inherit"`
58 // https://github.com/lima-vm/lima/issues/1595
59 if err := os.WriteFile(protected, nil, 0o400); err != nil {
60 return err
61 }
62 inst.Protected = true
63 return nil
64}
65
66// Unprotect unprotects the instance.
67// Unprotect does not return an error even when the instance is already unprotected.
68func (inst *Instance) Unprotect() error {
69 protected := filepath.Join(inst.Dir, filenames.Protected)
70 if err := os.RemoveAll(protected); err != nil {
71 return err
72 }
73 inst.Protected = false
74 return nil
75}
76
77func (inst *Instance) MarshalJSON() ([]byte, error) {
78 type Alias Instance
79 errorsAsStrings := make([]string, len(inst.Errors))
80 for i, err := range inst.Errors {
81 if err != nil {
82 errorsAsStrings[i] = err.Error()
83 }
84 }
85 return json.Marshal(&struct {
86 *Alias
87 Errors []string `json:"errors,omitempty"`
88 }{
89 Alias: (*Alias)(inst),
90 Errors: errorsAsStrings,
91 })
92}
93
94func (inst *Instance) UnmarshalJSON(data []byte) error {
95 type Alias Instance
96 aux := &struct {
97 *Alias
98 Errors []string `json:"errors,omitempty"`
99 }{
100 Alias: (*Alias)(inst),
101 }
102 if err := json.Unmarshal(data, &aux); err != nil {
103 return err
104 }
105 inst.Errors = nil
106 for _, msg := range aux.Errors {
107 inst.Errors = append(inst.Errors, errors.New(msg))
108 }
109 return nil
110}