From 87752492d0dc7df3cf78011d5ce315a3eb0cad51 Mon Sep 17 00:00:00 2001 From: bndw Date: Fri, 23 Jan 2026 21:52:50 -0800 Subject: Restructure CLI with Cobra Replace custom switch-based routing with Cobra for cleaner command hierarchy. Reorganize commands into logical groups: - Root command handles deployment (--binary, --static, --domain, etc.) - App management at top level: list, logs, status, restart, remove - env subcommand group: list, set, unset - host subcommand group: init, status, update, ssh - Standalone: ui (renamed from webui), version Add version command with ldflags support for build info. --- cmd/deploy/main.go | 130 +++++++++++++++++++++-------------------------------- 1 file changed, 51 insertions(+), 79 deletions(-) (limited to 'cmd/deploy/main.go') diff --git a/cmd/deploy/main.go b/cmd/deploy/main.go index 51439dd..ad61523 100644 --- a/cmd/deploy/main.go +++ b/cmd/deploy/main.go @@ -1,93 +1,65 @@ package main import ( - "fmt" "os" -) - -func main() { - if len(os.Args) < 2 { - printUsage() - os.Exit(1) - } - - command := os.Args[1] - - switch command { - case "init": - runInit(os.Args[2:]) - case "list": - runList(os.Args[2:]) - case "rm", "remove": - runRemove(os.Args[2:]) - case "logs": - runLogs(os.Args[2:]) - case "status": - runStatus(os.Args[2:]) - case "restart": - runRestart(os.Args[2:]) - case "env": - runEnv(os.Args[2:]) - case "webui": - runWebUI(os.Args[2:]) - case "vps": - runVPS(os.Args[2:]) - case "vps-update": - runUpdate(os.Args[2:]) - case "vps-ssh": - runSSH(os.Args[2:]) - case "help", "--help", "-h": - printUsage() - default: - // Default action is deploy - pass all args including the first one - runDeploy(os.Args[1:]) - } -} -func printUsage() { - usage := `deploy - Deploy Go apps and static sites to a VPS with automatic HTTPS - -USAGE: - deploy [flags] Deploy an app or static site - deploy [flags] Run a subcommand - -COMMANDS: - init Initialize a fresh VPS (one-time setup) - list List all deployed apps and sites - rm Remove a deployment - logs View logs for a deployment - status Check status of a deployment - restart Restart a deployment - env Manage environment variables - vps Show VPS health (uptime, disk, memory, load) - vps-update Update VPS packages (apt update && upgrade) - vps-ssh Open an interactive SSH session - webui Launch web UI to manage deployments + "github.com/bdw/deploy/cmd/deploy/env" + "github.com/bdw/deploy/cmd/deploy/host" + "github.com/spf13/cobra" +) -FLAGS: - Run 'deploy -h' or 'deploy -h' for flags +var ( + // Persistent flags + hostFlag string -EXAMPLES: - # Initialize VPS (sets it as default host) - deploy init --host user@vps-ip + // Version info (set via ldflags) + version = "dev" + commit = "none" + date = "unknown" +) - # Deploy Go app - deploy --binary ./myapp --domain api.example.com +var rootCmd = &cobra.Command{ + Use: "deploy", + Short: "Deploy Go apps and static sites to a VPS with automatic HTTPS", + Long: `deploy - Deploy Go apps and static sites to a VPS with automatic HTTPS - # Deploy static site - deploy --static --dir ./dist --domain example.com +A CLI tool for deploying applications and static sites to a VPS. +Uses Caddy for automatic HTTPS and systemd for service management.`, + RunE: runDeploy, + SilenceUsage: true, + SilenceErrors: true, +} - # List deployments - deploy list +func init() { + // Persistent flags available to all subcommands + rootCmd.PersistentFlags().StringVar(&hostFlag, "host", "", "VPS host (SSH config alias or user@host)") - # View logs - deploy logs myapp + // Root command (deploy) flags + rootCmd.Flags().String("binary", "", "Path to Go binary (for app deployment)") + rootCmd.Flags().Bool("static", false, "Deploy as static site") + rootCmd.Flags().String("dir", ".", "Directory to deploy (for static sites)") + rootCmd.Flags().String("domain", "", "Domain name (required)") + rootCmd.Flags().String("name", "", "App name (default: inferred from binary or directory)") + rootCmd.Flags().Int("port", 0, "Port override (default: auto-allocate)") + rootCmd.Flags().StringArray("env", nil, "Environment variable (KEY=VALUE, can be specified multiple times)") + rootCmd.Flags().String("env-file", "", "Path to .env file") + rootCmd.Flags().String("args", "", "Arguments to pass to binary") + rootCmd.Flags().StringArray("file", nil, "Config file to upload to working directory (can be specified multiple times)") - # Check VPS health - deploy vps + // Add subcommands + rootCmd.AddCommand(listCmd) + rootCmd.AddCommand(logsCmd) + rootCmd.AddCommand(statusCmd) + rootCmd.AddCommand(restartCmd) + rootCmd.AddCommand(removeCmd) + rootCmd.AddCommand(env.Cmd) + rootCmd.AddCommand(host.Cmd) + rootCmd.AddCommand(uiCmd) + rootCmd.AddCommand(versionCmd) +} - # Update VPS packages - deploy vps-update -` - fmt.Fprint(os.Stderr, usage) +func main() { + if err := rootCmd.Execute(); err != nil { + os.Exit(1) + } } -- cgit v1.2.3