Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add environment variable overrides for file loaded properties #30

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 37 additions & 0 deletions load.go
Expand Up @@ -101,6 +101,34 @@ func MustLoadFiles(filenames []string, enc Encoding, ignoreMissing bool) *Proper
return must(LoadFiles(filenames, enc, ignoreMissing))
}

// MustLoadFilesWithEnvOverrides reads multiple files in the given order
// into a Properties struct and panics on error. If 'ignoreMissing' is
// true then non-existent files will not be reported as error. After loading
// MustLoadFilesWithEnvOverrides checks for environment variables in the pattern
// {envPrefix}SOME_NAME=val and converts the key to a property name by substituting
// the _ for dots and removing the envPrefix. If this converted key matches any of
// the loaded properties from the files, it will override the value of the file
// loaded property with the value of the environment variable.
func MustLoadFilesWithEnvOverrides(filenames []string, enc Encoding, ignoreMissing bool, envPrefix string) *Properties {
propsFromFile := MustLoadFiles(filenames, enc, ignoreMissing)

for _, e := range os.Environ() {
pair := strings.Split(e, "=")
key := pair[0]
if envPrefix != "" && strings.HasPrefix(key, envPrefix) {
envToPropertyName := strings.ToLower(strings.Replace(key[3:], "_", ".", -1))
environmentVariableValue := os.Getenv(key)

envToPropertyName = findOriginalKeyRegardlessOfCase(envToPropertyName, propsFromFile)

LogPrintf("Overriding property %s with environment variable %s", envToPropertyName, key)
propsFromFile.MustSet(envToPropertyName, environmentVariableValue)
}
}

return propsFromFile
}

// MustLoadURL reads the content of a URL into a Properties struct and
// panics on error.
func MustLoadURL(url string) *Properties {
Expand Down Expand Up @@ -213,6 +241,15 @@ func must(p *Properties, err error) *Properties {
return p
}

func findOriginalKeyRegardlessOfCase(key string, props *Properties) string {
for _, origKey := range props.Keys() {
if strings.EqualFold(origKey, key) {
return origKey
}
}
return key
}

// expandName expands ${ENV_VAR} expressions in a name.
// If the environment variable does not exist then it will be replaced
// with an empty string. Malformed expressions like "${ENV_VAR" will
Expand Down
19 changes: 19 additions & 0 deletions load_test.go
Expand Up @@ -70,6 +70,25 @@ func TestLoadFiles(t *testing.T) {
assertKeyValues(t, "", p, "key", "value", "key2", "value2")
}

func TestLoadFilesWithEnvOverride(t *testing.T) {
tf := make(tempFiles, 0)
defer tf.removeAll()

filename := tf.makeFile("thrift.client=proxy")
filename2 := tf.makeFile("thrift.MyCamelCase=true")

os.Setenv("MP_THRIFT_CLIENT", "chicken")
os.Setenv("MP_THRIFT_MYCAMELCASE", "false")
defer os.Unsetenv("MP_THRIFT_CLIENT")
defer os.Unsetenv("MP_THRIFT_MYCAMELCASE")

p := MustLoadFilesWithEnvOverrides([]string{filename, filename2}, ISO_8859_1, false, "MP_")
assertKeyValues(t, "", p, "thrift.client", "chicken", "thrift.MyCamelCase", "false")

p = MustLoadFilesWithEnvOverrides([]string{filename, filename2}, ISO_8859_1, false, "")
assertKeyValues(t, "", p, "thrift.client", "proxy", "thrift.MyCamelCase", "true")
}

func TestLoadExpandedFile(t *testing.T) {
tf := make(tempFiles, 0)
defer tf.removeAll()
Expand Down