Skip to content

chore(cli): use xerrors.Errorf instead of fmt.Errorf #12368

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 2 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 9 additions & 7 deletions cli/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"slices"
"strings"

"golang.org/x/xerrors"

"github.com/coder/coder/v2/cli/clibase"
"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/cli/config"
Expand Down Expand Up @@ -60,7 +62,7 @@ func (r *RootCmd) switchOrganization() *clibase.Cmd {
conf := r.createConfig()
orgs, err := client.OrganizationsByUser(inv.Context(), codersdk.Me)
if err != nil {
return fmt.Errorf("failed to get organizations: %w", err)
return xerrors.Errorf("failed to get organizations: %w", err)
}
// Keep the list of orgs sorted
slices.SortFunc(orgs, func(a, b codersdk.Organization) int {
Expand All @@ -84,7 +86,7 @@ func (r *RootCmd) switchOrganization() *clibase.Cmd {
if switchToOrg == "" {
err := conf.Organization().Delete()
if err != nil && !errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("failed to unset organization: %w", err)
return xerrors.Errorf("failed to unset organization: %w", err)
}
_, _ = fmt.Fprintf(inv.Stdout, "Organization unset\n")
} else {
Expand All @@ -107,7 +109,7 @@ func (r *RootCmd) switchOrganization() *clibase.Cmd {
// Always write the uuid to the config file. Names can change.
err := conf.Organization().Write(orgs[index].ID.String())
if err != nil {
return fmt.Errorf("failed to write organization to config file: %w", err)
return xerrors.Errorf("failed to write organization to config file: %w", err)
}
}

Expand All @@ -123,7 +125,7 @@ func (r *RootCmd) switchOrganization() *clibase.Cmd {
}
return sdkError
}
return fmt.Errorf("failed to get current organization: %w", err)
return xerrors.Errorf("failed to get current organization: %w", err)
}

_, _ = fmt.Fprintf(inv.Stdout, "Current organization context set to %s (%s)\n", current.Name, current.ID.String())
Expand Down Expand Up @@ -213,7 +215,7 @@ func (r *RootCmd) currentOrganization() *clibase.Cmd {
typed, ok := data.([]codersdk.Organization)
if !ok {
// This should never happen
return "", fmt.Errorf("expected []Organization, got %T", data)
return "", xerrors.Errorf("expected []Organization, got %T", data)
}
return stringFormat(typed)
}),
Expand Down Expand Up @@ -250,7 +252,7 @@ func (r *RootCmd) currentOrganization() *clibase.Cmd {
case "current":
stringFormat = func(orgs []codersdk.Organization) (string, error) {
if len(orgs) != 1 {
return "", fmt.Errorf("expected 1 organization, got %d", len(orgs))
return "", xerrors.Errorf("expected 1 organization, got %d", len(orgs))
}
return fmt.Sprintf("Current CLI Organization: %s (%s)\n", orgs[0].Name, orgs[0].ID.String()), nil
}
Expand All @@ -275,7 +277,7 @@ func (r *RootCmd) currentOrganization() *clibase.Cmd {
default:
stringFormat = func(orgs []codersdk.Organization) (string, error) {
if len(orgs) != 1 {
return "", fmt.Errorf("expected 1 organization, got %d", len(orgs))
return "", xerrors.Errorf("expected 1 organization, got %d", len(orgs))
}
return fmt.Sprintf("Organization: %s (%s)\n", orgs[0].Name, orgs[0].ID.String()), nil
}
Expand Down
5 changes: 3 additions & 2 deletions cli/organizationmanage.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/google/uuid"
"golang.org/x/xerrors"

"github.com/coder/coder/v2/cli/clibase"
"github.com/coder/coder/v2/cli/cliui"
Expand Down Expand Up @@ -34,7 +35,7 @@ func (r *RootCmd) createOrganization() *clibase.Cmd {
// from creating it.
existing, _ := client.OrganizationByName(inv.Context(), orgName)
if existing.ID != uuid.Nil {
return fmt.Errorf("organization %q already exists", orgName)
return xerrors.Errorf("organization %q already exists", orgName)
}

_, err := cliui.Prompt(inv, cliui.PromptOptions{
Expand All @@ -53,7 +54,7 @@ func (r *RootCmd) createOrganization() *clibase.Cmd {
Name: orgName,
})
if err != nil {
return fmt.Errorf("failed to create organization: %w", err)
return xerrors.Errorf("failed to create organization: %w", err)
}

_, _ = fmt.Fprintf(inv.Stdout, "Organization %s (%s) created.\n", organization.Name, organization.ID)
Expand Down
2 changes: 1 addition & 1 deletion cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ func CurrentOrganization(r *RootCmd, inv *clibase.Invocation, client *codersdk.C
if selected == "" && conf.Organization().Exists() {
org, err := conf.Organization().Read()
if err != nil {
return codersdk.Organization{}, fmt.Errorf("read selected organization from config file %q: %w", conf.Organization(), err)
return codersdk.Organization{}, xerrors.Errorf("read selected organization from config file %q: %w", conf.Organization(), err)
}
selected = org
}
Expand Down