MCPcopy Create free account
hub / github.com/coder/coder / groupList

Method groupList

enterprise/cli/grouplist.go:16–66  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

14)
15
16func (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
68type groupTableRow struct {
69 // For json output:

Callers 1

groupsMethod · 0.95

Calls 12

InitClientMethod · 0.95
SelectedMethod · 0.95
FormatMethod · 0.95
AttachOptionsMethod · 0.95
AttachOptionsMethod · 0.95
NewOutputFormatterFunction · 0.92
TableFormatFunction · 0.92
JSONFormatFunction · 0.92
groupsToRowsFunction · 0.85
GroupsByOrganizationMethod · 0.80
ContextMethod · 0.65
ErrorfMethod · 0.45

Tested by

no test coverage detected