summaryrefslogtreecommitdiffstats
path: root/cmd/ship/remove.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/ship/remove.go')
-rw-r--r--cmd/ship/remove.go83
1 files changed, 83 insertions, 0 deletions
diff --git a/cmd/ship/remove.go b/cmd/ship/remove.go
new file mode 100644
index 0000000..922eb8f
--- /dev/null
+++ b/cmd/ship/remove.go
@@ -0,0 +1,83 @@
1package main
2
3import (
4 "fmt"
5
6 "github.com/bdw/ship/internal/ssh"
7 "github.com/bdw/ship/internal/state"
8 "github.com/spf13/cobra"
9)
10
11var removeCmd = &cobra.Command{
12 Use: "remove <app>",
13 Aliases: []string{"rm"},
14 Short: "Remove a deployment",
15 Args: cobra.ExactArgs(1),
16 RunE: runRemove,
17}
18
19func runRemove(cmd *cobra.Command, args []string) error {
20 name := args[0]
21
22 st, err := state.Load()
23 if err != nil {
24 return fmt.Errorf("error loading state: %w", err)
25 }
26
27 host := hostFlag
28 if host == "" {
29 host = st.GetDefaultHost()
30 }
31
32 if host == "" {
33 return fmt.Errorf("--host is required")
34 }
35
36 app, err := st.GetApp(host, name)
37 if err != nil {
38 return err
39 }
40
41 fmt.Printf("Removing deployment: %s\n", name)
42
43 client, err := ssh.Connect(host)
44 if err != nil {
45 return fmt.Errorf("error connecting to VPS: %w", err)
46 }
47 defer client.Close()
48
49 if app.Type == "app" {
50 fmt.Println("-> Stopping service...")
51 client.RunSudo(fmt.Sprintf("systemctl stop %s", name))
52 client.RunSudo(fmt.Sprintf("systemctl disable %s", name))
53
54 client.RunSudo(fmt.Sprintf("rm -f /etc/systemd/system/%s.service", name))
55 client.RunSudo("systemctl daemon-reload")
56
57 client.RunSudo(fmt.Sprintf("rm -f /usr/local/bin/%s", name))
58 client.RunSudo(fmt.Sprintf("rm -rf /var/lib/%s", name))
59 client.RunSudo(fmt.Sprintf("rm -f /etc/ship/env/%s.env", name))
60 client.RunSudo(fmt.Sprintf("userdel %s", name))
61 } else {
62 fmt.Println("-> Removing files...")
63 client.RunSudo(fmt.Sprintf("rm -rf /var/www/%s", name))
64 }
65
66 fmt.Println("-> Removing Caddy config...")
67 client.RunSudo(fmt.Sprintf("rm -f /etc/caddy/sites-enabled/%s.caddy", name))
68
69 fmt.Println("-> Reloading Caddy...")
70 if _, err := client.RunSudo("systemctl reload caddy"); err != nil {
71 fmt.Printf("Warning: Error reloading Caddy: %v\n", err)
72 }
73
74 if err := st.RemoveApp(host, name); err != nil {
75 return fmt.Errorf("error updating state: %w", err)
76 }
77 if err := st.Save(); err != nil {
78 return fmt.Errorf("error saving state: %w", err)
79 }
80
81 fmt.Println("Deployment removed successfully")
82 return nil
83}