跳到主要内容

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

浏览文件

cli.go

215 行 · 6.5 KB历史下载
1// Package cli is agent-vm's command line. The commands and options are those
2// of 0.2's agent-vm.sh, with the additions and changes of 0.3 in CHANGELOG.md
3// (--scratch, install, info, code, exit status 2 for wrong usage...).
4package cli
5
6import (
7 "context"
8 _ "embed"
9 "fmt"
10 "io"
11 "os"
12 "regexp"
13 "strings"
14
15 "github.com/sylvinus/agent-vm/internal/version"
16 "github.com/sylvinus/agent-vm/internal/vmname"
17)
18
19//go:embed help.txt
20var helpText string
21
22// IO is where a command reads and writes.
23type IO struct {
24 Stdin io.Reader
25 Stdout, Stderr io.Writer
26}
27
28// The commands VM options mean nothing to: `agent-vm --readonly stop` must
29// not read as if something had been made read-only.
30var noVMOpts = map[string]bool{
31 "stop": true, "rm": true, "destroy": true, "destroy-all": true, "list": true, "status": true,
32 "name": true, "info": true, "env": true, "project-env": true, "version": true, "--version": true,
33 "-V": true, "licenses": true, "doctor": true, "install": true, "uninstall": true, "help": true, "--help": true, "-h": true,
34}
35
36// exitUsage is the exit status of a command line that is wrong: an unknown
37// command, option or argument, a missing value.
38const exitUsage = 2
39
40// stateful are the commands that work on agent-vm's folder or its VMs, so
41// need to know where they are. The others (help, version, licenses, a wrong
42// command line) run without: `version --min` must not fail for want of a
43// home folder, where 1 means "older".
44var stateful = map[string]bool{
45 "name": true, "list": true, "status": true, "stop": true, "rm": true, "destroy": true, "destroy-all": true,
46 "info": true, "env": true, "project-env": true, "claude": true, "opencode": true, "codex": true, "vibe": true,
47 "pi": true, "shell": true, "sh": true, "run": true, "setup": true, "doctor": true, "install": true,
48 "uninstall": true, "code": true,
49}
50
51// needsState reports whether the command line runs a command in stateful.
52// `name` is one: the length of the Lima home's path limits the names.
53func needsState(args []string) bool {
54 opts := newVMOpts()
55 _, rest, err := opts.takeAll(args)
56 return err == nil && len(rest) > 0 && stateful[rest[0]]
57}
58
59// Main runs agent-vm with args (os.Args[1:]) and returns its exit status.
60func Main(args []string, io IO) int {
61 ctx := context.Background()
62 if !needsState(args) {
63 return (&app{io: io}).main(ctx, args)
64 }
65 e, err := newApp(io)
66 if err != nil {
67 fmt.Fprintf(io.Stderr, "Error: %v\n", err)
68 return 1
69 }
70 return e.main(ctx, args)
71}
72
73// main runs the command line. An app made only of io is enough for the
74// commands outside stateful.
75func (e *app) main(ctx context.Context, args []string) int {
76 io := e.io
77 opts := newVMOpts()
78 lead, args, err := opts.takeAll(args)
79 if err != nil {
80 fmt.Fprintf(io.Stderr, "Error: %v\n", err)
81 return exitUsage
82 }
83 cmd := "help"
84 if len(args) > 0 {
85 cmd, args = args[0], args[1:]
86 }
87 if len(lead) > 0 && noVMOpts[cmd] {
88 fmt.Fprintf(io.Stderr, "Error: %s is an option for the commands that start a VM (claude, opencode, codex, vibe, pi, shell, run, code), not for '%s'.\n", lead[0], cmd)
89 return exitUsage
90 }
91 switch cmd {
92 case "help", "--help", "-h":
93 fmt.Fprint(io.Stdout, helpText)
94 return 0
95 case "version", "--version", "-V":
96 return versionCmd(args, io)
97 case "licenses":
98 return licensesCmd(args, io)
99 case "name":
100 return e.nameCmd(args, io)
101 case "list", "status":
102 return e.listCmd(ctx, args)
103 case "stop":
104 return e.stopCmd(ctx, args)
105 case "rm", "destroy":
106 return e.rmCmd(ctx, args)
107 case "destroy-all":
108 return e.destroyAllCmd(ctx, args)
109 case "info":
110 return e.infoCmd(ctx, args)
111 case "env":
112 return e.sharedEnvCmd(args)
113 case "project-env":
114 return e.projectEnvCmd(ctx, args)
115 case "claude", "opencode", "codex", "vibe", "pi":
116 return e.agentCmd(ctx, cmd, opts, args)
117 case "shell", "sh":
118 return e.shellCmd(ctx, opts, args)
119 case "run":
120 return e.runCmd(ctx, opts, args)
121 case "setup":
122 return e.setupCmd(ctx, append(lead, args...))
123 case "doctor":
124 return e.doctorCmd(ctx, args)
125 case "install":
126 return e.installCmd(ctx, args)
127 case "uninstall":
128 return e.uninstallCmd(args)
129 case "code":
130 return e.codeCmd(ctx, opts, args)
131 default:
132 fmt.Fprintf(io.Stderr, "Unknown command: %s\nRun 'agent-vm help' for usage.\n", cmd)
133 return exitUsage
134 }
135}
136
137var versionRe = regexp.MustCompile(`^[0-9]+(\.[0-9]+)*(-[A-Za-z0-9.]+)?$`)
138
139// `version [--min X.Y.Z]`: silent and 0 when this agent-vm is at least
140// X.Y.Z, an error and 1 when older, 2 when the call itself is wrong.
141func versionCmd(args []string, io IO) int {
142 if len(args) == 0 {
143 fmt.Fprintln(io.Stdout, version.Version)
144 return 0
145 }
146 want := ""
147 for len(args) > 0 {
148 switch a := args[0]; {
149 case a == "--min":
150 if len(args) < 2 {
151 fmt.Fprintln(io.Stderr, "Error: --min needs a version (e.g. --min 0.2.0)")
152 return 2
153 }
154 want, args = args[1], args[2:]
155 case strings.HasPrefix(a, "--min="):
156 want, args = strings.TrimPrefix(a, "--min="), args[1:]
157 default:
158 fmt.Fprintf(io.Stderr, "Error: unknown option for version: %s\n", a)
159 return 2
160 }
161 }
162 if !versionRe.MatchString(want) {
163 fmt.Fprintf(io.Stderr, "Error: --min expects a version like 1.2.3 (got: '%s')\n", want)
164 return 2
165 }
166 if version.AtLeast(version.Version, want) {
167 return 0
168 }
169 fmt.Fprintf(io.Stderr, "Error: agent-vm %s is older than the required %s.\n", version.Version, want)
170 fmt.Fprintf(io.Stderr, " Update it: %s\n", updateCommand())
171 return 1
172}
173
174// updateCommand is how this copy of agent-vm is updated: by what installed
175// it. The installer's copy would otherwise land in ~/.local/bin, ahead of a
176// package's on the PATH, and hide its updates.
177func updateCommand() string {
178 exe, err := self()
179 if err != nil {
180 exe = ""
181 }
182 return updateCommandFor(exe)
183}
184
185func updateCommandFor(exe string) string {
186 switch {
187 case strings.HasPrefix(exe, "/nix/store/"):
188 return "with Nix, as installed (nix profile upgrade agent-vm, or the flake input)"
189 case strings.Contains(exe, "/Cellar/agent-vm/"):
190 return "brew upgrade agent-vm"
191 case exe == "/usr/bin/agent-vm":
192 return "install the .deb or .rpm of https://github.com/sylvinus/agent-vm/releases/latest"
193 }
194 return "curl -fsSL https://www.agent-vm.org/install.sh | sh"
195}
196
197// `name [dir]`: the VM name of a directory, the current one by default.
198func (e *app) nameCmd(args []string, io IO) int {
199 dir := ""
200 if len(args) > 0 {
201 dir = args[0]
202 }
203 abs, err := vmname.AbsDir(dir)
204 if err != nil {
205 fmt.Fprintf(io.Stderr, "Error: %v\n", err)
206 return 1
207 }
208 fmt.Fprintln(io.Stdout, e.instanceName(abs))
209 return 0
210}
211
212// Stdio is the process's own.
213func Stdio() IO {
214 return IO{Stdin: os.Stdin, Stdout: os.Stdout, Stderr: os.Stderr}
215}