跳到主要内容

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

浏览文件

runscript.go

67 行 · 2.0 KB历史下载
1// Package runscript is the runtime scripts: ~/.agent-vm/runtime.sh and a
2// project's .agent-vm.runtime.sh, run in the VM on every command that enters
3// it, under the shell their shebang names.
4package runscript
5
6import (
7 "os"
8 "strings"
9
10 "github.com/sylvinus/agent-vm/internal/env"
11)
12
13// ShebangAWK reads the interpreter a script asks for: bash or sh, zsh
14// otherwise. Only shells: the script is fed on stdin, read with -s. The
15// program is the shebang's first word ("#!/bin/bash -e"), or after env, its
16// options and assignments ("#!/usr/bin/env -S bash -e"). The VM runs it on a
17// project's script, which only the VM reads: keep it as it is.
18const ShebangAWK = `
19 NR == 1 {
20 sub(/\r$/, "")
21 if (substr($0, 1, 2) == "#!") {
22 n = split(substr($0, 3), w)
23 for (i = 1; i < n; i++)
24 if (w[i] != "env" && w[i] !~ /\/env$/ && w[i] !~ /^-/ && w[i] !~ /=/) break
25 p = w[i]
26 sub(/.*\//, "", p)
27 }
28 exit
29 }
30 END { print ((p == "bash" || p == "sh") ? p : "zsh") }
31`
32
33// StripCRAWK drops the CR ending each line, in the VM.
34const StripCRAWK = `{ sub(/\r$/, ""); print }`
35
36// Interpreter is the shell content's shebang asks for, as ShebangAWK reads
37// it.
38func Interpreter(content string) string {
39 first, _, _ := strings.Cut(content, "\n")
40 first = strings.TrimSuffix(first, "\r")
41 p := ""
42 if rest, ok := strings.CutPrefix(first, "#!"); ok {
43 w := strings.FieldsFunc(rest, func(r rune) bool { return r == ' ' || r == '\t' })
44 i := 0
45 for ; i < len(w)-1; i++ {
46 if w[i] != "env" && !strings.HasSuffix(w[i], "/env") && !strings.HasPrefix(w[i], "-") && !strings.Contains(w[i], "=") {
47 break
48 }
49 }
50 if i < len(w) {
51 p = w[i][strings.LastIndexByte(w[i], '/')+1:]
52 }
53 }
54 if p == "bash" || p == "sh" {
55 return p
56 }
57 return "zsh"
58}
59
60// ProjectPath is the project dir's runtime script (see env.ProjectPath).
61func ProjectPath(dir string) string {
62 p := os.Getenv("AGENT_VM_PROJECT_RUNTIME")
63 if p == "" {
64 p = ".agent-vm.runtime.sh"
65 }
66 return env.ProjectPath(dir, p)
67}