Skip to content

feat: switch organization context in coder organizations #12265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
switch -> set
  • Loading branch information
Emyrk committed Feb 26, 2024
commit 887bcaee0137f8224cc77aadf04ba663b80b0bb5
18 changes: 10 additions & 8 deletions cli/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ func (r *RootCmd) switchOrganization() *clibase.Cmd {
client := new(codersdk.Client)

cmd := &clibase.Cmd{
Use: "switch <organization name | ID>",
Short: "Switch the organization used by the cli. Pass an empty string to reset to the default organization.",
Long: "Switch the organization used by the cli. Pass an empty string to reset to the default organization.\n" + formatExamples(
Use: "set <organization name | ID>",
Short: "set the organization used by the CLI. Pass an empty string to reset to the default organization.",
Long: "set the organization used by the CLI. Pass an empty string to reset to the default organization.\n" + formatExamples(
example{
Description: "Remove the current organization and defer to the default.",
Command: "coder organizations switch ''",
Command: "coder organizations set ''",
},
example{
Description: "Switch to a custom organization.",
Command: "coder organizations switch my-org",
Command: "coder organizations set my-org",
},
),
Middleware: clibase.Chain(
Expand Down Expand Up @@ -118,7 +118,7 @@ func (r *RootCmd) switchOrganization() *clibase.Cmd {
var sdkError *codersdk.Error
if errors.As(err, &sdkError) {
if sdkError.Helper == "" && sdkError.StatusCode() != 500 {
sdkError.Helper = `If this error persists, try unsetting your org with 'coder organizations switch ""'`
sdkError.Helper = `If this error persists, try unsetting your org with 'coder organizations set ""'`
}
return sdkError
}
Expand Down Expand Up @@ -167,13 +167,15 @@ func promptUserSelectOrg(inv *clibase.Invocation, conf config.Root, orgs []coder
defaultOrg = orgs[index].Name
}

const deselectOption = "--Deselect--"
// deselectOption is the option to delete the organization config file and defer
// to default behavior.
const deselectOption = "[Default]"
if defaultOrg == "" {
defaultOrg = deselectOption
}

// Pull value from a prompt
_, _ = fmt.Fprintln(inv.Stdout, pretty.Sprint(cliui.DefaultStyles.Wrap, "Select an organization below to switch the current cli context to:"))
_, _ = fmt.Fprintln(inv.Stdout, pretty.Sprint(cliui.DefaultStyles.Wrap, "Select an organization below to set the current CLI context to:"))
value, err := cliui.Select(inv, cliui.SelectOptions{
Options: append([]string{deselectOption}, orgNames(orgs)...),
Default: defaultOrg,
Expand Down
2 changes: 1 addition & 1 deletion cli/organization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestOrganizationSwitch(t *testing.T) {
exp, err := client.OrganizationByName(ctx, codersdk.Me, "foo")
require.NoError(t, err)

inv, root := clitest.New(t, "organizations", "switch", "foo")
inv, root := clitest.New(t, "organizations", "set", "foo")
clitest.SetupConfig(t, client, root)
pty := ptytest.New(t).Attach(inv)
errC := make(chan error)
Expand Down
2 changes: 1 addition & 1 deletion cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ func CurrentOrganization(r *RootCmd, inv *clibase.Invocation, client *codersdk.C
return org.IsDefault
})
if index < 0 {
return codersdk.Organization{}, xerrors.Errorf("unable to determine current organization. Use 'coder organizations switch <org>' to select an organization to use")
return codersdk.Organization{}, xerrors.Errorf("unable to determine current organization. Use 'coder set <org>' to select an organization to use")
}

return orgs[index], nil
Expand Down