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