Skip to content

Commit bb2bf18

Browse files
committed
feat: add coder organizations switch to change org context
1 parent 3487710 commit bb2bf18

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

cli/organization.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package cli
22

33
import (
4+
"errors"
45
"fmt"
6+
"os"
57

68
"github.com/coder/coder/v2/cli/clibase"
79
"github.com/coder/coder/v2/cli/cliui"
@@ -14,7 +16,6 @@ func (r *RootCmd) organizations() *clibase.Cmd {
1416
Use: "organizations { current }",
1517
Short: "Organization related commands",
1618
Aliases: []string{"organization", "org", "orgs"},
17-
1819
Handler: func(inv *clibase.Invocation) error {
1920
return inv.Command.HelpHandler(inv)
2021
},
@@ -27,6 +28,45 @@ func (r *RootCmd) organizations() *clibase.Cmd {
2728
return cmd
2829
}
2930

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+
3070
func (r *RootCmd) currentOrganization() *clibase.Cmd {
3171
var (
3272
client = new(codersdk.Client)

0 commit comments

Comments
 (0)