diff options
Diffstat (limited to 'cmd/deploy/list.go')
| -rw-r--r-- | cmd/deploy/list.go | 36 |
1 files changed, 17 insertions, 19 deletions
diff --git a/cmd/deploy/list.go b/cmd/deploy/list.go index ce1605b..ab19a12 100644 --- a/cmd/deploy/list.go +++ b/cmd/deploy/list.go | |||
| @@ -1,44 +1,41 @@ | |||
| 1 | package main | 1 | package main |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "flag" | ||
| 5 | "fmt" | 4 | "fmt" |
| 6 | "os" | 5 | "os" |
| 7 | "text/tabwriter" | 6 | "text/tabwriter" |
| 8 | 7 | ||
| 9 | "github.com/bdw/deploy/internal/state" | 8 | "github.com/bdw/deploy/internal/state" |
| 9 | "github.com/spf13/cobra" | ||
| 10 | ) | 10 | ) |
| 11 | 11 | ||
| 12 | func runList(args []string) { | 12 | var listCmd = &cobra.Command{ |
| 13 | fs := flag.NewFlagSet("list", flag.ExitOnError) | 13 | Use: "list", |
| 14 | host := fs.String("host", "", "VPS host (SSH config alias or user@host)") | 14 | Short: "List all deployed apps and sites", |
| 15 | fs.Parse(args) | 15 | RunE: runList, |
| 16 | } | ||
| 16 | 17 | ||
| 17 | // Load state | 18 | func runList(cmd *cobra.Command, args []string) error { |
| 18 | st, err := state.Load() | 19 | st, err := state.Load() |
| 19 | if err != nil { | 20 | if err != nil { |
| 20 | fmt.Fprintf(os.Stderr, "Error loading state: %v\n", err) | 21 | return fmt.Errorf("error loading state: %w", err) |
| 21 | os.Exit(1) | ||
| 22 | } | 22 | } |
| 23 | 23 | ||
| 24 | // Get host from flag or state default | 24 | host := hostFlag |
| 25 | if *host == "" { | 25 | if host == "" { |
| 26 | *host = st.GetDefaultHost() | 26 | host = st.GetDefaultHost() |
| 27 | } | 27 | } |
| 28 | 28 | ||
| 29 | if *host == "" { | 29 | if host == "" { |
| 30 | fmt.Fprintf(os.Stderr, "Error: --host is required\n") | 30 | return fmt.Errorf("--host is required") |
| 31 | fs.Usage() | ||
| 32 | os.Exit(1) | ||
| 33 | } | 31 | } |
| 34 | 32 | ||
| 35 | apps := st.ListApps(*host) | 33 | apps := st.ListApps(host) |
| 36 | if len(apps) == 0 { | 34 | if len(apps) == 0 { |
| 37 | fmt.Printf("No deployments found for %s\n", *host) | 35 | fmt.Printf("No deployments found for %s\n", host) |
| 38 | return | 36 | return nil |
| 39 | } | 37 | } |
| 40 | 38 | ||
| 41 | // Print table | ||
| 42 | w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0) | 39 | w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0) |
| 43 | fmt.Fprintln(w, "NAME\tTYPE\tDOMAIN\tPORT") | 40 | fmt.Fprintln(w, "NAME\tTYPE\tDOMAIN\tPORT") |
| 44 | for name, app := range apps { | 41 | for name, app := range apps { |
| @@ -49,4 +46,5 @@ func runList(args []string) { | |||
| 49 | fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", name, app.Type, app.Domain, port) | 46 | fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", name, app.Type, app.Domain, port) |
| 50 | } | 47 | } |
| 51 | w.Flush() | 48 | w.Flush() |
| 49 | return nil | ||
| 52 | } | 50 | } |
