Skip to content

Commit

Permalink
Fix env resolution priority (#282)
Browse files Browse the repository at this point in the history
* Fix env resolution priority

Fixes #269

* Defer GO_ENV rollback only once
  • Loading branch information
stanislas-m committed Nov 1, 2018
1 parent 9c88834 commit d1727d5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
5 changes: 4 additions & 1 deletion soda/cmd/root.go
Expand Up @@ -19,7 +19,10 @@ var RootCmd = &cobra.Command{
Short: "A tasty treat for all your database needs",
PersistentPreRun: func(c *cobra.Command, args []string) {
fmt.Printf("%s\n\n", Version)
env = defaults.String(os.Getenv("GO_ENV"), env)
// CLI flag has priority
if !c.PersistentFlags().Changed("env") {
env = defaults.String(os.Getenv("GO_ENV"), env)
}
setConfigLocation()
},
Run: func(cmd *cobra.Command, args []string) {
Expand Down
38 changes: 38 additions & 0 deletions soda/cmd/root_integration_test.go
@@ -0,0 +1,38 @@
package cmd

import (
"os"
"testing"

"github.com/stretchr/testify/require"
)

func Test_RootCmd_NoArg(t *testing.T) {
oldEnv := os.Getenv("GO_ENV")
defer os.Setenv("GO_ENV", oldEnv)

// Fallback on default env
r := require.New(t)
c := RootCmd
c.SetArgs([]string{})
err := c.Execute()
r.NoError(err)
r.Equal("development", env)

// Override with GO_ENV
c.SetArgs([]string{})
os.Setenv("GO_ENV", "test")
err = c.Execute()
r.NoError(err)
r.Equal("test", env)

// CLI flag priority
c.SetArgs([]string{
"--env",
"production",
})
os.Setenv("GO_ENV", "test")
err = c.Execute()
r.NoError(err)
r.Equal("production", env)
}

0 comments on commit d1727d5

Please sign in to comment.