@@ -2,6 +2,7 @@ package cli
2
2
3
3
import (
4
4
"fmt"
5
+ "strings"
5
6
6
7
"github.com/coder/coder/v2/cli/clibase"
7
8
"github.com/coder/coder/v2/cli/cliui"
@@ -29,29 +30,28 @@ func (r *RootCmd) organizations() *clibase.Cmd {
29
30
30
31
func (r * RootCmd ) currentOrganization () * clibase.Cmd {
31
32
var (
32
- client = new (codersdk.Client )
33
- formatter = cliui .NewOutputFormatter (
33
+ stringFormat func (orgs []codersdk.Organization ) (string , error )
34
+ client = new (codersdk.Client )
35
+ formatter = cliui .NewOutputFormatter (
34
36
cliui .ChangeFormatterData (cliui .TextFormat (), func (data any ) (any , error ) {
35
37
typed , ok := data .([]codersdk.Organization )
36
38
if ! ok {
37
39
// This should never happen
38
40
return "" , fmt .Errorf ("expected []Organization, got %T" , data )
39
41
}
40
- if len (typed ) != 1 {
41
- return "" , fmt .Errorf ("expected 1 organization, got %d" , len (typed ))
42
- }
43
- return fmt .Sprintf ("Current organization: %s (%s)\n " , typed [0 ].Name , typed [0 ].ID .String ()), nil
42
+ return stringFormat (typed )
44
43
}),
45
44
cliui .TableFormat ([]codersdk.Organization {}, []string {"id" , "name" , "default" }),
46
45
cliui .JSONFormat (),
47
46
)
48
47
onlyID = false
49
48
)
50
49
cmd := & clibase.Cmd {
51
- Use : "current" ,
52
- Short : "Show the current selected organization the cli will use ." ,
50
+ Use : "show [ current|me|uuid] " ,
51
+ Short : "Show organization information. By default, if no argument is provided, the current cli configured organization is shown ." ,
53
52
Middleware : clibase .Chain (
54
53
r .InitClient (client ),
54
+ clibase .RequireRangeArgs (0 , 1 ),
55
55
),
56
56
Options : clibase.OptionSet {
57
57
{
@@ -63,15 +63,60 @@ func (r *RootCmd) currentOrganization() *clibase.Cmd {
63
63
},
64
64
},
65
65
Handler : func (inv * clibase.Invocation ) error {
66
- org , err := CurrentOrganization (r , inv , client )
67
- if err != nil {
68
- return err
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 }
69
112
}
70
113
71
114
if onlyID {
72
- _ , _ = fmt .Fprintf (inv .Stdout , "%s\n " , org .ID )
115
+ for _ , org := range orgs {
116
+ _ , _ = fmt .Fprintf (inv .Stdout , "%s\n " , org .ID )
117
+ }
73
118
} else {
74
- out , err := formatter .Format (inv .Context (), []codersdk. Organization { org } )
119
+ out , err := formatter .Format (inv .Context (), orgs )
75
120
if err != nil {
76
121
return err
77
122
}
0 commit comments