From 9bd149e6dddf419c0277ef0a02fce36abba20c09 Mon Sep 17 00:00:00 2001 From: Finessse Date: Wed, 17 Dec 2025 19:11:25 +0700 Subject: [PATCH] cmd: add cliproxyctl --- cmd/cliproxyctl/main.go | 86 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 cmd/cliproxyctl/main.go diff --git a/cmd/cliproxyctl/main.go b/cmd/cliproxyctl/main.go new file mode 100644 index 00000000..48ffbdc4 --- /dev/null +++ b/cmd/cliproxyctl/main.go @@ -0,0 +1,86 @@ +package main + +import ( + "encoding/json" + "flag" + "fmt" + "os" + "strings" + + "github.com/router-for-me/CLIProxyAPI/v6/internal/desktopctl" +) + +func main() { + if len(os.Args) < 2 { + usage() + os.Exit(2) + } + cmd := strings.ToLower(strings.TrimSpace(os.Args[1])) + + fs := flag.NewFlagSet("cliproxyctl "+cmd, flag.ExitOnError) + configPath := fs.String("config", "", "Path to config.yaml") + repoRoot := fs.String("repo", "", "Repo root (used to locate bin/ and logs/)") + exePath := fs.String("exe", "", "Path to CLIProxyAPI binary") + jsonOut := fs.Bool("json", true, "Output JSON when applicable") + _ = fs.Parse(os.Args[2:]) + + switch cmd { + case "status": + st, err := desktopctl.StatusFor(*configPath) + if err != nil { + fatal(err) + } + printStatus(st, *jsonOut) + case "start": + st, err := desktopctl.Start(desktopctl.StartOptions{RepoRoot: *repoRoot, ConfigPath: *configPath, ExePath: *exePath}) + if err != nil { + fatal(err) + } + printStatus(st, *jsonOut) + case "stop": + if err := desktopctl.Stop(desktopctl.StopOptions{}); err != nil { + fatal(err) + } + fmt.Println("stopped") + case "restart": + st, err := desktopctl.Restart(desktopctl.StartOptions{RepoRoot: *repoRoot, ConfigPath: *configPath, ExePath: *exePath}) + if err != nil { + fatal(err) + } + printStatus(st, *jsonOut) + case "open-ui": + if err := desktopctl.OpenManagementUI(*configPath); err != nil { + fatal(err) + } + case "open-logs": + if err := desktopctl.OpenLogsFolder(*repoRoot, *configPath); err != nil { + fatal(err) + } + default: + usage() + os.Exit(2) + } +} + +func usage() { + fmt.Fprintln(os.Stderr, "Usage: cliproxyctl [flags]") + fmt.Fprintln(os.Stderr, "Flags: -config -repo -exe ") +} + +func fatal(err error) { + fmt.Fprintln(os.Stderr, "error:", err) + os.Exit(1) +} + +func printStatus(st desktopctl.Status, jsonOut bool) { + if jsonOut { + data, _ := json.MarshalIndent(st, "", " ") + fmt.Println(string(data)) + return + } + if st.Running { + fmt.Printf("running pid=%d port=%d base=%s\n", st.PID, st.Port, st.BaseURL) + } else { + fmt.Printf("stopped last_error=%s\n", st.LastError) + } +}