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

Commit b2fe134

Browse files
committed
Silence cobra error handling and use flog.Fatal
1 parent b7cd44f commit b2fe134

File tree

6 files changed

+9
-16
lines changed

6 files changed

+9
-16
lines changed

cmd/coder/main.go

+1-3
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

+1-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55

66
"cdr.dev/coder-cli/coder-sdk"
77
"golang.org/x/xerrors"
8-
9-
"go.coder.com/flog"
108
)
119

1210
// Helpers for working with the Coder Enterprise API.
@@ -73,7 +71,6 @@ func findEnv(ctx context.Context, client *coder.Client, envName, userEmail strin
7371
// Keep track of what we found for the logs.
7472
found = append(found, env.Name)
7573
}
76-
flog.Error("found %q", found)
77-
flog.Error("%q not found", envName)
74+
7875
return nil, coder.ErrNotFound
7976
}

internal/cmd/cmd.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import (
1010
// Make constructs the "coder" root command
1111
func Make() *cobra.Command {
1212
app := &cobra.Command{
13-
Use: "coder",
14-
Short: "coder provides a CLI for working with an existing Coder Enterprise installation",
13+
Use: "coder",
14+
Short: "coder provides a CLI for working with an existing Coder Enterprise installation",
15+
SilenceErrors: true,
16+
SilenceUsage: true,
1517
}
1618

1719
app.AddCommand(

internal/cmd/login.go

+1-4
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

+1-1
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
}

internal/cmd/urls.go

+1-2
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)