feat(device): shell 支持配置绝对路径与 busybox,默认仍为 sh/cmd
不做自动探测(各系统 shell 变体太多,交给配置);配了路径就按 POSIX 语义 -c 执行, busybox 走 sh -c;上报给 core 的 shell 归成短标签(bash/busybox/sh/cmd/powershell)。
This commit is contained in:
@@ -63,6 +63,20 @@ sh scripts/build.sh all
|
||||
|
||||
优先级:**命令行显式参数 > 环境变量 `NODE_TOKEN`(仅 token)> 配置文件 > 默认值**。
|
||||
|
||||
### shell 用哪个
|
||||
|
||||
默认:macOS / Linux 用 `/bin/sh`(固定,不读 `$SHELL`——用户把默认 shell 设成 fish 时,
|
||||
模型写的 POSIX 语法会全废);Windows 用 `cmd`。
|
||||
|
||||
想换就在 `config.json` 里写 `shell`,**不做自动探测**:装了 Git Bash / busybox 就写路径,
|
||||
没装就保持默认。
|
||||
|
||||
```json
|
||||
{ "shell": "C:\\Program Files\\Git\\bin\\bash.exe" }
|
||||
```
|
||||
|
||||
报给 core 的是短标签(`sh` / `bash` / `busybox` / `cmd` / `powershell`),同时带上解释器路径与版本。
|
||||
|
||||
| 参数 | 说明 |
|
||||
|---|---|
|
||||
| `-server` | core 的接入地址,`wss://…/dev/ws` |
|
||||
|
||||
@@ -171,7 +171,7 @@ func main() {
|
||||
host = *name
|
||||
}
|
||||
|
||||
log.Printf("agent-device %s 启动:root=%s shell=%s → %s", version, r.root, r.shell, *server)
|
||||
log.Printf("agent-device %s 启动:root=%s shell=%s → %s", version, r.root, shellKind(r.shell), *server)
|
||||
shPath, shVer := shellInfo(r.shell)
|
||||
log.Printf("shell:%s(%s %s)", r.shell, shPath, shVer)
|
||||
cfg := wsnode.Config{URL: *server, Token: *token, Insecure: *insecure, FixClock: *fixClock}
|
||||
@@ -181,7 +181,7 @@ func main() {
|
||||
if err := c.Send(proto.Msg{
|
||||
T: proto.TKindHello, Ver: version, Name: host,
|
||||
OS: runtime.GOOS, Arch: runtime.GOARCH,
|
||||
Shell: r.shell, ShellPath: shPath, ShellVer: shVer, Root: r.root,
|
||||
Shell: shellKind(r.shell), ShellPath: shPath, ShellVer: shVer, Root: r.root,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -361,6 +361,9 @@ func flatten(env map[string]string) []string {
|
||||
return out
|
||||
}
|
||||
|
||||
// defaultShell 选默认解释器。POSIX 上固定 /bin/sh(不读 $SHELL:用户把 shell 设成 fish
|
||||
// 之后模型写的 POSIX 语法全废);Windows 默认 cmd,想用 git-bash / busybox 就在
|
||||
// config.json 里写 "shell": "C:\\Program Files\\Git\\bin\\bash.exe"(或 PATH 里的名字)。
|
||||
func defaultShell() string {
|
||||
if runtime.GOOS == "windows" {
|
||||
return "cmd"
|
||||
@@ -368,13 +371,39 @@ func defaultShell() string {
|
||||
return "sh"
|
||||
}
|
||||
|
||||
// shellKind 把路径/命令名归成一个短标签给 core 看(hello 的 shell 字段)。
|
||||
func shellKind(p string) string {
|
||||
b := strings.ToLower(filepath.Base(p))
|
||||
switch {
|
||||
case strings.Contains(b, "busybox"):
|
||||
return "busybox"
|
||||
case strings.Contains(b, "bash"):
|
||||
return "bash"
|
||||
case b == "sh" || b == "sh.exe" || b == "/bin/sh":
|
||||
return "sh"
|
||||
case strings.Contains(b, "powershell"), b == "pwsh", b == "pwsh.exe":
|
||||
return "powershell"
|
||||
case b == "cmd" || b == "cmd.exe":
|
||||
return "cmd"
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func shellCmd(name string) (string, []string) {
|
||||
switch name {
|
||||
case "cmd":
|
||||
return "cmd", []string{"/c"}
|
||||
case "powershell", "pwsh":
|
||||
return name, []string{"-NoProfile", "-NonInteractive", "-Command"}
|
||||
default:
|
||||
}
|
||||
// busybox 的子命令形式:busybox sh -c "…"
|
||||
if strings.Contains(strings.ToLower(filepath.Base(name)), "busybox") {
|
||||
return name, []string{"sh", "-c"}
|
||||
}
|
||||
// 带路径的(git-bash 绝对路径、/bin/sh)直接当 POSIX shell 用
|
||||
if strings.ContainsAny(name, `\/`) {
|
||||
return name, []string{"-c"}
|
||||
}
|
||||
if runtime.GOOS == "windows" {
|
||||
return "cmd", []string{"/c"}
|
||||
}
|
||||
@@ -383,7 +412,6 @@ func shellCmd(name string) (string, []string) {
|
||||
}
|
||||
return "sh", []string{"-c"}
|
||||
}
|
||||
}
|
||||
|
||||
// shellInfo 探测实际解释器与版本。这是模型写命令的唯一入口,所以路径要解开符号链接
|
||||
// (/bin/sh 可能是 dash / bash / busybox),版本拿不到就留空(dash、busybox 不支持 --version)。
|
||||
|
||||
Reference in New Issue
Block a user