feat: Add capability to set default for environment variable in config (#1248)

Support `${ENV_VALUE:default_value}`

🛠️ Fixes #1001
This commit is contained in:
Dr. Strangelove
2025-08-26 16:28:55 -04:00
committed by GitHub
parent 7651357d42
commit 5bcd52e7dc
2 changed files with 10 additions and 1 deletions

View File

@@ -265,8 +265,9 @@ type ToolsFile struct {
}
// parseEnv replaces environment variables ${ENV_NAME} with their values.
// also support ${ENV_NAME:default_value}.
func parseEnv(input string) (string, error) {
re := regexp.MustCompile(`\$\{(\w+)\}`)
re := regexp.MustCompile(`\$\{(\w+)(:(\w+))?\}`)
var err error
output := re.ReplaceAllStringFunc(input, func(match string) string {
@@ -277,6 +278,9 @@ func parseEnv(input string) (string, error) {
if value, found := os.LookupEnv(variableName); found {
return value
}
if len(parts) == 4 {
return parts[3]
}
err = fmt.Errorf("environment variable not found: %q", variableName)
return ""
})

View File

@@ -22,6 +22,11 @@ etc., you could use environment variables instead with the format `${ENV_NAME}`.
user: ${USER_NAME}
password: ${PASSWORD}
```
A default value can be specified like `${ENV_NAME:default}`.
```yaml
port: ${DB_PORT:3306}
```
### Sources