Skip to content

Commit

Permalink
add more arguments to LoadConfigs to help with custom logic
Browse files Browse the repository at this point in the history
  • Loading branch information
beckend committed May 26, 2021
1 parent 4e9d52c commit 764915e
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 12 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
SHELL := /bin/bash

.PHONY: test test-watch coverage-examine
.PHONY: test test-watch coverage-examine upgrade

test:
go test ./... -cover -coverprofile=coverage.coverprofile
Expand All @@ -10,3 +10,6 @@ test-watch:

coverage-examine: test
go tool cover -html=coverage.coverprofile

upgrade:
go-mod-upgrade
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.16

require (
github.com/evanphx/json-patch v0.5.2
github.com/fatih/color v1.11.0
github.com/fatih/color v1.12.0
github.com/go-playground/validator/v10 v10.6.1
github.com/gookit/goutil v0.3.13
github.com/json-iterator/go v1.1.11
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ github.com/evanphx/json-patch v0.5.2 h1:xVCHIVMUu1wtM/VkR9jVZ45N3FhZfYMMYGorLCR8
github.com/evanphx/json-patch v0.5.2/go.mod h1:ZWS5hhDbVDyob71nXKNL0+PWn6ToqBHMikGIFbs31qQ=
github.com/fatih/color v1.11.0 h1:l4iX0RqNnx/pU7rY2DB/I+znuYY0K3x6Ywac6EIr0PA=
github.com/fatih/color v1.11.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/fatih/color v1.12.0 h1:mRhaKNwANqRgUBGKmnI5ZxEk7QXmjQeCcuYFMX2bfcc=
github.com/fatih/color v1.12.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
Expand Down
21 changes: 13 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ type LoadConfigsOptionsTOML struct {
}

type LoadConfigsOptions struct {
TOML *LoadConfigsOptionsTOML
RunEnv string
FilesLoaded []string
ConfigJSONMergedBytes []byte
RunEnv string
TOML *LoadConfigsOptionsTOML
}

type (
Expand All @@ -65,13 +67,14 @@ 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"), "")
var filesToBeMerged []string
var (
_, envKeyUserExists = os.LookupEnv(options.EnvKeyRunEnv)
envRun = environment.GetEnv(conditional.String(envKeyUserExists, options.EnvKeyRunEnv, "RUN_ENV"), "")
filesToBeMerged []string
filesToLoad []string
)

if options.PathConfigs != "" {
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 != "" {
Expand All @@ -93,6 +96,9 @@ func New(options *NewOptions) (*Config, error) {

if options.LoadConfigs != nil {
byteSlicesUser, err := options.LoadConfigs(&LoadConfigsOptions{
FilesLoaded: filesToLoad,
ConfigJSONMergedBytes: bytesJSONMerged,
RunEnv: envRun,
TOML: &LoadConfigsOptionsTOML{
FileToJSON: file.TOMLFileToJSON,
BytesToJSON: file.TOMLBytesToJSON,
Expand All @@ -101,7 +107,6 @@ func New(options *NewOptions) (*Config, error) {
FileReaderToJSON: file.TOMLFileReaderToJSON,
FileReaderCallbackToJSON: file.TOMLFileReaderCallbackToJSON,
},
RunEnv: envRun,
})
if err != nil {
return nil, err
Expand Down
6 changes: 5 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,18 +221,22 @@ var _ = Describe("pkg main", func() {
_, err := config.New(&config.NewOptions{
ConfigUnmarshal: &result,
LoadConfigs: func(options *config.LoadConfigsOptions) ([][]byte, error) {
Expect(len(options.ConfigJSONMergedBytes) > 0).To(Equal(true))
Expect(len(options.FilesLoaded) > 0).To(Equal(true))

b1, err := options.TOML.FileReaderCallbackToJSON(func() (fs.File, error) {
return os.Open(path.Join(pathFixtures, "configs-custom/test1.toml"))
})
common.FailOnError(err)

return [][]byte{b1}, nil
},
PathConfigs: path.Join(pathFixtures, "configs-base"),
})
common.FailOnError(err)

Expect(result["RunEnv"]).To(Equal("development"))
Expect(result["Shell"]).ToNot(Equal("${SHELL}"))
Expect(result["Shell2"]).ToNot(Equal("${SHELL}"))
})
})
})
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/configs-custom/test1.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
NameService = "something"
RunEnv = "development"
Shell = "${SHELL}"
Shell2 = "${SHELL}"

[MessageBrokerTracking]
URI = "amqp://user:bitnami@localhost:5672/%2f"
Expand Down

0 comments on commit 764915e

Please sign in to comment.