package action import ( "context" "fmt" "os" pwin "zerorigin/internal/platform/windows" ) func init() { Register("launch_application", func() Action { return &launchAction{} }) } // launchAction 启动指定程序。 // 安全约束(架构文档 §9.2):路径必须为绝对路径且文件存在; // 不使用 shell 拼接;结果与错误均记录,不记录敏感参数。 type launchAction struct{} func (a *launchAction) ID() string { return "launch_application" } func (a *launchAction) Name() string { return "启动程序" } func (a *launchAction) Schema() ActionSchema { return ActionSchema{Params: []ParamSpec{ {Name: "path", Label: "程序路径", Type: "string", Required: true, Help: `Windows 绝对路径,例如 C:\Windows\notepad.exe`}, {Name: "args", Label: "启动参数", Type: "string", Required: false, Default: "", Help: "传给程序的命令行参数(可选)"}, }} } func (a *launchAction) Validate(params map[string]any) error { path := GetString(params, "path") if path == "" { return fmt.Errorf("launch_application: missing param \"path\"") } if !filepathIsAbs(path) { return fmt.Errorf("launch_application: path must be absolute: %q", path) } fi, err := os.Stat(path) if err != nil { return fmt.Errorf("launch_application: %q: %v", path, err) } if fi.IsDir() { return fmt.Errorf("launch_application: %q is a directory, want executable", path) } return nil } func (a *launchAction) Execute(ctx context.Context, params map[string]any) error { path := GetString(params, "path") args := GetString(params, "args") return pwin.OpenPathWithArgs(path, args) } // filepathIsAbs 判断路径是否绝对路径(避免引入 filepath 包仅为此一处)。 func filepathIsAbs(p string) bool { if len(p) >= 2 { if p[1] == ':' && (p[0] >= 'A' && p[0] <= 'Z' || p[0] >= 'a' && p[0] <= 'z') { return true // C:\... / c:\... } if p[0] == '\\' && p[1] == '\\' { return true // \\server\share } } return false }