Skip to content

Commit

Permalink
feat: Add additional help text with missing access-token (#219)
Browse files Browse the repository at this point in the history
Add additional help text with missing access-token
  • Loading branch information
dbolson authored Apr 30, 2024
1 parent 2e3445c commit b74053c
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 16 deletions.
8 changes: 8 additions & 0 deletions cmd/cmdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"bytes"
"fmt"
"io"
"os"
"testing"
Expand Down Expand Up @@ -69,3 +70,10 @@ func SetupTestEnvVars(_ *testing.T) func(t *testing.T) {
os.Unsetenv("LD_BASE_URI")
}
}

func ExtraErrorHelp(cmdName string, cmdAction string) string {
out := ".\n\nUse `ldcli config --set access-token <value>` to configure the value to persist across CLI commands."
out += fmt.Sprintf("\n\nSee `ldcli %s %s --help` for supported flags and usage.", cmdName, cmdAction)

return out
}
2 changes: 1 addition & 1 deletion cmd/environments/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func TestGet(t *testing.T) {

_, err := cmd.CallCmd(t, clients, &analytics.NoopClient{}, args)

assert.EqualError(t, err, `required flag(s) "access-token", "environment", "project" not set`+errorHelp)
assert.EqualError(t, err, `required flag(s) "access-token", "environment", "project" not set`+cmd.ExtraErrorHelp("environments", "get"))
})

t.Run("with missing short flag value is an error", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/flags/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func TestCreate(t *testing.T) {

_, err := cmd.CallCmd(t, clients, &analytics.NoopClient{}, args)

assert.EqualError(t, err, `required flag(s) "access-token", "data", "project" not set`+errorHelp)
assert.EqualError(t, err, `required flag(s) "access-token", "data", "project" not set`+cmd.ExtraErrorHelp("flags", "create"))
})

t.Run("with missing short flag value is an error", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/flags/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func TestGet(t *testing.T) {

_, err := cmd.CallCmd(t, clients, &analytics.NoopClient{}, args)

assert.EqualError(t, err, `required flag(s) "access-token", "environment", "flag", "project" not set`+errorHelp)
assert.EqualError(t, err, `required flag(s) "access-token", "environment", "flag", "project" not set`+cmd.ExtraErrorHelp("flags", "get"))
})

t.Run("with invalid base-uri is an error", func(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions cmd/flags/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func TestUpdate(t *testing.T) {

_, err := cmd.CallCmd(t, clients, &analytics.NoopClient{}, args)

assert.EqualError(t, err, `required flag(s) "access-token", "data", "flag", "project" not set`+errorHelp)
assert.EqualError(t, err, `required flag(s) "access-token", "data", "flag", "project" not set`+cmd.ExtraErrorHelp("flags", "update"))
})

t.Run("with invalid base-uri is an error", func(t *testing.T) {
Expand Down Expand Up @@ -235,7 +235,7 @@ func TestToggle(t *testing.T) {

_, err := cmd.CallCmd(t, clients, &analytics.NoopClient{}, args)

assert.EqualError(t, err, `required flag(s) "access-token", "environment", "flag", "project" not set`+errorHelp)
assert.EqualError(t, err, `required flag(s) "access-token", "environment", "flag", "project" not set`+cmd.ExtraErrorHelp("flags", "toggle-on"))
})

t.Run("with invalid base-uri is an error", func(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions cmd/members/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestCreate(t *testing.T) {

_, err := cmd.CallCmd(t, clients, &analytics.NoopClient{}, args)

assert.EqualError(t, err, `required flag(s) "access-token", "data" not set`+errorHelp)
assert.EqualError(t, err, `required flag(s) "access-token", "data" not set`+cmd.ExtraErrorHelp("members", "create"))
})

t.Run("with invalid base-uri is an error", func(t *testing.T) {
Expand All @@ -131,7 +131,7 @@ func TestCreate(t *testing.T) {
"access-token",
"base-uri",
"data",
"output",
"output",
}, analytics.SUCCESS)

client := members.MockClient{}
Expand Down
4 changes: 2 additions & 2 deletions cmd/members/invite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestInvite(t *testing.T) {

_, err := cmd.CallCmd(t, clients, &analytics.NoopClient{}, args)

assert.EqualError(t, err, `required flag(s) "access-token", "emails" not set`+errorHelp)
assert.EqualError(t, err, `required flag(s) "access-token", "emails" not set`+cmd.ExtraErrorHelp("members", "invite"))
})

t.Run("with invalid base-uri is an error", func(t *testing.T) {
Expand All @@ -131,7 +131,7 @@ func TestInvite(t *testing.T) {
"access-token",
"base-uri",
"emails",
"output",
"output",
}, analytics.SUCCESS)

client := members.MockClient{}
Expand Down
2 changes: 1 addition & 1 deletion cmd/projects/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func TestCreate(t *testing.T) {

_, err := cmd.CallCmd(t, clients, &analytics.NoopClient{}, args)

assert.EqualError(t, err, `required flag(s) "access-token", "data" not set`+errorHelp)
assert.EqualError(t, err, `required flag(s) "access-token", "data" not set`+cmd.ExtraErrorHelp("projects", "create"))
})

t.Run("with missing short flag value is an error", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/projects/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func TestList(t *testing.T) {

_, err := cmd.CallCmd(t, clients, &analytics.NoopClient{}, args)

assert.EqualError(t, err, `required flag(s) "access-token" not set`+errorHelp)
assert.EqualError(t, err, `required flag(s) "access-token" not set`+cmd.ExtraErrorHelp("projects", "list"))
})

t.Run("with missing long flag value is an error", func(t *testing.T) {
Expand Down
16 changes: 11 additions & 5 deletions cmd/validators/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"net/url"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -39,11 +40,16 @@ func Validate() cobra.PositionalArgs {
}

func CmdError(err error, commandPath string) error {
errorMessage := fmt.Sprintf(
"%s. See `%s --help` for supported flags and usage.",
err.Error(),
commandPath,
)
errorMessage := err.Error() + "."

// show additional help if a missing flag can be set in the config file
if strings.Contains(err.Error(), cliflags.AccessTokenFlag) {
errorMessage += "\n\n"
errorMessage += fmt.Sprintf("Use `ldcli config --set %s <value>` to configure the value to persist across CLI commands.\n\n", cliflags.AccessTokenFlag)
} else {
errorMessage += " "
}
errorMessage += fmt.Sprintf("See `%s --help` for supported flags and usage.", commandPath)

return errors.New(errorMessage)
}
Expand Down
37 changes: 37 additions & 0 deletions cmd/validators/validators_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package validators_test

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"

"ldcli/cmd/validators"
)

func TestCmdError(t *testing.T) {
t.Run("with missing access-token value shows additional help", func(t *testing.T) {
var expected string
expected += "required flag(s) \"access-token\" not set.\n\n"
expected += "Use `ldcli config --set access-token <value>` to configure the value to persist across CLI commands.\n\n"
expected += "See `ldcli command action --help` for supported flags and usage."

err := validators.CmdError(
errors.New(`required flag(s) "access-token" not set`),
"ldcli command action",
)

assert.EqualError(t, err, expected)
})

t.Run("with missing other flag value shows regular help", func(t *testing.T) {
expected := "required flag(s) \"my-flag\" not set. See `ldcli command action --help` for supported flags and usage."

err := validators.CmdError(
errors.New(`required flag(s) "my-flag" not set`),
"ldcli command action",
)

assert.EqualError(t, err, expected)
})
}

0 comments on commit b74053c

Please sign in to comment.