Skip to content

Commit

Permalink
Add ReadEnvVarSlice_String to read config values (#1039)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszcl authored Aug 6, 2024
1 parent d1b3fde commit 0583756
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion config/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,33 @@ func MustReadEnvVar_Boolean(name string) *bool {
return ptr.Ptr(value.(bool))
}

// ReadEnvVarSlice_String reads all environment variables matching the specified pattern and returns a slice of strings.
func ReadEnvVarSlice_String(pattern string) []string {
re := regexp.MustCompile(pattern)
var values []string

for _, env := range os.Environ() {
pair := strings.SplitN(env, "=", 2)
if len(pair) != 2 {
continue
}
key, value := pair[0], pair[1]
if re.MatchString(key) && value != "" {
values = append(values, value)
}
}
return values
}

// ReadEnvVarGroupedMap combines environment variables into a map where keys map to slices of strings.
// It accepts `singleEnvPattern` for single variables and `groupEnvPattern` for grouped variables.
// Returns a map combining values from both patterns, with single values wrapped in slices.
func ReadEnvVarGroupedMap(singleEnvPattern, groupEnvPattern string) map[string][]string {
return mergeMaps(readEnvVarSingleMap(singleEnvPattern), readEnvVarGroupedMap(groupEnvPattern))
var singleMap map[string]string
if singleEnvPattern != "" {
singleMap = readEnvVarSingleMap(singleEnvPattern)
}
return mergeMaps(singleMap, readEnvVarGroupedMap(groupEnvPattern))
}

// readEnvVarValue reads an environment variable and returns the value parsed according to the specified type.
Expand Down

0 comments on commit 0583756

Please sign in to comment.