package cli import ( "context" "encoding/json" "errors" "flag" "io" "os" "github.com/Nwflower/reims-go/internal/host" ) // elevateExec runs one whitelisted action; tests replace it. var elevateExec = host.ExecuteElevatedAction // cmdElevate is the hidden `__elevate` subcommand the desktop app starts through // a UAC prompt. It runs before any data folder is opened, so an elevated run // never creates or changes files in the user's data folder. It is not listed // in the usage text. func cmdElevate(args []string) error { fs := flag.NewFlagSet("__elevate", flag.ContinueOnError) fs.SetOutput(io.Discard) action := fs.String("action", "", "whitelisted elevation action") user := fs.String("user", "", "account the action applies to") resultFile := fs.String("result-file", "", "file the JSON result is written to") if err := fs.Parse(args); err != nil { return writeElevateFailure(*resultFile, *action, err) } if _, ok := host.LookupElevationAction(*action); !ok { return writeElevateFailure(*resultFile, *action, errors.New("unsupported elevation action")) } res, err := elevateExec(context.Background(), *action, *user) if err != nil { if res.Action == "" { res.Action = *action } if res.Error == "" { res.Error = err.Error() } res.OK = false } if werr := writeElevateResult(*resultFile, res); werr != nil && err == nil { return werr } return err } func writeElevateFailure(path, action string, err error) error { _ = writeElevateResult(path, host.ElevationResult{Action: action, Error: err.Error()}) return err } // writeElevateResult fills the file in place: the caller created it, and // replacing it would drop the access the caller holds. func writeElevateResult(path string, res host.ElevationResult) error { if path == "" { return nil } data, err := json.Marshal(res) if err != nil { return err } return os.WriteFile(path, data, 0o600) }