summaryrefslogtreecommitdiffstats
path: root/cmd/deploy/list.go
blob: ab19a12e0a3d0a004daecc2f138afed8cb5f60ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main

import (
	"fmt"
	"os"
	"text/tabwriter"

	"github.com/bdw/deploy/internal/state"
	"github.com/spf13/cobra"
)

var listCmd = &cobra.Command{
	Use:   "list",
	Short: "List all deployed apps and sites",
	RunE:  runList,
}

func runList(cmd *cobra.Command, args []string) error {
	st, err := state.Load()
	if err != nil {
		return fmt.Errorf("error loading state: %w", err)
	}

	host := hostFlag
	if host == "" {
		host = st.GetDefaultHost()
	}

	if host == "" {
		return fmt.Errorf("--host is required")
	}

	apps := st.ListApps(host)
	if len(apps) == 0 {
		fmt.Printf("No deployments found for %s\n", host)
		return nil
	}

	w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
	fmt.Fprintln(w, "NAME\tTYPE\tDOMAIN\tPORT")
	for name, app := range apps {
		port := ""
		if app.Type == "app" {
			port = fmt.Sprintf(":%d", app.Port)
		}
		fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", name, app.Type, app.Domain, port)
	}
	w.Flush()
	return nil
}