Skip to content

feat: add verbose error messaging #3053

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add a lil test
  • Loading branch information
sreya committed Jul 13, 2022
commit eb57e7b8b22b31e93ed0bd399d18174aaad10aee
30 changes: 30 additions & 0 deletions cli/cliflag/cliflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ import (
"github.com/spf13/pflag"
)

// IsSetBool returns the value of the boolean flag if it is set.
// It returns false if the flag isn't set or if any error occurs attempting
// to parse the value of the flag.
func IsSetBool(cmd *cobra.Command, name string) bool {
val, ok := IsSet(cmd, name)
if !ok {
return false
}

b, err := strconv.ParseBool(val)
return err == nil && b
}

// IsSet returns the string value of the flag and whether it was set.
func IsSet(cmd *cobra.Command, name string) (string, bool) {
flag := cmd.Flag(name)
if flag == nil {
Expand Down Expand Up @@ -77,6 +91,22 @@ func Uint8VarP(flagset *pflag.FlagSet, ptr *uint8, name string, shorthand string
flagset.Uint8VarP(ptr, name, shorthand, uint8(vi64), fmtUsage(usage, env))
}

func Bool(flagset *pflag.FlagSet, name, shorthand, env string, def bool, usage string) {
val, ok := os.LookupEnv(env)
if !ok || val == "" {
flagset.BoolP(name, shorthand, def, fmtUsage(usage, env))
return
}

valb, err := strconv.ParseBool(val)
if err != nil {
flagset.BoolP(name, shorthand, def, fmtUsage(usage, env))
return
}

flagset.BoolP(name, shorthand, valb, fmtUsage(usage, env))
}

// BoolVarP sets a bool flag on the given flag set.
func BoolVarP(flagset *pflag.FlagSet, ptr *bool, name string, shorthand string, env string, def bool, usage string) {
val, ok := os.LookupEnv(env)
Expand Down
14 changes: 6 additions & 8 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ const (
varAgentURL = "agent-url"
varGlobalConfig = "global-config"
varNoOpen = "no-open"
varNoVersionCheck = "no-version-warning"
varForceTty = "force-tty"
varVerbose = "verbose"
notLoggedInMessage = "You are not logged in. Try logging in using 'coder login <url>'."

noVersionCheckFlag = "no-version-warning"
envNoVersionCheck = "CODER_NO_VERSION_WARNING"
envNoVersionCheck = "CODER_NO_VERSION_WARNING"
)

var (
Expand All @@ -59,8 +59,6 @@ func init() {
}

func Root() *cobra.Command {
var varSuppressVersion bool

cmd := &cobra.Command{
Use: "coder",
SilenceErrors: true,
Expand All @@ -69,7 +67,7 @@ func Root() *cobra.Command {
`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
err := func() error {
if varSuppressVersion {
if cliflag.IsSetBool(cmd, varNoVersionCheck) {
return nil
}

Expand Down Expand Up @@ -142,7 +140,7 @@ func Root() *cobra.Command {
cmd.SetUsageTemplate(usageTemplate())

cmd.PersistentFlags().String(varURL, "", "Specify the URL to your deployment.")
cliflag.BoolVarP(cmd.PersistentFlags(), &varSuppressVersion, noVersionCheckFlag, "", envNoVersionCheck, false, "Suppress warning when client and server versions do not match.")
cliflag.Bool(cmd.PersistentFlags(), varNoVersionCheck, "", envNoVersionCheck, false, "Suppress warning when client and server versions do not match.")
cliflag.String(cmd.PersistentFlags(), varToken, "", envSessionToken, "", fmt.Sprintf("Specify an authentication token. For security reasons setting %s is preferred.", envSessionToken))
cliflag.String(cmd.PersistentFlags(), varAgentToken, "", "CODER_AGENT_TOKEN", "", "Specify an agent authentication token.")
_ = cmd.PersistentFlags().MarkHidden(varAgentToken)
Expand All @@ -153,7 +151,7 @@ func Root() *cobra.Command {
_ = cmd.PersistentFlags().MarkHidden(varForceTty)
cmd.PersistentFlags().Bool(varNoOpen, false, "Block automatically opening URLs in the browser.")
_ = cmd.PersistentFlags().MarkHidden(varNoOpen)
cliflag.String(cmd.PersistentFlags(), varVerbose, "v", "CODER_VERBOSE", "", "Enable verbose output")
cliflag.Bool(cmd.PersistentFlags(), varVerbose, "v", "CODER_VERBOSE", false, "Enable verbose output")

return cmd
}
Expand Down Expand Up @@ -439,7 +437,7 @@ func FormatCobraError(err error, cmd *cobra.Command) string {
_, _ = fmt.Fprintln(&output, httpErr.Friendly())
}

if flag := cmd.Flag(varVerbose); flag != nil && flag.Value.String() != "" {
if cliflag.IsSetBool(cmd, varVerbose) {
_, _ = fmt.Fprintln(&output, err.Error())
}

Expand Down
39 changes: 35 additions & 4 deletions cli/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,53 @@ import (
"bytes"
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
"golang.org/x/xerrors"

"github.com/coder/coder/buildinfo"
"github.com/coder/coder/cli"
"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/codersdk"
)

func TestRoot(t *testing.T) {
t.Run("FormatCobraError", func(t *testing.T) {
t.Parallel()

cmd, _ := clitest.New(t, "delete")
t.Run("OK", func(t *testing.T) {
t.Parallel()

cmd, err := cmd.ExecuteC()
errStr := cli.FormatCobraError(err, cmd)
require.Contains(t, errStr, "Run 'coder delete --help' for usage.")
cmd, _ := clitest.New(t, "delete")

cmd, err := cmd.ExecuteC()
errStr := cli.FormatCobraError(err, cmd)
require.Contains(t, errStr, "Run 'coder delete --help' for usage.")
})

t.Run("Verbose", func(t *testing.T) {
t.Parallel()

cmd, _ := clitest.New(t, "--verbose")

cmd.RunE = func(cmd *cobra.Command, args []string) error {
var err error = &codersdk.Error{
Response: codersdk.Response{
Message: "This is a message.",
},
Helper: "Try this instead.",
}

err = xerrors.Errorf("wrap me: %w", err)

return err
}

cmd, err := cmd.ExecuteC()
errStr := cli.FormatCobraError(err, cmd)
require.Contains(t, errStr, "This is a message. Try this instead.")
require.Contains(t, errStr, err.Error())
})
})

t.Run("Version", func(t *testing.T) {
Expand Down