Skip to content

Commit 9f3591a

Browse files
authored
chore(cli): use xerrors.Errorf instead of fmt.Errorf (coder#12368)
1 parent cbcf4ef commit 9f3591a

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

cli/organization.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"slices"
88
"strings"
99

10+
"golang.org/x/xerrors"
11+
1012
"github.com/coder/coder/v2/cli/clibase"
1113
"github.com/coder/coder/v2/cli/cliui"
1214
"github.com/coder/coder/v2/cli/config"
@@ -60,7 +62,7 @@ func (r *RootCmd) switchOrganization() *clibase.Cmd {
6062
conf := r.createConfig()
6163
orgs, err := client.OrganizationsByUser(inv.Context(), codersdk.Me)
6264
if err != nil {
63-
return fmt.Errorf("failed to get organizations: %w", err)
65+
return xerrors.Errorf("failed to get organizations: %w", err)
6466
}
6567
// Keep the list of orgs sorted
6668
slices.SortFunc(orgs, func(a, b codersdk.Organization) int {
@@ -84,7 +86,7 @@ func (r *RootCmd) switchOrganization() *clibase.Cmd {
8486
if switchToOrg == "" {
8587
err := conf.Organization().Delete()
8688
if err != nil && !errors.Is(err, os.ErrNotExist) {
87-
return fmt.Errorf("failed to unset organization: %w", err)
89+
return xerrors.Errorf("failed to unset organization: %w", err)
8890
}
8991
_, _ = fmt.Fprintf(inv.Stdout, "Organization unset\n")
9092
} else {
@@ -107,7 +109,7 @@ func (r *RootCmd) switchOrganization() *clibase.Cmd {
107109
// Always write the uuid to the config file. Names can change.
108110
err := conf.Organization().Write(orgs[index].ID.String())
109111
if err != nil {
110-
return fmt.Errorf("failed to write organization to config file: %w", err)
112+
return xerrors.Errorf("failed to write organization to config file: %w", err)
111113
}
112114
}
113115

@@ -123,7 +125,7 @@ func (r *RootCmd) switchOrganization() *clibase.Cmd {
123125
}
124126
return sdkError
125127
}
126-
return fmt.Errorf("failed to get current organization: %w", err)
128+
return xerrors.Errorf("failed to get current organization: %w", err)
127129
}
128130

129131
_, _ = 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 {
213215
typed, ok := data.([]codersdk.Organization)
214216
if !ok {
215217
// This should never happen
216-
return "", fmt.Errorf("expected []Organization, got %T", data)
218+
return "", xerrors.Errorf("expected []Organization, got %T", data)
217219
}
218220
return stringFormat(typed)
219221
}),
@@ -250,7 +252,7 @@ func (r *RootCmd) currentOrganization() *clibase.Cmd {
250252
case "current":
251253
stringFormat = func(orgs []codersdk.Organization) (string, error) {
252254
if len(orgs) != 1 {
253-
return "", fmt.Errorf("expected 1 organization, got %d", len(orgs))
255+
return "", xerrors.Errorf("expected 1 organization, got %d", len(orgs))
254256
}
255257
return fmt.Sprintf("Current CLI Organization: %s (%s)\n", orgs[0].Name, orgs[0].ID.String()), nil
256258
}
@@ -275,7 +277,7 @@ func (r *RootCmd) currentOrganization() *clibase.Cmd {
275277
default:
276278
stringFormat = func(orgs []codersdk.Organization) (string, error) {
277279
if len(orgs) != 1 {
278-
return "", fmt.Errorf("expected 1 organization, got %d", len(orgs))
280+
return "", xerrors.Errorf("expected 1 organization, got %d", len(orgs))
279281
}
280282
return fmt.Sprintf("Organization: %s (%s)\n", orgs[0].Name, orgs[0].ID.String()), nil
281283
}

cli/organizationmanage.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55

66
"github.com/google/uuid"
7+
"golang.org/x/xerrors"
78

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

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

5960
_, _ = fmt.Fprintf(inv.Stdout, "Organization %s (%s) created.\n", organization.Name, organization.ID)

cli/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ func CurrentOrganization(r *RootCmd, inv *clibase.Invocation, client *codersdk.C
715715
if selected == "" && conf.Organization().Exists() {
716716
org, err := conf.Organization().Read()
717717
if err != nil {
718-
return codersdk.Organization{}, fmt.Errorf("read selected organization from config file %q: %w", conf.Organization(), err)
718+
return codersdk.Organization{}, xerrors.Errorf("read selected organization from config file %q: %w", conf.Organization(), err)
719719
}
720720
selected = org
721721
}

0 commit comments

Comments
 (0)