1
1
package cli
2
2
3
3
import (
4
+ "errors"
4
5
"fmt"
6
+ "os"
5
7
6
8
"github.com/coder/coder/v2/cli/clibase"
7
9
"github.com/coder/coder/v2/cli/cliui"
@@ -14,7 +16,6 @@ func (r *RootCmd) organizations() *clibase.Cmd {
14
16
Use : "organizations { current }" ,
15
17
Short : "Organization related commands" ,
16
18
Aliases : []string {"organization" , "org" , "orgs" },
17
-
18
19
Handler : func (inv * clibase.Invocation ) error {
19
20
return inv .Command .HelpHandler (inv )
20
21
},
@@ -27,6 +28,45 @@ func (r *RootCmd) organizations() *clibase.Cmd {
27
28
return cmd
28
29
}
29
30
31
+ func (r * RootCmd ) switchOrganization () * clibase.Cmd {
32
+ var (
33
+ client = new (codersdk.Client )
34
+ )
35
+
36
+ cmd := & clibase.Cmd {
37
+ Use : "switch <organization name | ID>" ,
38
+ Short : "Switch the organization used by the cli. Pass an empty string to reset to the default organization." ,
39
+ Long : "Switch the organization used by the cli. Pass an empty string to reset to the default organization.\n " + formatExamples (
40
+ example {
41
+ Description : "Remove the current organization and defer to the default." ,
42
+ Command : "coder organizations switch ''" ,
43
+ },
44
+ example {
45
+ Description : "Switch to a custom organization." ,
46
+ Command : "coder organizations switch my-org" ,
47
+ },
48
+ ),
49
+ Middleware : clibase .Chain (
50
+ r .InitClient (client ),
51
+ ),
52
+ Options : clibase.OptionSet {},
53
+ Handler : func (inv * clibase.Invocation ) error {
54
+ conf := r .createConfig ()
55
+ // If the user passes an empty string, we want to remove the organization
56
+ if inv .Args [0 ] == "" {
57
+ err := conf .Organization ().Delete ()
58
+ if err != nil && ! errors .Is (err , os .ErrNotExist ) {
59
+ return fmt .Errorf ("failed to unset organization: %w" , err )
60
+ }
61
+ }
62
+
63
+ return nil
64
+ },
65
+ }
66
+
67
+ return cmd
68
+ }
69
+
30
70
func (r * RootCmd ) currentOrganization () * clibase.Cmd {
31
71
var (
32
72
client = new (codersdk.Client )
0 commit comments