Skip to content

Commit

Permalink
add options.RunEnv to LoadFiles callback
Browse files Browse the repository at this point in the history
  • Loading branch information
beckend committed May 24, 2021
1 parent 1cbee08 commit 7cb7ffd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,18 @@ type TestValidateStructOne struct {
Password string `validate:"required"`
}

err := os.Setenv("RUN_ENV", "staging")
if err !=nil {
panic(err)
}

var result TestValidateStructOne
_, err := config.New(&config.NewOptions{
ConfigUnmarshal: &result,
EnvKeyRunEnv: "RUN_ENV",
LoadConfigs: func(options *config.LoadConfigsOptions) ([][]byte, error) {
Expect(options.RunEnv).To(Equal("staging"))

b1, err := options.TOML.BytesToJSON([]byte("RunEnv = 'overriden'"))
if err != nil {
return nil, err
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ type LoadConfigsOptionsTOML struct {
}

type LoadConfigsOptions struct {
TOML *LoadConfigsOptionsTOML
TOML *LoadConfigsOptionsTOML
RunEnv string
}

type (
Expand Down Expand Up @@ -91,6 +92,7 @@ func New(options *NewOptions) (*Config, error) {
StringToJSON: file.TOMLStringToJSON,
ReaderToJSON: file.TOMLReaderToJSON,
},
RunEnv: envRun,
})
if err != nil {
return nil, err
Expand Down
21 changes: 17 additions & 4 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,21 @@ var _ = Describe("pkg validation", func() {

When("option LoadConfigs", func() {
It("allows user overrides and keeps what is not overriden", func() {
keyEnvTarget := "RUN_ENV_CUSTOM"
keyEnvTargetValue := "staging"
err := os.Setenv(keyEnvTarget, keyEnvTargetValue)
defer os.Unsetenv(keyEnvTarget)
if err != nil {
panic(err)
}

var result TestValidateStructOne
_, err := config.New(&config.NewOptions{
_, err = config.New(&config.NewOptions{
ConfigUnmarshal: &result,
EnvKeyRunEnv: "RUN_ENV",
EnvKeyRunEnv: keyEnvTarget,
LoadConfigs: func(options *config.LoadConfigsOptions) ([][]byte, error) {
Expect(options.RunEnv).To(Equal(keyEnvTargetValue))

b1, err := options.TOML.BytesToJSON([]byte("RunEnv = 'overriden'"))
if err != nil {
return nil, err
Expand Down Expand Up @@ -152,10 +162,13 @@ var _ = Describe("pkg validation", func() {
var result TestValidateStructTwo
keyEnvTarget := "RUN_ENV_CUSTOM"
keyEnvTargetValue := "staging"
os.Setenv(keyEnvTarget, keyEnvTargetValue)
err := os.Setenv(keyEnvTarget, keyEnvTargetValue)
if err != nil {
panic(err)
}
defer os.Unsetenv(keyEnvTarget)

_, err := config.New(&config.NewOptions{
_, err = config.New(&config.NewOptions{
ConfigUnmarshal: &result,
EnvKeyRunEnv: keyEnvTarget,
PathConfigs: path.Join(pathFixtures, "configs-env"),
Expand Down

0 comments on commit 7cb7ffd

Please sign in to comment.