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
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
rename var
  • Loading branch information
Emyrk committed Feb 26, 2024
commit b07765cf6dec17fdc7931af8ff42af4db868404f
14 changes: 7 additions & 7 deletions cli/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,21 @@ func (r *RootCmd) switchOrganization() *clibase.Cmd {
return strings.Compare(a.Name, b.Name)
})

var orgArg string
var switchToOrg string
if len(inv.Args) == 0 {
// Pull orgArg from a prompt selector, rather than command line
// Pull switchToOrg from a prompt selector, rather than command line
// args.
orgArg, err = promptUserSelectOrg(inv, conf, orgs)
switchToOrg, err = promptUserSelectOrg(inv, conf, orgs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be nice if the currently selected one is highlighted in the list, perhaps? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is! But I read it only from the config file. I ignore the CurrentOrganization because that takes into account the -z flag and is_default.

If a user has nothing set, I want the reset option to be selected.

if err != nil {
return err
}
} else {
orgArg = inv.Args[0]
switchToOrg = inv.Args[0]
}

// If the user passes an empty string, we want to remove the organization
// from the config file. This will defer to default behavior.
if orgArg == "" {
if switchToOrg == "" {
err := conf.Organization().Delete()
if err != nil && !errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("failed to unset organization: %w", err)
Expand All @@ -89,13 +89,13 @@ func (r *RootCmd) switchOrganization() *clibase.Cmd {
} else {
// Find the selected org in our list.
index := slices.IndexFunc(orgs, func(org codersdk.Organization) bool {
return org.Name == orgArg || org.ID.String() == orgArg
return org.Name == switchToOrg || org.ID.String() == switchToOrg
})
if index < 0 {
// Using this error for better error message formatting
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should really have one for CLI errors too. (PS. There is exitError, but you might still prefer this approach.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea we really should. I did not want to make a new one for this.

exitError is a single line, this error format will display the helper text on a new line.

err := &codersdk.Error{
Response: codersdk.Response{
Message: fmt.Sprintf("Organization %q not found. Is the name correct, and are you a member of it?", orgArg),
Message: fmt.Sprintf("Organization %q not found. Is the name correct, and are you a member of it?", switchToOrg),
Detail: "Ensure the organization argument is correct and you are a member of it.",
},
Helper: fmt.Sprintf("Valid organizations you can switch to: %q", strings.Join(orgNames(orgs), ", ")),
Expand Down