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
This repository was archived by the owner on Aug 30, 2024. It is now read-only.
/coder-v1-cliPublic archive

Commitc9043b7

Browse files
authored
Silence cobra error handling and use flog.Fatal (#134)
* Silence cobra error handling and use flog.Fatal* Improve environment not found error message
1 parent282f351 commitc9043b7

File tree

7 files changed

+25
-19
lines changed

7 files changed

+25
-19
lines changed

‎cmd/coder/main.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ func main() {
4242
app.Version=fmt.Sprintf("%s %s %s/%s",version,runtime.Version(),runtime.GOOS,runtime.GOARCH)
4343

4444
iferr:=app.ExecuteContext(ctx);err!=nil {
45-
// NOTE: The returned error is already handled and logged by the cmd lib (cobra), so no need to re-handle it here.
46-
// As we are in the main, if there was an error, exit the process with an error code.
47-
os.Exit(1)
45+
flog.Fatal("%v",err)
4846
}
4947
}

‎internal/cmd/ceapi.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ package cmd
22

33
import (
44
"context"
5+
"fmt"
56

67
"cdr.dev/coder-cli/coder-sdk"
78
"golang.org/x/xerrors"
8-
9-
"go.coder.com/flog"
109
)
1110

1211
// Helpers for working with the Coder Enterprise API.
@@ -73,7 +72,18 @@ func findEnv(ctx context.Context, client *coder.Client, envName, userEmail strin
7372
// Keep track of what we found for the logs.
7473
found=append(found,env.Name)
7574
}
76-
flog.Error("found %q",found)
77-
flog.Error("%q not found",envName)
78-
returnnil,coder.ErrNotFound
75+
76+
returnnil,notFoundButDidFind{
77+
needle:envName,
78+
haystack:found,
79+
}
80+
}
81+
82+
typenotFoundButDidFindstruct {
83+
needlestring
84+
haystack []string
85+
}
86+
87+
func (nnotFoundButDidFind)Error()string {
88+
returnfmt.Sprintf("\"%s\" not found in %q: %v",n.needle,n.haystack,coder.ErrNotFound)
7989
}

‎internal/cmd/cmd.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ var verbose bool = false
1313
// Make constructs the "coder" root command
1414
funcMake()*cobra.Command {
1515
app:=&cobra.Command{
16-
Use:"coder",
17-
Short:"coder provides a CLI for working with an existing Coder Enterprise installation",
16+
Use:"coder",
17+
Short:"coder provides a CLI for working with an existing Coder Enterprise installation",
18+
SilenceErrors:true,
19+
SilenceUsage:true,
1820
}
1921

2022
app.AddCommand(

‎internal/cmd/envs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ coder envs --user charlie@coder.com ls -o json \
111111
}
112112

113113
iferr=egroup.Wait();err!=nil {
114-
returnxerrors.Errorf("some stop operations failed: %w",err)
114+
returnxerrors.Errorf("some stop operations failed")
115115
}
116116
returnnil
117117
},

‎internal/cmd/login.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"net"
77
"net/http"
88
"net/url"
9-
"os"
109
"strings"
1110

1211
"cdr.dev/coder-cli/coder-sdk"
@@ -42,10 +41,8 @@ func makeLoginCmd() *cobra.Command {
4241
// Don't return errors as it would print the usage.
4342

4443
iferr:=login(cmd,u,config.URL,config.Session);err!=nil {
45-
flog.Error("Login error: %s.",err)
46-
os.Exit(1)
44+
returnxerrors.Errorf("Login error",err)
4745
}
48-
4946
returnnil
5047
},
5148
}

‎internal/cmd/shell.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func shell(_ *cobra.Command, cmdArgs []string) error {
7171
ifexitErr,ok:=err.(wsep.ExitError);ok {
7272
os.Exit(exitErr.Code)
7373
}
74-
flog.Fatal("%+v",err)
74+
returnxerrors.Errorf("run command: %w",err)
7575
}
7676
returnnil
7777
}
@@ -153,7 +153,7 @@ func runCommand(ctx context.Context, envName, command string, args []string) err
153153
iferr!=nil {
154154
varcloseErr websocket.CloseError
155155
ifxerrors.As(err,&closeErr) {
156-
returnxerrors.Errorf("network error, is %q online? (%w)",envName,err)
156+
returnxerrors.Errorf("network error, is %q online?",envName)
157157
}
158158
returnxerrors.Errorf("start remote command: %w",err)
159159
}

‎internal/cmd/urls.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ func validatePort(port string) (int, error) {
7575
}
7676
ifp<1 {
7777
// Port 0 means 'any free port', which we don't support.
78-
flog.Error("Port must be > 0")
79-
return0,strconv.ErrRange
78+
return0,xerrors.New("Port must be > 0")
8079
}
8180
returnint(p),nil
8281
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp