|
5 | 5 | "fmt" |
6 | 6 | "time" |
7 | 7 |
|
| 8 | + "github.com/dustin/go-humanize" |
8 | 9 | "github.com/planetscale/cli/internal/cmdutil" |
9 | 10 | "github.com/planetscale/cli/internal/printer" |
10 | 11 | ps "github.com/planetscale/planetscale-go/planetscale" |
@@ -74,6 +75,67 @@ func toInfraPods(pods []*ps.BranchInfraPod) []*InfraPod { |
74 | 75 | return result |
75 | 76 | } |
76 | 77 |
|
| 78 | +type PostgresInfraRow struct { |
| 79 | + Component string `header:"component" json:"component"` |
| 80 | + Role string `header:"role" json:"role"` |
| 81 | + Size string `header:"size" json:"size"` |
| 82 | + AZ string `header:"az" json:"az"` |
| 83 | + Storage string `header:"storage" json:"storage"` |
| 84 | + Name string `header:"name" json:"name"` |
| 85 | + |
| 86 | + orig any |
| 87 | +} |
| 88 | + |
| 89 | +func (r *PostgresInfraRow) MarshalJSON() ([]byte, error) { |
| 90 | + return json.MarshalIndent(r.orig, "", " ") |
| 91 | +} |
| 92 | + |
| 93 | +func (r *PostgresInfraRow) MarshalCSVValue() interface{} { |
| 94 | + return []*PostgresInfraRow{r} |
| 95 | +} |
| 96 | + |
| 97 | +func toPostgresInfraRows(pg *ps.PostgresBranchInfrastructure) []*PostgresInfraRow { |
| 98 | + rows := make([]*PostgresInfraRow, 0, len(pg.Nodes)+len(pg.Bouncers)) |
| 99 | + |
| 100 | + for _, node := range pg.Nodes { |
| 101 | + storage := "-" |
| 102 | + if node.VolumeUsageBytes != nil && node.VolumeCapacityBytes != nil { |
| 103 | + storage = fmt.Sprintf("%s / %s", |
| 104 | + humanize.IBytes(uint64(*node.VolumeUsageBytes)), |
| 105 | + humanize.IBytes(uint64(*node.VolumeCapacityBytes))) |
| 106 | + } |
| 107 | + |
| 108 | + rows = append(rows, &PostgresInfraRow{ |
| 109 | + Component: "postgres", |
| 110 | + Role: node.Role, |
| 111 | + Size: node.ClusterDisplayName, |
| 112 | + AZ: node.AvailabilityZone, |
| 113 | + Storage: storage, |
| 114 | + Name: node.Name, |
| 115 | + orig: node, |
| 116 | + }) |
| 117 | + } |
| 118 | + |
| 119 | + for _, bouncer := range pg.Bouncers { |
| 120 | + size := "-" |
| 121 | + if bouncer.SKU != nil { |
| 122 | + size = bouncer.SKU.DisplayName |
| 123 | + } |
| 124 | + |
| 125 | + rows = append(rows, &PostgresInfraRow{ |
| 126 | + Component: "pgbouncer", |
| 127 | + Role: bouncer.Target, |
| 128 | + Size: size, |
| 129 | + AZ: "-", |
| 130 | + Storage: "-", |
| 131 | + Name: bouncer.Name, |
| 132 | + orig: bouncer, |
| 133 | + }) |
| 134 | + } |
| 135 | + |
| 136 | + return rows |
| 137 | +} |
| 138 | + |
77 | 139 | func humanAge(d time.Duration) string { |
78 | 140 | if d < time.Minute { |
79 | 141 | return fmt.Sprintf("%ds", int(d.Seconds())) |
@@ -137,12 +199,25 @@ func InfraCmd(ch *cmdutil.Helper) *cobra.Command { |
137 | 199 |
|
138 | 200 | end() |
139 | 201 |
|
140 | | - if len(infra.Pods) == 0 && ch.Printer.Format() == printer.Human { |
141 | | - ch.Printer.Printf("No pods found for branch %s.\n", printer.BoldBlue(branch)) |
142 | | - return nil |
143 | | - } |
| 202 | + switch { |
| 203 | + case infra.Postgres != nil: |
| 204 | + rows := toPostgresInfraRows(infra.Postgres) |
| 205 | + if len(rows) == 0 && ch.Printer.Format() == printer.Human { |
| 206 | + ch.Printer.Printf("No nodes found for branch %s.\n", printer.BoldBlue(branch)) |
| 207 | + return nil |
| 208 | + } |
144 | 209 |
|
145 | | - return ch.Printer.PrintResource(toInfraPods(infra.Pods)) |
| 210 | + return ch.Printer.PrintResource(rows) |
| 211 | + case infra.Vitess != nil: |
| 212 | + if len(infra.Vitess.Pods) == 0 && ch.Printer.Format() == printer.Human { |
| 213 | + ch.Printer.Printf("No pods found for branch %s.\n", printer.BoldBlue(branch)) |
| 214 | + return nil |
| 215 | + } |
| 216 | + |
| 217 | + return ch.Printer.PrintResource(toInfraPods(infra.Vitess.Pods)) |
| 218 | + default: |
| 219 | + return fmt.Errorf("unexpected infrastructure response for branch %s", branch) |
| 220 | + } |
146 | 221 | }, |
147 | 222 | } |
148 | 223 |
|
|
0 commit comments