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 error message
  • Loading branch information
Emyrk committed Feb 26, 2024
commit c65ec3595de065dd762fada43010822239e276d3
35 changes: 34 additions & 1 deletion cli/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"os"
"slices"
"strings"

"github.com/coder/coder/v2/cli/clibase"
Expand All @@ -23,6 +24,7 @@ func (r *RootCmd) organizations() *clibase.Cmd {
},
Children: []*clibase.Cmd{
r.currentOrganization(),
r.switchOrganization(),
},
}

Expand Down Expand Up @@ -53,13 +55,44 @@ func (r *RootCmd) switchOrganization() *clibase.Cmd {
),
Options: clibase.OptionSet{},
Handler: func(inv *clibase.Invocation) error {
orgArg := inv.Args[0]
conf := r.createConfig()
orgs, err := client.OrganizationsByUser(inv.Context(), codersdk.Me)
if err != nil {
return fmt.Errorf("failed to get organizations: %w", err)
}

// If the user passes an empty string, we want to remove the organization
if inv.Args[0] == "" {
if orgArg == "" {
err := conf.Organization().Delete()
if err != nil && !errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("failed to unset organization: %w", err)
}
} else {
index := slices.IndexFunc(orgs, func(org codersdk.Organization) bool {
return org.Name == orgArg || org.ID.String() == orgArg
})
if index < 0 {
names := make([]string, 0, len(orgs))
for _, org := range orgs {
names = append(names, org.Name)
}

// 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.", orgArg),
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(names, ", ")),
}
return err
}

err := conf.Organization().Write(orgs[index].ID.String())
if err != nil {
return fmt.Errorf("failed to write organization to config file: %w", err)
}
}

return nil
Expand Down
8 changes: 6 additions & 2 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -1192,8 +1192,12 @@ func formatRunCommandError(err *clibase.RunCommandError, opts *formatOpts) strin
func formatCoderSDKError(from string, err *codersdk.Error, opts *formatOpts) string {
var str strings.Builder
if opts.Verbose {
_, _ = str.WriteString(pretty.Sprint(headLineStyle(), fmt.Sprintf("API request error to \"%s:%s\". Status code %d", err.Method(), err.URL(), err.StatusCode())))
_, _ = str.WriteString("\n")
// If all these fields are empty, then do not print this information.
// This can occur if the error is being used outside the api.
if !(err.Method() == "" && err.URL() == "" && err.StatusCode() == 0) {
_, _ = str.WriteString(pretty.Sprint(headLineStyle(), fmt.Sprintf("API request error to \"%s:%s\". Status code %d", err.Method(), err.URL(), err.StatusCode())))
_, _ = str.WriteString("\n")
}
}
// Always include this trace. Users can ignore this.
if from != "" {
Expand Down