Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit c9043b7

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 parent 282f351 commit c9043b7

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
if err := 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-
return nil, coder.ErrNotFound
75+
76+
return nil, notFoundButDidFind{
77+
needle: envName,
78+
haystack: found,
79+
}
80+
}
81+
82+
type notFoundButDidFind struct {
83+
needle string
84+
haystack []string
85+
}
86+
87+
func (n notFoundButDidFind) Error() string {
88+
return fmt.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
func Make() *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
if err = egroup.Wait(); err != nil {
114-
return xerrors.Errorf("some stop operations failed: %w", err)
114+
return xerrors.Errorf("some stop operations failed")
115115
}
116116
return nil
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
if err := login(cmd, u, config.URL, config.Session); err != nil {
45-
flog.Error("Login error: %s.", err)
46-
os.Exit(1)
44+
return xerrors.Errorf("Login error", err)
4745
}
48-
4946
return nil
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
if exitErr, ok := err.(wsep.ExitError); ok {
7272
os.Exit(exitErr.Code)
7373
}
74-
flog.Fatal("%+v", err)
74+
return xerrors.Errorf("run command: %w", err)
7575
}
7676
return nil
7777
}
@@ -153,7 +153,7 @@ func runCommand(ctx context.Context, envName, command string, args []string) err
153153
if err != nil {
154154
var closeErr websocket.CloseError
155155
if xerrors.As(err, &closeErr) {
156-
return xerrors.Errorf("network error, is %q online? (%w)", envName, err)
156+
return xerrors.Errorf("network error, is %q online?", envName)
157157
}
158158
return xerrors.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
if p < 1 {
7777
// Port 0 means 'any free port', which we don't support.
78-
flog.Error("Port must be > 0")
79-
return 0, strconv.ErrRange
78+
return 0, xerrors.New("Port must be > 0")
8079
}
8180
return int(p), nil
8281
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy