summaryrefslogtreecommitdiffstats
path: root/cmd/ship/env/unset.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/ship/env/unset.go')
-rw-r--r--cmd/ship/env/unset.go92
1 files changed, 92 insertions, 0 deletions
diff --git a/cmd/ship/env/unset.go b/cmd/ship/env/unset.go
new file mode 100644
index 0000000..7d9a141
--- /dev/null
+++ b/cmd/ship/env/unset.go
@@ -0,0 +1,92 @@
1package env
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 unsetCmd = &cobra.Command{
12 Use: "unset <app> KEY...",
13 Short: "Unset environment variable(s)",
14 Long: "Remove one or more environment variables from an app.",
15 Args: cobra.MinimumNArgs(2),
16 RunE: runUnset,
17}
18
19func runUnset(cmd *cobra.Command, args []string) error {
20 name := args[0]
21 keys := args[1:]
22
23 st, err := state.Load()
24 if err != nil {
25 return fmt.Errorf("error loading state: %w", err)
26 }
27
28 host, _ := cmd.Flags().GetString("host")
29 if host == "" {
30 host = st.GetDefaultHost()
31 }
32
33 if host == "" {
34 return fmt.Errorf("--host is required")
35 }
36
37 app, err := st.GetApp(host, name)
38 if err != nil {
39 return err
40 }
41
42 if app.Type != "app" {
43 return fmt.Errorf("env is only available for apps, not static sites")
44 }
45
46 if app.Env == nil {
47 return fmt.Errorf("no environment variables set")
48 }
49
50 changed := false
51 for _, key := range keys {
52 if _, exists := app.Env[key]; exists {
53 delete(app.Env, key)
54 changed = true
55 fmt.Printf("Unset %s\n", key)
56 } else {
57 fmt.Printf("Warning: %s not found\n", key)
58 }
59 }
60
61 if !changed {
62 return nil
63 }
64
65 if err := st.Save(); err != nil {
66 return fmt.Errorf("error saving state: %w", err)
67 }
68
69 client, err := ssh.Connect(host)
70 if err != nil {
71 return fmt.Errorf("error connecting to VPS: %w", err)
72 }
73 defer client.Close()
74
75 fmt.Println("-> Updating environment file on VPS...")
76 envFilePath := fmt.Sprintf("/etc/ship/env/%s.env", name)
77 envContent := ""
78 for k, v := range app.Env {
79 envContent += fmt.Sprintf("%s=%s\n", k, v)
80 }
81 if err := client.WriteSudoFile(envFilePath, envContent); err != nil {
82 return fmt.Errorf("error updating env file: %w", err)
83 }
84
85 fmt.Println("-> Restarting service...")
86 if _, err := client.RunSudo(fmt.Sprintf("systemctl restart %s", name)); err != nil {
87 return fmt.Errorf("error restarting service: %w", err)
88 }
89
90 fmt.Println("Environment variables updated successfully")
91 return nil
92}