Skip to content

Commit

Permalink
Merge pull request #343 from DopplerHQ/watsonian/config-dir-creation-…
Browse files Browse the repository at this point in the history
…change

Don't create default config dir if --configuration used
  • Loading branch information
watsonian authored Nov 7, 2022
2 parents 26ee6ef + 34ce96e commit fb2df93
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
8 changes: 8 additions & 0 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func loadFlags(cmd *cobra.Command) {
configuration.Scope = normalizedScope

configuration.CanReadEnv = !utils.GetBoolFlag(cmd, "no-read-env")
configuration.SetConfigDir(utils.GetPathFlagIfChanged(cmd, "config-dir", configuration.UserConfigDir))
configuration.UserConfigFile = utils.GetPathFlagIfChanged(cmd, "configuration", configuration.UserConfigFile)
http.UseTimeout = !utils.GetBoolFlag(cmd, "no-timeout")

Expand Down Expand Up @@ -216,7 +217,14 @@ func init() {

rootCmd.PersistentFlags().Bool("no-read-env", false, "do not read config from the environment")
rootCmd.PersistentFlags().String("scope", configuration.Scope, "the directory to scope your config to")
rootCmd.PersistentFlags().String("config-dir", configuration.UserConfigDir, "config directory")
rootCmd.PersistentFlags().String("configuration", configuration.UserConfigFile, "config file")
if err := rootCmd.PersistentFlags().MarkDeprecated("configuration", "please use --config-dir instead"); err != nil {
utils.HandleError(err)
}
if err := rootCmd.PersistentFlags().MarkHidden("configuration"); err != nil {
utils.HandleError(err)
}
rootCmd.PersistentFlags().BoolVar(&utils.OutputJSON, "json", utils.OutputJSON, "output json")
rootCmd.PersistentFlags().BoolVar(&utils.Debug, "debug", utils.Debug, "output additional information")
rootCmd.PersistentFlags().BoolVar(&printConfig, "print-config", printConfig, "output active configuration")
Expand Down
11 changes: 6 additions & 5 deletions pkg/configuration/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ import (
"gopkg.in/yaml.v3"
)

// baseConfigDir (e.g. /home/user/)
var baseConfigDir string

// UserConfigDir (e.g. /home/user/.doppler)
var UserConfigDir string

Expand All @@ -53,13 +50,17 @@ var configUid = -1
var configGid = -1

func init() {
baseConfigDir = utils.HomeDir()
UserConfigDir = filepath.Join(baseConfigDir, ".doppler")
SetConfigDir(filepath.Join(utils.HomeDir(), ".doppler"))
}

func SetConfigDir(dir string) {
UserConfigDir = dir
UserConfigFile = filepath.Join(UserConfigDir, configFileName)
}

// Setup the config directory and config file
func Setup() {
utils.LogDebug(fmt.Sprintf("Using config dir %s", UserConfigDir))
utils.LogDebug(fmt.Sprintf("Using config file %s", UserConfigFile))

if !utils.Exists(UserConfigDir) {
Expand Down
18 changes: 17 additions & 1 deletion tests/e2e/configure.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ beforeAll() {
}

beforeEach() {
rm -f ./temp-config
rm -rf ./temp-config ./temp-config-dir
}

afterAll() {
Expand Down Expand Up @@ -70,6 +70,22 @@ config="$("$DOPPLER_BINARY" configure get config --configuration=./temp-config -

beforeEach

# test configure w/ custom config-dir
mkdir ./temp-config-dir
"$DOPPLER_BINARY" configure set config 123 --config-dir=./temp-config-dir --scope=/ --silent
config="$("$DOPPLER_BINARY" configure get config --configuration=./temp-config-dir/.doppler.yaml --scope=/ --plain)"
[[ "$config" == "123" ]] || error "ERROR: config-dir not properly used"

beforeEach

# test configure w/ custom config-dir AND custom configuration
mkdir ./temp-config-dir
"$DOPPLER_BINARY" configure set config 123 --config-dir=./temp-config-dir --configuration ./temp-config --scope=/ --silent
config="$("$DOPPLER_BINARY" configure get config --configuration=./temp-config --scope=/ --plain)"
[[ "$config" == "123" ]] || error "ERROR: configuration not properly used when specified with config-dir"

beforeEach

# test unset
"$DOPPLER_BINARY" configure set config 123 --configuration=./temp-config --scope=/ --silent
"$DOPPLER_BINARY" configure unset config --configuration=./temp-config --scope=/ --silent
Expand Down

0 comments on commit fb2df93

Please sign in to comment.