Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9a7aba6

Browse files
committed
Switch error message
1 parentbb2bf18 commit9a7aba6

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+
iferr!=nil {
61+
returnfmt.Errorf("failed to get organizations: %w",err)
62+
}
63+
5564
// If the user passes an empty string, we want to remove the organization
56-
ifinv.Args[0]=="" {
65+
iforgArg=="" {
5766
err:=conf.Organization().Delete()
5867
iferr!=nil&&!errors.Is(err,os.ErrNotExist) {
5968
returnfmt.Errorf("failed to unset organization: %w",err)
6069
}
70+
}else {
71+
index:=slices.IndexFunc(orgs,func(org codersdk.Organization)bool {
72+
returnorg.Name==orgArg||org.ID.String()==orgArg
73+
})
74+
ifindex<0 {
75+
names:=make([]string,0,len(orgs))
76+
for_,org:=rangeorgs {
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+
returnerr
89+
}
90+
91+
err:=conf.Organization().Write(orgs[index].ID.String())
92+
iferr!=nil {
93+
returnfmt.Errorf("failed to write organization to config file: %w",err)
94+
}
6195
}
6296

6397
returnnil

‎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
funcformatCoderSDKError(fromstring,err*codersdk.Error,opts*formatOpts)string {
11931193
varstr strings.Builder
11941194
ifopts.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
iffrom!="" {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp