|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/coder/coder/v2/cli/clibase" |
| 8 | + "github.com/coder/coder/v2/cli/cliui" |
| 9 | + "github.com/coder/coder/v2/codersdk" |
| 10 | +) |
| 11 | + |
| 12 | +func (r *RootCmd) organizations() *clibase.Cmd { |
| 13 | + cmd := &clibase.Cmd{ |
| 14 | + Annotations: workspaceCommand, |
| 15 | + Use: "organizations [subcommand]", |
| 16 | + Short: "Organization related commands", |
| 17 | + Aliases: []string{"organization", "org", "orgs"}, |
| 18 | + Hidden: true, // Hidden until these commands are complete. |
| 19 | + Handler: func(inv *clibase.Invocation) error { |
| 20 | + return inv.Command.HelpHandler(inv) |
| 21 | + }, |
| 22 | + Children: []*clibase.Cmd{ |
| 23 | + r.currentOrganization(), |
| 24 | + }, |
| 25 | + } |
| 26 | + |
| 27 | + cmd.Options = clibase.OptionSet{} |
| 28 | + return cmd |
| 29 | +} |
| 30 | + |
| 31 | +func (r *RootCmd) currentOrganization() *clibase.Cmd { |
| 32 | + var ( |
| 33 | + stringFormat func(orgs []codersdk.Organization) (string, error) |
| 34 | + client = new(codersdk.Client) |
| 35 | + formatter = cliui.NewOutputFormatter( |
| 36 | + cliui.ChangeFormatterData(cliui.TextFormat(), func(data any) (any, error) { |
| 37 | + typed, ok := data.([]codersdk.Organization) |
| 38 | + if !ok { |
| 39 | + // This should never happen |
| 40 | + return "", fmt.Errorf("expected []Organization, got %T", data) |
| 41 | + } |
| 42 | + return stringFormat(typed) |
| 43 | + }), |
| 44 | + cliui.TableFormat([]codersdk.Organization{}, []string{"id", "name", "default"}), |
| 45 | + cliui.JSONFormat(), |
| 46 | + ) |
| 47 | + onlyID = false |
| 48 | + ) |
| 49 | + cmd := &clibase.Cmd{ |
| 50 | + Use: "show [current|me|uuid]", |
| 51 | + Short: "Show the organization, if no argument is given, the organization currently in use will be shown.", |
| 52 | + Middleware: clibase.Chain( |
| 53 | + r.InitClient(client), |
| 54 | + clibase.RequireRangeArgs(0, 1), |
| 55 | + ), |
| 56 | + Options: clibase.OptionSet{ |
| 57 | + { |
| 58 | + Name: "only-id", |
| 59 | + Description: "Only print the organization ID.", |
| 60 | + Required: false, |
| 61 | + Flag: "only-id", |
| 62 | + Value: clibase.BoolOf(&onlyID), |
| 63 | + }, |
| 64 | + }, |
| 65 | + Handler: func(inv *clibase.Invocation) error { |
| 66 | + orgArg := "current" |
| 67 | + if len(inv.Args) >= 1 { |
| 68 | + orgArg = inv.Args[0] |
| 69 | + } |
| 70 | + |
| 71 | + var orgs []codersdk.Organization |
| 72 | + var err error |
| 73 | + switch strings.ToLower(orgArg) { |
| 74 | + case "current": |
| 75 | + stringFormat = func(orgs []codersdk.Organization) (string, error) { |
| 76 | + if len(orgs) != 1 { |
| 77 | + return "", fmt.Errorf("expected 1 organization, got %d", len(orgs)) |
| 78 | + } |
| 79 | + return fmt.Sprintf("Current CLI Organization: %s (%s)\n", orgs[0].Name, orgs[0].ID.String()), nil |
| 80 | + } |
| 81 | + org, err := CurrentOrganization(r, inv, client) |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + orgs = []codersdk.Organization{org} |
| 86 | + case "me": |
| 87 | + stringFormat = func(orgs []codersdk.Organization) (string, error) { |
| 88 | + var str strings.Builder |
| 89 | + _, _ = fmt.Fprint(&str, "Organizations you are a member of:\n") |
| 90 | + for _, org := range orgs { |
| 91 | + _, _ = fmt.Fprintf(&str, "\t%s (%s)\n", org.Name, org.ID.String()) |
| 92 | + } |
| 93 | + return str.String(), nil |
| 94 | + } |
| 95 | + orgs, err = client.OrganizationsByUser(inv.Context(), codersdk.Me) |
| 96 | + if err != nil { |
| 97 | + return err |
| 98 | + } |
| 99 | + default: |
| 100 | + stringFormat = func(orgs []codersdk.Organization) (string, error) { |
| 101 | + if len(orgs) != 1 { |
| 102 | + return "", fmt.Errorf("expected 1 organization, got %d", len(orgs)) |
| 103 | + } |
| 104 | + return fmt.Sprintf("Organization: %s (%s)\n", orgs[0].Name, orgs[0].ID.String()), nil |
| 105 | + } |
| 106 | + // This works for a uuid or a name |
| 107 | + org, err := client.OrganizationByName(inv.Context(), orgArg) |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + orgs = []codersdk.Organization{org} |
| 112 | + } |
| 113 | + |
| 114 | + if onlyID { |
| 115 | + for _, org := range orgs { |
| 116 | + _, _ = fmt.Fprintf(inv.Stdout, "%s\n", org.ID) |
| 117 | + } |
| 118 | + } else { |
| 119 | + out, err := formatter.Format(inv.Context(), orgs) |
| 120 | + if err != nil { |
| 121 | + return err |
| 122 | + } |
| 123 | + _, _ = fmt.Fprint(inv.Stdout, out) |
| 124 | + } |
| 125 | + return nil |
| 126 | + }, |
| 127 | + } |
| 128 | + formatter.AttachOptions(&cmd.Options) |
| 129 | + |
| 130 | + return cmd |
| 131 | +} |
0 commit comments