diff --git a/cmd/restray/cli.go b/cmd/restray/cli.go index 24fad6e..58f85fd 100644 --- a/cmd/restray/cli.go +++ b/cmd/restray/cli.go @@ -26,6 +26,8 @@ Commands: op mount [-p name] Mount repository (Ctrl+C to unmount) op pre-hook [-p name] Run pre-hook command op post-hook [-p name] Run post-hook command + configure config Create/open config file in editor + configure env [-p name] Create/open env file in editor Flags: -c, --config dir Config directory (default: $RESTRAY_CONFIG or OS default) @@ -63,6 +65,58 @@ func cliParseFlags(args []string) (profileName string, remaining []string) { return profileName, fs.Args() } +func editorCmd() string { + if e := os.Getenv("VISUAL"); e != "" { + return e + } + if e := os.Getenv("EDITOR"); e != "" { + return e + } + if runtime.GOOS == "windows" { + return "notepad" + } + return "vi" +} + +func cliEdit(path string) int { + cmd := exec.Command(editorCmd(), path) + cmd.Stdin = os.Stdin + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + if err := cmd.Run(); err != nil { + if code := exitCode(err); code > 0 { + return code + } + return 1 + } + return 0 +} + +func cliConfigure(args []string) int { + if len(args) == 0 { + fmt.Fprintln(os.Stderr, `Usage: restray configure [-p name]`) + return 1 + } + switch args[0] { + case "config": + ensureConfigFile() + return cliEdit(configPath()) + case "env": + name, _ := cliParseFlags(args[1:]) + cfg := loadConfig() + _, prof, err := resolveProfile(cfg, name) + if err != nil { + fmt.Fprintln(os.Stderr, "error:", err) + return 1 + } + ensureEnvFile(prof.EnvFile) + return cliEdit(prof.EnvFile) + default: + fmt.Fprintf(os.Stderr, "unknown configure target: %s\n", args[0]) + return 1 + } +} + func cliRunRestic(prof Profile, args ...string) int { p, _ := findRestic() if p == "" { @@ -310,6 +364,9 @@ func runCLI(args []string) int { return 1 } + case "configure": + return cliConfigure(args[1:]) + case "-h", "--help", "help": cliUsage() return 0