Skip to content

Commit

Permalink
no default run env
Browse files Browse the repository at this point in the history
  • Loading branch information
beckend committed May 24, 2021
1 parent 7cb7ffd commit 11c8fb3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
22 changes: 11 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ import (
mapstructure "github.com/mitchellh/mapstructure"
)

var (
defaultRUNENV = "development"
json = jsoniter.ConfigCompatibleWithStandardLibrary
)
var json = jsoniter.ConfigCompatibleWithStandardLibrary

type Config struct {
ErrorsValidation *validator.ValidationErrors
Expand Down Expand Up @@ -65,15 +62,18 @@ type NewOptions struct {
// New read configurations with priority, the later overrides the previous
func New(options *NewOptions) (*Config, error) {
_, envKeyUserExists := os.LookupEnv(options.EnvKeyRunEnv)
envRun := environment.GetEnv(conditional.String(envKeyUserExists, options.EnvKeyRunEnv, "RUN_ENV"), defaultRUNENV)
envRun := environment.GetEnv(conditional.String(envKeyUserExists, options.EnvKeyRunEnv, "RUN_ENV"), "")
var filesToBeMerged []string
var filesToLoad []string

// the order to load is base, env specific, then local, where the next overrides the previous values
filesToLoad = append(filesToLoad, path.Join(options.PathConfigs, "base.toml"))
if envRun != "" {
filesToLoad = append(filesToLoad, path.Join(options.PathConfigs, envRun+".toml"))
}
filesToLoad = append(filesToLoad, path.Join(options.PathConfigs, "local.toml"))

for _, pathFile := range [...](string){
// the order to load is base, env specific, then local, where the next overrides the previous values
path.Join(options.PathConfigs, "base.toml"),
path.Join(options.PathConfigs, envRun+".toml"),
path.Join(options.PathConfigs, "local.toml"),
} {
for _, pathFile := range filesToLoad {
if _, err := os.Stat(pathFile); err == nil {
filesToBeMerged = append(filesToBeMerged, pathFile)
}
Expand Down
23 changes: 23 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config_test

import (
"errors"
os "os"
path "path"
filepath "path/filepath"
Expand Down Expand Up @@ -55,6 +56,28 @@ var _ = Describe("pkg validation", func() {
})
})

When("RunEnv env is missing", func() {
It("string will be empty", func() {
var result TestValidateStructTwo
_, err := config.New(&config.NewOptions{
ConfigUnmarshal: &result,
PathConfigs: path.Join(pathFixtures, "does/not-exist"),
OnConfigBeforeValidation: func(options *config.OnConfigBeforeValidationOptions) error {
cfg := options.ConfigUnmarshal.(*TestValidateStructTwo)
cfg.AccessKey = "somethings"

if cfg.RunEnV != "" {
return errors.New("this is not supposed to happen")
}

return nil
},
})

Expect(err.Error()).To(ContainSubstring("struct validation failed"))
})
})

When("option LoadConfigs", func() {
It("allows user overrides and keeps what is not overriden", func() {
keyEnvTarget := "RUN_ENV_CUSTOM"
Expand Down

0 comments on commit 11c8fb3

Please sign in to comment.