()
| 14 | ) |
| 15 | |
| 16 | func (r *RootCmd) groupList() *serpent.Command { |
| 17 | formatter := cliui.NewOutputFormatter( |
| 18 | cliui.TableFormat([]groupTableRow{}, nil), |
| 19 | cliui.JSONFormat(), |
| 20 | ) |
| 21 | orgContext := agpl.NewOrganizationContext() |
| 22 | |
| 23 | cmd := &serpent.Command{ |
| 24 | Use: "list", |
| 25 | Short: "List user groups", |
| 26 | Middleware: serpent.Chain( |
| 27 | serpent.RequireNArgs(0), |
| 28 | ), |
| 29 | Handler: func(inv *serpent.Invocation) error { |
| 30 | ctx := inv.Context() |
| 31 | client, err := r.InitClient(inv) |
| 32 | if err != nil { |
| 33 | return err |
| 34 | } |
| 35 | |
| 36 | org, err := orgContext.Selected(inv, client) |
| 37 | if err != nil { |
| 38 | return xerrors.Errorf("current organization: %w", err) |
| 39 | } |
| 40 | |
| 41 | groups, err := client.GroupsByOrganization(ctx, org.ID) |
| 42 | if err != nil { |
| 43 | return xerrors.Errorf("get groups: %w", err) |
| 44 | } |
| 45 | |
| 46 | rows := groupsToRows(groups...) |
| 47 | out, err := formatter.Format(inv.Context(), rows) |
| 48 | if err != nil { |
| 49 | return xerrors.Errorf("display groups: %w", err) |
| 50 | } |
| 51 | |
| 52 | if out == "" { |
| 53 | _, _ = fmt.Fprintf(inv.Stderr, "%s No groups found in %s! Create one:\n\n", agpl.Caret, color.HiWhiteString(org.Name)) |
| 54 | _, _ = fmt.Fprintln(inv.Stderr, color.HiMagentaString(" $ coder groups create <name>\n")) |
| 55 | return nil |
| 56 | } |
| 57 | |
| 58 | _, _ = fmt.Fprintln(inv.Stdout, out) |
| 59 | return nil |
| 60 | }, |
| 61 | } |
| 62 | |
| 63 | formatter.AttachOptions(&cmd.Options) |
| 64 | orgContext.AttachOptions(cmd) |
| 65 | return cmd |
| 66 | } |
| 67 | |
| 68 | type groupTableRow struct { |
| 69 | // For json output: |
no test coverage detected