diff options
Diffstat (limited to 'cmd/ship/env/list.go')
| -rw-r--r-- | cmd/ship/env/list.go | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/cmd/ship/env/list.go b/cmd/ship/env/list.go new file mode 100644 index 0000000..ad76eb6 --- /dev/null +++ b/cmd/ship/env/list.go | |||
| @@ -0,0 +1,69 @@ | |||
| 1 | package env | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "fmt" | ||
| 5 | "strings" | ||
| 6 | |||
| 7 | "github.com/bdw/ship/internal/state" | ||
| 8 | "github.com/spf13/cobra" | ||
| 9 | ) | ||
| 10 | |||
| 11 | var listCmd = &cobra.Command{ | ||
| 12 | Use: "list <app>", | ||
| 13 | Short: "List environment variables for an app", | ||
| 14 | Args: cobra.ExactArgs(1), | ||
| 15 | RunE: runList, | ||
| 16 | } | ||
| 17 | |||
| 18 | func runList(cmd *cobra.Command, args []string) error { | ||
| 19 | name := args[0] | ||
| 20 | |||
| 21 | st, err := state.Load() | ||
| 22 | if err != nil { | ||
| 23 | return fmt.Errorf("error loading state: %w", err) | ||
| 24 | } | ||
| 25 | |||
| 26 | host, _ := cmd.Flags().GetString("host") | ||
| 27 | if host == "" { | ||
| 28 | host = st.GetDefaultHost() | ||
| 29 | } | ||
| 30 | |||
| 31 | if host == "" { | ||
| 32 | return fmt.Errorf("--host is required") | ||
| 33 | } | ||
| 34 | |||
| 35 | app, err := st.GetApp(host, name) | ||
| 36 | if err != nil { | ||
| 37 | return err | ||
| 38 | } | ||
| 39 | |||
| 40 | if app.Type != "app" { | ||
| 41 | return fmt.Errorf("env is only available for apps, not static sites") | ||
| 42 | } | ||
| 43 | |||
| 44 | fmt.Printf("Environment variables for %s:\n\n", name) | ||
| 45 | if len(app.Env) == 0 { | ||
| 46 | fmt.Println(" (none)") | ||
| 47 | } else { | ||
| 48 | for k, v := range app.Env { | ||
| 49 | display := v | ||
| 50 | if isSensitive(k) { | ||
| 51 | display = "***" | ||
| 52 | } | ||
| 53 | fmt.Printf(" %s=%s\n", k, display) | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | return nil | ||
| 58 | } | ||
| 59 | |||
| 60 | func isSensitive(key string) bool { | ||
| 61 | key = strings.ToLower(key) | ||
| 62 | sensitiveWords := []string{"key", "secret", "password", "token", "api"} | ||
| 63 | for _, word := range sensitiveWords { | ||
| 64 | if strings.Contains(key, word) { | ||
| 65 | return true | ||
| 66 | } | ||
| 67 | } | ||
| 68 | return false | ||
| 69 | } | ||
