From 3bee6d0673846301fd3aa3882adf5a70c76ef2e2 Mon Sep 17 00:00:00 2001 From: Charlie Moog Date: Wed, 7 Oct 2020 18:58:30 -0500 Subject: [PATCH 1/2] Silence cobra error handling and use flog.Fatal --- cmd/coder/main.go | 4 +--- internal/cmd/ceapi.go | 5 +---- internal/cmd/cmd.go | 6 ++++-- internal/cmd/login.go | 5 +---- internal/cmd/shell.go | 2 +- internal/cmd/urls.go | 3 +-- 6 files changed, 9 insertions(+), 16 deletions(-) diff --git a/cmd/coder/main.go b/cmd/coder/main.go index 5f2c0c9c..1147a87e 100644 --- a/cmd/coder/main.go +++ b/cmd/coder/main.go @@ -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) } } diff --git a/internal/cmd/ceapi.go b/internal/cmd/ceapi.go index 2da0dd25..a412785e 100644 --- a/internal/cmd/ceapi.go +++ b/internal/cmd/ceapi.go @@ -5,8 +5,6 @@ import ( "cdr.dev/coder-cli/coder-sdk" "golang.org/x/xerrors" - - "go.coder.com/flog" ) // Helpers for working with the Coder Enterprise API. @@ -73,7 +71,6 @@ 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 } diff --git a/internal/cmd/cmd.go b/internal/cmd/cmd.go index 30bfa1ee..8917b22e 100644 --- a/internal/cmd/cmd.go +++ b/internal/cmd/cmd.go @@ -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( diff --git a/internal/cmd/login.go b/internal/cmd/login.go index 857a1bb1..a1740b48 100644 --- a/internal/cmd/login.go +++ b/internal/cmd/login.go @@ -6,7 +6,6 @@ import ( "net" "net/http" "net/url" - "os" "strings" "cdr.dev/coder-cli/coder-sdk" @@ -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 }, } diff --git a/internal/cmd/shell.go b/internal/cmd/shell.go index 3f34075a..ceb8c88f 100644 --- a/internal/cmd/shell.go +++ b/internal/cmd/shell.go @@ -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 } diff --git a/internal/cmd/urls.go b/internal/cmd/urls.go index 7a3300fc..b79ddaa8 100644 --- a/internal/cmd/urls.go +++ b/internal/cmd/urls.go @@ -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 } From efc1f6e4aca956c63c17909086ae693252785208 Mon Sep 17 00:00:00 2001 From: Charlie Moog Date: Tue, 20 Oct 2020 23:08:29 -0500 Subject: [PATCH 2/2] Improve environment not found error message --- internal/cmd/ceapi.go | 15 ++++++++++++++- internal/cmd/envs.go | 2 +- internal/cmd/shell.go | 2 +- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/internal/cmd/ceapi.go b/internal/cmd/ceapi.go index a412785e..e15b101d 100644 --- a/internal/cmd/ceapi.go +++ b/internal/cmd/ceapi.go @@ -2,6 +2,7 @@ package cmd import ( "context" + "fmt" "cdr.dev/coder-cli/coder-sdk" "golang.org/x/xerrors" @@ -72,5 +73,17 @@ func findEnv(ctx context.Context, client *coder.Client, envName, userEmail strin found = append(found, env.Name) } - 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) } diff --git a/internal/cmd/envs.go b/internal/cmd/envs.go index b67ab1a8..b18be8a6 100644 --- a/internal/cmd/envs.go +++ b/internal/cmd/envs.go @@ -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 }, diff --git a/internal/cmd/shell.go b/internal/cmd/shell.go index ceb8c88f..4e08edf0 100644 --- a/internal/cmd/shell.go +++ b/internal/cmd/shell.go @@ -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) } 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