Settings: Expand variables in configuration (#25075)
This commit is contained in:
+4
-26
@@ -12,7 +12,6 @@ import (
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -440,30 +439,6 @@ func makeAbsolute(path string, root string) string {
|
||||
return filepath.Join(root, path)
|
||||
}
|
||||
|
||||
func EvalEnvVarExpression(value string) string {
|
||||
regex := regexp.MustCompile(`\${(\w+)}`)
|
||||
return regex.ReplaceAllStringFunc(value, func(envVar string) string {
|
||||
envVar = strings.TrimPrefix(envVar, "${")
|
||||
envVar = strings.TrimSuffix(envVar, "}")
|
||||
envValue := os.Getenv(envVar)
|
||||
|
||||
// if env variable is hostname and it is empty use os.Hostname as default
|
||||
if envVar == "HOSTNAME" && envValue == "" {
|
||||
envValue, _ = os.Hostname()
|
||||
}
|
||||
|
||||
return envValue
|
||||
})
|
||||
}
|
||||
|
||||
func evalConfigValues(file *ini.File) {
|
||||
for _, section := range file.Sections() {
|
||||
for _, key := range section.Keys() {
|
||||
key.SetValue(EvalEnvVarExpression(key.Value()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func loadSpecifiedConfigFile(configFile string, masterFile *ini.File) error {
|
||||
if configFile == "" {
|
||||
configFile = filepath.Join(HomePath, CustomInitPath)
|
||||
@@ -550,7 +525,10 @@ func (cfg *Cfg) loadConfiguration(args *CommandLineArgs) (*ini.File, error) {
|
||||
applyCommandLineProperties(commandLineProps, parsedFile)
|
||||
|
||||
// evaluate config values containing environment variables
|
||||
evalConfigValues(parsedFile)
|
||||
err = expandConfig(parsedFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// update data path and logging config
|
||||
dataPath, err := valueAsString(parsedFile.Section("paths"), "data", "")
|
||||
|
||||
Reference in New Issue
Block a user