diff options
| author | bndw <ben@bdw.to> | 2026-01-23 21:52:50 -0800 |
|---|---|---|
| committer | bndw <ben@bdw.to> | 2026-01-23 21:52:50 -0800 |
| commit | 87752492d0dc7df3cf78011d5ce315a3eb0cad51 (patch) | |
| tree | 76843c127fece33f5c28dd7bd533044043478825 /cmd/deploy/env/unset.go | |
| parent | 57eb67df265a7a6bb544cde83a3be5eadf53fdf2 (diff) | |
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.
Diffstat (limited to 'cmd/deploy/env/unset.go')
| -rw-r--r-- | cmd/deploy/env/unset.go | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/cmd/deploy/env/unset.go b/cmd/deploy/env/unset.go new file mode 100644 index 0000000..65a8986 --- /dev/null +++ b/cmd/deploy/env/unset.go | |||
| @@ -0,0 +1,92 @@ | |||
| 1 | package env | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "fmt" | ||
| 5 | |||
| 6 | "github.com/bdw/deploy/internal/ssh" | ||
| 7 | "github.com/bdw/deploy/internal/state" | ||
| 8 | "github.com/spf13/cobra" | ||
| 9 | ) | ||
| 10 | |||
| 11 | var 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 | |||
| 19 | func 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/deploy/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 | } | ||
