|
| 1 | +package teams |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "io" |
| 6 | + "sort" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/spf13/cobra" |
| 10 | + "gopkg.in/yaml.v3" |
| 11 | + |
| 12 | + "github.com/intility/indev/internal/redact" |
| 13 | + "github.com/intility/indev/internal/telemetry" |
| 14 | + "github.com/intility/indev/internal/ux" |
| 15 | + "github.com/intility/indev/pkg/client" |
| 16 | + "github.com/intility/indev/pkg/clientset" |
| 17 | + "github.com/intility/indev/pkg/outputformat" |
| 18 | +) |
| 19 | + |
| 20 | +func NewListCommand(set clientset.ClientSet) *cobra.Command { |
| 21 | + output := outputformat.Format("") |
| 22 | + cmd := &cobra.Command{ |
| 23 | + Use: "list", |
| 24 | + Short: "List all teams", |
| 25 | + Long: `List all teams in the Intility Developer Platform`, |
| 26 | + PreRunE: set.EnsureSignedInPreHook, |
| 27 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 28 | + ctx, span := telemetry.StartSpan(cmd.Context(), "teams.list") |
| 29 | + defer span.End() |
| 30 | + |
| 31 | + cmd.SilenceUsage = true |
| 32 | + |
| 33 | + teams, err := set.PlatformClient.ListTeams(ctx) |
| 34 | + if err != nil { |
| 35 | + return redact.Errorf("could not list teams: %w", redact.Safe(err)) |
| 36 | + } |
| 37 | + |
| 38 | + if len(teams) == 0 { |
| 39 | + ux.Fprint(cmd.OutOrStdout(), "No teams found\n") |
| 40 | + return nil |
| 41 | + } |
| 42 | + |
| 43 | + if err = printTeamsList(cmd.OutOrStdout(), output, teams); err != nil { |
| 44 | + return redact.Errorf("could not print teams list: %w", redact.Safe(err)) |
| 45 | + } |
| 46 | + |
| 47 | + return nil |
| 48 | + }, |
| 49 | + } |
| 50 | + |
| 51 | + cmd.Flags().VarP(&output, "output", "o", "Output format (wide, json, yaml)") |
| 52 | + |
| 53 | + return cmd |
| 54 | +} |
| 55 | + |
| 56 | +func printTeamsList(writer io.Writer, format outputformat.Format, teams []client.Team) error { |
| 57 | + var err error |
| 58 | + |
| 59 | + sort.Slice(teams, func(i, j int) bool { |
| 60 | + // list teams where authenticated user has membership first |
| 61 | + return strings.Join(teams[i].Role, ",") > strings.Join(teams[j].Role, ",") |
| 62 | + }) |
| 63 | + |
| 64 | + switch format { |
| 65 | + case "wide": |
| 66 | + table := ux.TableFromObjects(teams, func(team client.Team) []ux.Row { |
| 67 | + return []ux.Row{ |
| 68 | + ux.NewRow("Id", team.ID), |
| 69 | + ux.NewRow("Name", team.Name), |
| 70 | + ux.NewRow("Description", team.Description), |
| 71 | + ux.NewRow("Role", strings.Join(team.Role, ",")), |
| 72 | + } |
| 73 | + }) |
| 74 | + |
| 75 | + ux.Fprint(writer, "%s", table.String()) |
| 76 | + case "json": |
| 77 | + enc := json.NewEncoder(writer) |
| 78 | + enc.SetIndent("", " ") |
| 79 | + err = enc.Encode(teams) |
| 80 | + case "yaml": |
| 81 | + indent := 2 |
| 82 | + enc := yaml.NewEncoder(writer) |
| 83 | + enc.SetIndent(indent) |
| 84 | + err = enc.Encode(teams) |
| 85 | + default: |
| 86 | + table := ux.TableFromObjects(teams, func(team client.Team) []ux.Row { |
| 87 | + return []ux.Row{ |
| 88 | + ux.NewRow("Name", team.Name), |
| 89 | + ux.NewRow("Description", team.Description), |
| 90 | + ux.NewRow("Role", strings.Join(team.Role, ",")), |
| 91 | + } |
| 92 | + }) |
| 93 | + |
| 94 | + ux.Fprint(writer, "%s", table.String()) |
| 95 | + } |
| 96 | + |
| 97 | + if err != nil { |
| 98 | + return redact.Errorf("output encoder failed: %w", redact.Safe(err)) |
| 99 | + } |
| 100 | + |
| 101 | + return nil |
| 102 | +} |
0 commit comments