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

Silence cobra error handling and use flog.Fatal #134

Merged
merged 2 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions cmd/coder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ func main() {
app.Version = fmt.Sprintf("%s %s %s/%s", version, runtime.Version(), runtime.GOOS, runtime.GOARCH)

if err := app.ExecuteContext(ctx); err != nil {
// NOTE: The returned error is already handled and logged by the cmd lib (cobra), so no need to re-handle it here.
// As we are in the main, if there was an error, exit the process with an error code.
os.Exit(1)
flog.Fatal("%v", err)
}
}
20 changes: 15 additions & 5 deletions internal/cmd/ceapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package cmd

import (
"context"
"fmt"

"cdr.dev/coder-cli/coder-sdk"
"golang.org/x/xerrors"

"go.coder.com/flog"
)

// Helpers for working with the Coder Enterprise API.
Expand Down Expand Up @@ -73,7 +72,18 @@ func findEnv(ctx context.Context, client *coder.Client, envName, userEmail strin
// Keep track of what we found for the logs.
found = append(found, env.Name)
}
flog.Error("found %q", found)
flog.Error("%q not found", envName)
return nil, coder.ErrNotFound

return nil, notFoundButDidFind{
needle: envName,
haystack: found,
}
}

type notFoundButDidFind struct {
needle string
haystack []string
}

func (n notFoundButDidFind) Error() string {
return fmt.Sprintf("\"%s\" not found in %q: %v", n.needle, n.haystack, coder.ErrNotFound)
}
6 changes: 4 additions & 2 deletions internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ var verbose bool = false
// Make constructs the "coder" root command
func Make() *cobra.Command {
app := &cobra.Command{
Use: "coder",
Short: "coder provides a CLI for working with an existing Coder Enterprise installation",
Use: "coder",
Short: "coder provides a CLI for working with an existing Coder Enterprise installation",
SilenceErrors: true,
SilenceUsage: true,
}

app.AddCommand(
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/envs.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ coder envs --user charlie@coder.com ls -o json \
}

if err = egroup.Wait(); err != nil {
return xerrors.Errorf("some stop operations failed: %w", err)
return xerrors.Errorf("some stop operations failed")
}
return nil
},
Expand Down
5 changes: 1 addition & 4 deletions internal/cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"net"
"net/http"
"net/url"
"os"
"strings"

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

if err := login(cmd, u, config.URL, config.Session); err != nil {
flog.Error("Login error: %s.", err)
os.Exit(1)
return xerrors.Errorf("Login error", err)
}

return nil
},
}
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func shell(_ *cobra.Command, cmdArgs []string) error {
if exitErr, ok := err.(wsep.ExitError); ok {
os.Exit(exitErr.Code)
}
flog.Fatal("%+v", err)
return xerrors.Errorf("run command: %w", err)
}
return nil
}
Expand Down Expand Up @@ -153,7 +153,7 @@ func runCommand(ctx context.Context, envName, command string, args []string) err
if err != nil {
var closeErr websocket.CloseError
if xerrors.As(err, &closeErr) {
return xerrors.Errorf("network error, is %q online? (%w)", envName, err)
return xerrors.Errorf("network error, is %q online?", envName)
}
return xerrors.Errorf("start remote command: %w", err)
}
Expand Down
3 changes: 1 addition & 2 deletions internal/cmd/urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ func validatePort(port string) (int, error) {
}
if p < 1 {
// Port 0 means 'any free port', which we don't support.
flog.Error("Port must be > 0")
return 0, strconv.ErrRange
return 0, xerrors.New("Port must be > 0")
}
return int(p), nil
}
Expand Down