Skip to content

Commit 0c2cf56

Browse files
committed
feat: add coder organizations switch to change org context
1 parent d2998c6 commit 0c2cf56

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
"strings"
68

79
"github.com/coder/coder/v2/cli/clibase"
@@ -28,6 +30,45 @@ func (r *RootCmd) organizations() *clibase.Cmd {
2830
return cmd
2931
}
3032

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+
3172
func (r *RootCmd) currentOrganization() *clibase.Cmd {
3273
var (
3374
stringFormat func(orgs []codersdk.Organization) (string, error)

0 commit comments

Comments
 (0)