跳到主要内容

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

浏览文件

options.go

137 行 · 3.2 KB历史下载
1package cli
2
3import (
4 "fmt"
5 "regexp"
6 "strconv"
7 "strings"
8)
9
10// VMOpts are agent-vm's own options, for the commands that start a VM.
11type VMOpts struct {
12 Disk, Memory, CPUs int // 0: not given
13 SSHPort int // -1: not given; 0 gives the port back to Lima
14 Reset bool
15 ReadOnly bool
16 Scratch bool
17 UnsafeWritableGit bool
18 UnsafeNoPrompts bool
19 RM bool // --rm: delete the VM after the command
20}
21
22func newVMOpts() VMOpts {
23 return VMOpts{SSHPort: -1}
24}
25
26var positiveRe = regexp.MustCompile(`^[1-9][0-9]*$`)
27
28// take reads the option at the start of args, if it is one of agent-vm's,
29// and returns how many words it used: 0 when args[0] is not an option. A
30// value is checked here, so `--disk 10G` is one clear error.
31func (o *VMOpts) take(args []string) (int, error) {
32 if len(args) == 0 {
33 return 0, nil
34 }
35 opt, val, n := args[0], "", 0
36 switch opt {
37 case "--disk", "--memory", "--ram", "--cpus", "--ssh-port":
38 if len(args) < 2 {
39 return 0, fmt.Errorf("%s needs a value.", opt)
40 }
41 val, n = args[1], 2
42 case "--reset":
43 o.Reset = true
44 return 1, nil
45 case "--readonly":
46 o.ReadOnly = true
47 return 1, nil
48 case "--scratch":
49 o.Scratch = true
50 return 1, nil
51 case "--unsafe-writable-git", "--unsafe-writable-git=1":
52 o.UnsafeWritableGit = true
53 return 1, nil
54 case "--unsafe-disable-security-prompts":
55 o.UnsafeNoPrompts = true
56 return 1, nil
57 case "--rm":
58 o.RM = true
59 return 1, nil
60 default:
61 name, v, ok := strings.Cut(opt, "=")
62 switch {
63 case !ok:
64 return 0, nil
65 case name == "--disk", name == "--memory", name == "--ram", name == "--cpus", name == "--ssh-port":
66 opt, val, n = name, v, 1
67 default:
68 return 0, nil
69 }
70 }
71 if opt == "--ram" {
72 opt = "--memory"
73 }
74 if opt == "--ssh-port" {
75 p, err := strconv.Atoi(val)
76 if val != "0" && (!positiveRe.MatchString(val) || err != nil || p < 1024 || p > 65535) {
77 return 0, fmt.Errorf("--ssh-port must be 0 or a port from 1024 to 65535 (got: '%s')", val)
78 }
79 o.SSHPort = p
80 return n, nil
81 }
82 v, err := strconv.Atoi(val)
83 if !positiveRe.MatchString(val) || err != nil {
84 return 0, fmt.Errorf("%s must be a positive integer (got: '%s')", opt, val)
85 }
86 switch opt {
87 case "--disk":
88 o.Disk = v
89 case "--memory":
90 o.Memory = v
91 default:
92 o.CPUs = v
93 }
94 return n, nil
95}
96
97// takeAll reads options from the start of args, and returns the words
98// they used and the rest.
99func (o *VMOpts) takeAll(args []string) (taken, rest []string, err error) {
100 for {
101 n, err := o.take(args)
102 if err != nil {
103 return nil, nil, err
104 }
105 if n == 0 {
106 return taken, args, nil
107 }
108 taken = append(taken, args[:n]...)
109 args = args[n:]
110 }
111}
112
113// split reads agent-vm's options before a command's own words: from the
114// first other word on, everything belongs to the command, so `run docker run
115// --rm x` keeps its --rm. `--` ends the options, and --tty asks for a
116// terminal.
117func (o *VMOpts) split(args []string) (cmd []string, tty bool, err error) {
118 for {
119 n, err := o.take(args)
120 if err != nil {
121 return nil, false, err
122 }
123 if n > 0 {
124 args = args[n:]
125 continue
126 }
127 if len(args) > 0 && args[0] == "--tty" {
128 tty = true
129 args = args[1:]
130 continue
131 }
132 if len(args) > 0 && args[0] == "--" {
133 args = args[1:]
134 }
135 return args, tty, nil
136 }
137}