Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ default_cassette.yaml
.vscode/*.log
stripe-completion.bash
stripe-completion.zsh
.env
43 changes: 31 additions & 12 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/BurntSushi/toml"
"github.com/mitchellh/go-homedir"
log "github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/spf13/viper"
prefixed "github.com/x-cray/logrus-prefixed-formatter"

Expand Down Expand Up @@ -109,18 +110,36 @@ func (c *Config) InitConfig() {
if c.ProfilesFile != "" {
viper.SetConfigFile(c.ProfilesFile)
} else {
configFolder := c.GetConfigFolder(os.Getenv("XDG_CONFIG_HOME"))
configFile := filepath.Join(configFolder, "config.toml")
c.ProfilesFile = configFile
viper.SetConfigType("toml")
viper.SetConfigFile(configFile)
viper.SetConfigPermissions(os.FileMode(0600))

// Try to change permissions manually, because we used to create files
// with default permissions (0644)
err := os.Chmod(configFile, os.FileMode(0600))
if err != nil && !os.IsNotExist(err) {
log.Fatalf("%s", err)
currPath, _ := os.Getwd()
envFile := filepath.Join(currPath, ".env")
var fs = afero.NewOsFs()
envFileExists, _ := afero.Exists(fs, envFile)
if envFileExists {
c.ProfilesFile = envFile
viper.SetConfigFile(envFile)
if err := viper.ReadInConfig(); err == nil {
log.WithFields(log.Fields{
"prefix": "config.Config.InitConfig",
"path": viper.ConfigFileUsed(),
}).Debug("Using .env file")
}
}

// If the STRIPE_API_KEY is not set, check/create the config.toml profile file
if !viper.IsSet("STRIPE_API_KEY") {
configFolder := c.GetConfigFolder(os.Getenv("XDG_CONFIG_HOME"))
configFile := filepath.Join(configFolder, "config.toml")
c.ProfilesFile = configFile
viper.SetConfigType("toml")
viper.SetConfigFile(configFile)
viper.SetConfigPermissions(os.FileMode(0600))

// Try to change permissions manually, because we used to create files
// with default permissions (0644)
err := os.Chmod(configFile, os.FileMode(0600))
if err != nil && !os.IsNotExist(err) {
log.Fatalf("%s", err)
}
}
}

Expand Down
16 changes: 16 additions & 0 deletions pkg/config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/99designs/keyring"
"github.com/spf13/afero"
"github.com/spf13/viper"

"github.com/stripe/stripe-cli/pkg/ansi"
Expand Down Expand Up @@ -141,6 +142,21 @@ func (p *Profile) GetAccountID() (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")

// Check if there is a .env file with the STRIPE_API_KEY
currPath, _ := os.Getwd()
envFile := filepath.Join(currPath, ".env")
var fs = afero.NewOsFs()
envFileExists, _ := afero.Exists(fs, envFile)
if envFileExists {
viper.SetConfigFile(envFile)
if err := viper.ReadInConfig(); err == nil {
if envApiKey := viper.GetString("STRIPE_API_KEY"); envApiKey != "" {
envKey = envApiKey
}
}
}

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