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
}

View File

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

View File

@ -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
}