diff --git a/pkg/config/profile.go b/pkg/config/profile.go index 71b4786e3..8b86375ea 100644 --- a/pkg/config/profile.go +++ b/pkg/config/profile.go @@ -7,6 +7,7 @@ import ( "strings" "time" + log "github.com/sirupsen/logrus" "github.com/spf13/viper" "github.com/stripe/stripe-cli/pkg/validators" @@ -135,7 +136,7 @@ func (p *Profile) GetAPIKey(livemode bool) (string, error) { p.RegisterAlias(TestModeAPIKeyName, "api_key") } - if err := viper.ReadInConfig(); err == nil { + if err = viper.ReadInConfig(); err == nil { key = viper.GetString(p.GetConfigField(TestModeAPIKeyName)) } } else { @@ -153,6 +154,13 @@ func (p *Profile) GetAPIKey(livemode bool) (string, error) { return key, nil } + // If the log level is debug, return the raw error message. otherwise + // return a generic configuration message + level := log.GetLevel() + if level == log.DebugLevel { + return "", err + } + return "", validators.ErrAPIKeyNotConfigured } diff --git a/pkg/config/profile_test.go b/pkg/config/profile_test.go index be40bf9bb..e7eb5ad64 100644 --- a/pkg/config/profile_test.go +++ b/pkg/config/profile_test.go @@ -5,9 +5,12 @@ import ( "io/ioutil" "os" "path/filepath" + "runtime" "testing" + "github.com/sirupsen/logrus" "github.com/spf13/viper" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -101,6 +104,43 @@ test_mode_key_expires_at = '` + expiresAt + `' cleanUp(c.ProfilesFile) } +func TestAPIKeyLogLevel(t *testing.T) { + // Set the level to debug + logrus.SetLevel(logrus.DebugLevel) + + c := &Config{ + Color: "auto", + LogLevel: "debug", + Profile: Profile{ + ProfileName: "tests", + TestModeAPIKey: "asdas", + }, + ProfilesFile: "", + } + + // For debug mode, the error should complain about a config file missing + // since we did not init the config + key, err := c.Profile.GetAPIKey(false) + + // Little weird but windows returns a different error message than others + os := runtime.GOOS + switch os { + case "windows": + assert.ErrorContains(t, err, `config.toml: The system cannot find the file specified.`) + default: + assert.ErrorContains(t, err, `config.toml: no such file or directory`) + } + + assert.Equal(t, "", key) + + // In info mode, it should give a cleaner error about the key not being + // configured + logrus.SetLevel(logrus.InfoLevel) + key, err = c.Profile.GetAPIKey(false) + assert.EqualError(t, err, "you have not configured API keys yet") + assert.Equal(t, "", key) +} + func helperLoadBytes(t *testing.T, name string) []byte { bytes, err := ioutil.ReadFile(name) if err != nil {