Skip to content

Commit

Permalink
Manually check for env variables and disable the automatic ones (#256)
Browse files Browse the repository at this point in the history
* Manually check for env variables and disable the automatic ones

* Add key validator for env variables
  • Loading branch information
tomer-stripe committed Oct 29, 2019
1 parent eade698 commit e65d7e2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
4 changes: 0 additions & 4 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
log "github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/stripe/stripe-cli/pkg/cmd/resource"
"github.com/stripe/stripe-cli/pkg/config"
Expand Down Expand Up @@ -92,9 +91,6 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&Config.Profile.ProfileName, "project-name", "p", "default", "the project name to read from for config")
rootCmd.Flags().BoolP("version", "v", false, "Get the version of the Stripe CLI")

viper.SetEnvPrefix("stripe")
viper.AutomaticEnv() // read in environment variables that match

rootCmd.AddCommand(newCompletionCmd().cmd)
rootCmd.AddCommand(newConfigCmd().cmd)
rootCmd.AddCommand(newDeleteCmd().reqs.Cmd)
Expand Down
15 changes: 15 additions & 0 deletions pkg/config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -55,6 +56,10 @@ func (p *Profile) GetColor() (string, error) {

// GetDeviceName returns the configured device name
func (p *Profile) GetDeviceName() (string, error) {
if os.Getenv("STRIPE_DEVICE_NAME") != "" {
return os.Getenv("STRIPE_DEVICE_NAME"), nil
}

if p.DeviceName != "" {
return p.DeviceName, nil
}
Expand All @@ -68,6 +73,16 @@ func (p *Profile) GetDeviceName() (string, error) {

// GetAPIKey will return the existing key for the given profile
func (p *Profile) GetAPIKey(livemode bool) (string, error) {
envKey := os.Getenv("STRIPE_API_KEY")
if envKey != "" {
err := validators.APIKey(envKey)
if err != nil {
return "", err
}

return envKey, nil
}

if p.APIKey != "" {
err := validators.APIKey(p.APIKey)
if err != nil {
Expand Down

0 comments on commit e65d7e2

Please sign in to comment.