Skip to content

Commit 9a7aba6

Browse files
committed
Switch error message
1 parent bb2bf18 commit 9a7aba6

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

cli/organization.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"errors"
55
"fmt"
66
"os"
7+
"slices"
8+
"strings"
79

810
"github.com/coder/coder/v2/cli/clibase"
911
"github.com/coder/coder/v2/cli/cliui"
@@ -21,6 +23,7 @@ func (r *RootCmd) organizations() *clibase.Cmd {
2123
},
2224
Children: []*clibase.Cmd{
2325
r.currentOrganization(),
26+
r.switchOrganization(),
2427
},
2528
}
2629

@@ -51,13 +54,44 @@ func (r *RootCmd) switchOrganization() *clibase.Cmd {
5154
),
5255
Options: clibase.OptionSet{},
5356
Handler: func(inv *clibase.Invocation) error {
57+
orgArg := inv.Args[0]
5458
conf := r.createConfig()
59+
orgs, err := client.OrganizationsByUser(inv.Context(), codersdk.Me)
60+
if err != nil {
61+
return fmt.Errorf("failed to get organizations: %w", err)
62+
}
63+
5564
// If the user passes an empty string, we want to remove the organization
56-
if inv.Args[0] == "" {
65+
if orgArg == "" {
5766
err := conf.Organization().Delete()
5867
if err != nil && !errors.Is(err, os.ErrNotExist) {
5968
return fmt.Errorf("failed to unset organization: %w", err)
6069
}
70+
} else {
71+
index := slices.IndexFunc(orgs, func(org codersdk.Organization) bool {
72+
return org.Name == orgArg || org.ID.String() == orgArg
73+
})
74+
if index < 0 {
75+
names := make([]string, 0, len(orgs))
76+
for _, org := range orgs {
77+
names = append(names, org.Name)
78+
}
79+
80+
// Using this error for better error message formatting
81+
err := &codersdk.Error{
82+
Response: codersdk.Response{
83+
Message: fmt.Sprintf("Organization %q not found.", orgArg),
84+
Detail: "Ensure the organization argument is correct and you are a member of it.",
85+
},
86+
Helper: fmt.Sprintf("Valid organizations you can switch to: %q", strings.Join(names, ", ")),
87+
}
88+
return err
89+
}
90+
91+
err := conf.Organization().Write(orgs[index].ID.String())
92+
if err != nil {
93+
return fmt.Errorf("failed to write organization to config file: %w", err)
94+
}
6195
}
6296

6397
return nil

cli/root.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,8 +1192,12 @@ func formatRunCommandError(err *clibase.RunCommandError, opts *formatOpts) strin
11921192
func formatCoderSDKError(from string, err *codersdk.Error, opts *formatOpts) string {
11931193
var str strings.Builder
11941194
if opts.Verbose {
1195-
_, _ = str.WriteString(pretty.Sprint(headLineStyle(), fmt.Sprintf("API request error to \"%s:%s\". Status code %d", err.Method(), err.URL(), err.StatusCode())))
1196-
_, _ = str.WriteString("\n")
1195+
// If all these fields are empty, then do not print this information.
1196+
// This can occur if the error is being used outside the api.
1197+
if !(err.Method() == "" && err.URL() == "" && err.StatusCode() == 0) {
1198+
_, _ = str.WriteString(pretty.Sprint(headLineStyle(), fmt.Sprintf("API request error to \"%s:%s\". Status code %d", err.Method(), err.URL(), err.StatusCode())))
1199+
_, _ = str.WriteString("\n")
1200+
}
11971201
}
11981202
// Always include this trace. Users can ignore this.
11991203
if from != "" {

0 commit comments

Comments
 (0)