|
| 1 | +package mpg |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/spf13/cobra" |
| 8 | + |
| 9 | + "github.com/superfly/flyctl/gql" |
| 10 | + "github.com/superfly/flyctl/iostreams" |
| 11 | + |
| 12 | + "github.com/superfly/flyctl/internal/command" |
| 13 | + "github.com/superfly/flyctl/internal/command/orgs" |
| 14 | + "github.com/superfly/flyctl/internal/config" |
| 15 | + "github.com/superfly/flyctl/internal/flag" |
| 16 | + "github.com/superfly/flyctl/internal/flyutil" |
| 17 | + "github.com/superfly/flyctl/internal/render" |
| 18 | + "github.com/superfly/flyctl/internal/uiexutil" |
| 19 | +) |
| 20 | + |
| 21 | +func newList() *cobra.Command { |
| 22 | + const ( |
| 23 | + long = `List MPG clusters owned by the specified organization. |
| 24 | +If no organization is specified, the user's personal organization is used.` |
| 25 | + short = "List MPG clusters." |
| 26 | + usage = "list" |
| 27 | + ) |
| 28 | + |
| 29 | + cmd := command.New(usage, short, long, runList, |
| 30 | + command.RequireSession, |
| 31 | + command.RequireUiex, |
| 32 | + ) |
| 33 | + |
| 34 | + cmd.Aliases = []string{"ls"} |
| 35 | + |
| 36 | + flag.Add(cmd, flag.JSONOutput()) |
| 37 | + flag.Add(cmd, flag.Org()) |
| 38 | + |
| 39 | + return cmd |
| 40 | +} |
| 41 | + |
| 42 | +func runList(ctx context.Context) error { |
| 43 | + cfg := config.FromContext(ctx) |
| 44 | + out := iostreams.FromContext(ctx).Out |
| 45 | + |
| 46 | + org, err := orgs.OrgFromFlagOrSelect(ctx) |
| 47 | + if err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + |
| 51 | + uiexClient := uiexutil.ClientFromContext(ctx) |
| 52 | + genqClient := flyutil.ClientFromContext(ctx).GenqClient() |
| 53 | + |
| 54 | + // For ui-ex request we need the real org slug |
| 55 | + var fullOrg *gql.GetOrganizationResponse |
| 56 | + if fullOrg, err = gql.GetOrganization(ctx, genqClient, org.Slug); err != nil { |
| 57 | + err = fmt.Errorf("failed fetching org: %w", err) |
| 58 | + return err |
| 59 | + } |
| 60 | + |
| 61 | + clusters, err := uiexClient.ListManagedClusters(ctx, fullOrg.Organization.RawSlug) |
| 62 | + if err != nil { |
| 63 | + return fmt.Errorf("failed to list managed clusters for organization %s: %w", org.Slug, err) |
| 64 | + } |
| 65 | + |
| 66 | + if len(clusters.Data) == 0 { |
| 67 | + fmt.Fprintf(out, "No managed postgres clusters found in organization %s\n", org.Slug) |
| 68 | + return nil |
| 69 | + } |
| 70 | + |
| 71 | + if cfg.JSONOutput { |
| 72 | + return render.JSON(out, clusters.Data) |
| 73 | + } |
| 74 | + |
| 75 | + rows := make([][]string, 0, len(clusters.Data)) |
| 76 | + for _, cluster := range clusters.Data { |
| 77 | + rows = append(rows, []string{ |
| 78 | + cluster.Id, |
| 79 | + cluster.Name, |
| 80 | + cluster.Organization.Slug, |
| 81 | + cluster.Region, |
| 82 | + cluster.Status, |
| 83 | + cluster.Plan, |
| 84 | + }) |
| 85 | + } |
| 86 | + |
| 87 | + return render.Table(out, "", rows, "ID", "Name", "Org", "Region", "Status", "Plan") |
| 88 | +} |
0 commit comments