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

Silence cobra error handling and use flog.Fatal#134

Merged
cmoog merged 2 commits intomasterfromfix-error-fmt
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionscmd/coder/main.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsinternal/cmd/ceapi.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 deletionsinternal/cmd/cmd.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletioninternal/cmd/envs.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsinternal/cmd/login.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,7 +6,6 @@ import (
"net"
"net/http"
"net/url"
"os"
"strings"

"cdr.dev/coder-cli/coder-sdk"
Expand DownExpand 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 deletionsinternal/cmd/shell.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 deletionsinternal/cmd/urls.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp