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/remove.go | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 cmd/deploy/remove.go (limited to 'cmd/deploy/remove.go') diff --git a/cmd/deploy/remove.go b/cmd/deploy/remove.go new file mode 100644 index 0000000..5a98bf3 --- /dev/null +++ b/cmd/deploy/remove.go @@ -0,0 +1,83 @@ +package main + +import ( + "fmt" + + "github.com/bdw/deploy/internal/ssh" + "github.com/bdw/deploy/internal/state" + "github.com/spf13/cobra" +) + +var removeCmd = &cobra.Command{ + Use: "remove ", + Aliases: []string{"rm"}, + Short: "Remove a deployment", + Args: cobra.ExactArgs(1), + RunE: runRemove, +} + +func runRemove(cmd *cobra.Command, args []string) error { + name := args[0] + + st, err := state.Load() + if err != nil { + return fmt.Errorf("error loading state: %w", err) + } + + host := hostFlag + if host == "" { + host = st.GetDefaultHost() + } + + if host == "" { + return fmt.Errorf("--host is required") + } + + app, err := st.GetApp(host, name) + if err != nil { + return err + } + + fmt.Printf("Removing deployment: %s\n", name) + + client, err := ssh.Connect(host) + if err != nil { + return fmt.Errorf("error connecting to VPS: %w", err) + } + defer client.Close() + + if app.Type == "app" { + fmt.Println("-> Stopping service...") + client.RunSudo(fmt.Sprintf("systemctl stop %s", name)) + client.RunSudo(fmt.Sprintf("systemctl disable %s", name)) + + client.RunSudo(fmt.Sprintf("rm -f /etc/systemd/system/%s.service", name)) + client.RunSudo("systemctl daemon-reload") + + client.RunSudo(fmt.Sprintf("rm -f /usr/local/bin/%s", name)) + client.RunSudo(fmt.Sprintf("rm -rf /var/lib/%s", name)) + client.RunSudo(fmt.Sprintf("rm -f /etc/deploy/env/%s.env", name)) + client.RunSudo(fmt.Sprintf("userdel %s", name)) + } else { + fmt.Println("-> Removing files...") + client.RunSudo(fmt.Sprintf("rm -rf /var/www/%s", name)) + } + + fmt.Println("-> Removing Caddy config...") + client.RunSudo(fmt.Sprintf("rm -f /etc/caddy/sites-enabled/%s.caddy", name)) + + fmt.Println("-> Reloading Caddy...") + if _, err := client.RunSudo("systemctl reload caddy"); err != nil { + fmt.Printf("Warning: Error reloading Caddy: %v\n", err) + } + + if err := st.RemoveApp(host, name); err != nil { + return fmt.Errorf("error updating state: %w", err) + } + if err := st.Save(); err != nil { + return fmt.Errorf("error saving state: %w", err) + } + + fmt.Println("Deployment removed successfully") + return nil +} -- cgit v1.2.3