Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
}

var nameRE = regexp.MustCompile(`^[a-zA-Z0-9_.:-]+$`)
var envVarRE = regexp.MustCompile(`\${([^}]+)}`)

func nameValid(name string) error {
if !nameRE.MatchString(name) {
Expand All @@ -84,6 +85,36 @@
return nil
}

// Recursively replace environment variables in a map or slice
func replaceEnvVars(input interface{}) interface{} {
switch v := input.(type) {
case string:
return replaceEnvInString(v)
case map[string]interface{}:
for key, value := range v {
v[key] = replaceEnvVars(value)
}
return v
case []interface{}:
for i, value := range v {
v[i] = replaceEnvVars(value)
}
return v
default:
return input
}
}

// Replace environment variable placeholders in a string
func replaceEnvInString(s string) string {
return envVarRE.ReplaceAllStringFunc(s, func(match string) string {
envVar := match[2 : len(match)-1]
if envValue := os.Getenv(envVar); envValue != "" {
return envValue
}
return match // Keep original if no environment variable is set
})
}
func loadConfigFile(conf *globalconfig.All, checkDB bool) error {
err := viper.ReadInConfig()

Expand All @@ -94,6 +125,13 @@
log.INFO.Println("using config file:", cfgFile)

if err == nil {
allSettings := viper.AllSettings()

Check failure on line 129 in cmd/setup.go

View workflow job for this annotation

GitHub Actions / Lint

File is not properly formatted (gci)
// Replace environment variables in the entire configuration
replacedSettings := replaceEnvVars(allSettings).(map[string]interface{})

viper.MergeConfigMap(replacedSettings)

if err = viper.UnmarshalExact(conf); err != nil {
err = fmt.Errorf("failed parsing config file: %w", err)
}
Expand Down
Loading