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

This commit is contained in:
Cian Johnston
2024-02-29 18:58:48 +00:00
committed by GitHub
parent cbcf4ef2c4
commit 9f3591add8
3 changed files with 13 additions and 10 deletions

View File

@ -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"
@ -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 {
@ -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 {
@ -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)
}
}
@ -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())
@ -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)
}),
@ -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
}
@ -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
}