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