Skip to content

Commit 5bcd52e

Browse files
feat: Add capability to set default for environment variable in config (#1248)
Support `${ENV_VALUE:default_value}` 🛠️ Fixes #1001
1 parent 7651357 commit 5bcd52e

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

‎cmd/root.go‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,9 @@ type ToolsFile struct {
265265
}
266266

267267
// parseEnv replaces environment variables ${ENV_NAME} with their values.
268+
// also support ${ENV_NAME:default_value}.
268269
func parseEnv(input string) (string, error) {
269-
re := regexp.MustCompile(`\$\{(\w+)\}`)
270+
re := regexp.MustCompile(`\$\{(\w+)(:(\w+))?\}`)
270271

271272
var err error
272273
output := re.ReplaceAllStringFunc(input, func(match string) string {
@@ -277,6 +278,9 @@ func parseEnv(input string) (string, error) {
277278
if value, found := os.LookupEnv(variableName); found {
278279
return value
279280
}
281+
if len(parts) == 4 {
282+
return parts[3]
283+
}
280284
err = fmt.Errorf("environment variable not found: %q", variableName)
281285
return ""
282286
})

‎docs/en/getting-started/configure.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ etc., you could use environment variables instead with the format `${ENV_NAME}`.
2222
user: ${USER_NAME}
2323
password: ${PASSWORD}
2424
```
25+
A default value can be specified like `${ENV_NAME:default}`.
26+
27+
```yaml
28+
port: ${DB_PORT:3306}
29+
```
2530

2631
### Sources
2732

0 commit comments

Comments
 (0)