Skip to content

Commit 995b813

Browse files
committed
feat: add coder organizations switch to change org context
1 parent cad75c2 commit 995b813

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

cli/organization.go

Lines changed: 41 additions & 0 deletions
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"
@@ -27,6 +29,45 @@ func (r *RootCmd) organizations() *clibase.Cmd {
2729
return cmd
2830
}
2931

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

0 commit comments

Comments
 (0)