Skip to content

Commit bf2f1f0

Browse files
authored
feat: add full environment substitution (#162)
1 parent 0183241 commit bf2f1f0

File tree

5 files changed

+28
-10
lines changed

5 files changed

+28
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Changes since 1.3.0
22
- chore(deps): bump to golang 1.24
3+
- feat: add full environment substitution
34

45
# 1.3.0
56
- feat: add templating command

docs/key-features-and-use-cases.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,21 @@ repositories:
6161

6262
### Environment variables
6363

64-
`helm-compose` is able to inject environment variables inside your values block to deal with secrets that shouldn't be committed to your source control.
64+
`helm-compose` utilizes [a8m/envsubst](https://github.com/a8m/envsubst?tab=readme-ov-file#docs) to substitute environment variables inside your values block. This allows for better dealing with secrets that shouldn't be committed to your source control.
65+
66+
|__Expression__ | __Meaning__ |
67+
| ----------------- | -------------- |
68+
|`${var}` | Value of var (same as `$var`)
69+
|`${var-$DEFAULT}` | If var not set, evaluate expression as $DEFAULT
70+
|`${var:-$DEFAULT}` | If var not set or is empty, evaluate expression as $DEFAULT
71+
|`${var=$DEFAULT}` | If var not set, evaluate expression as $DEFAULT
72+
|`${var:=$DEFAULT}` | If var not set or is empty, evaluate expression as $DEFAULT
73+
|`${var+$OTHER}` | If var set, evaluate expression as $OTHER, otherwise as empty string
74+
|`${var:+$OTHER}` | If var set, evaluate expression as $OTHER, otherwise as empty string
75+
|`$${var}` | Escape expressions. Result will be `${var}`.
76+
77+
<sub>Most of the rows in this table were taken from [here](http://www.tldp.org/LDP/abs/html/refcards.html#AEN22728)</sub>
6578

66-
Syntax: `${MY_ENV_VARIABLE}`.
6779

6880
```bash
6981
export WORDPRESS_ADMIN_PASSWORD="xxx"

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
)
1515

1616
require (
17+
github.com/a8m/envsubst v1.4.3 // indirect
1718
github.com/fxamacker/cbor/v2 v2.8.0 // indirect
1819
github.com/google/gnostic-models v0.6.9 // indirect
1920
github.com/google/go-cmp v0.7.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
22
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
3+
github.com/a8m/envsubst v1.4.3 h1:kDF7paGK8QACWYaQo6KtyYBozY2jhQrTuNNuUxQkhJY=
4+
github.com/a8m/envsubst v1.4.3/go.mod h1:4jjHWQlZoaXPoLQUb7H2qT4iLkZDdmEQiOUogdUmqVU=
35
github.com/aws/aws-sdk-go v1.55.6 h1:cSg4pvZ3m8dgYcgqB97MrcdjUmZ1BeMYKUxMMB89IPk=
46
github.com/aws/aws-sdk-go v1.55.6/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU=
57
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=

internal/util/util.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,9 @@ import (
2020
"math"
2121
"os"
2222
"os/exec"
23-
"regexp"
2423
"strings"
25-
)
2624

27-
var (
28-
re = regexp.MustCompile(`\$\{(.*?)\}`)
25+
"github.com/a8m/envsubst"
2926
)
3027

3128
func IsDebug() bool {
@@ -69,12 +66,17 @@ func ConvertJson(obj interface{}) interface{} {
6966
}
7067
case string:
7168
str := obj.(string)
72-
matches := re.FindStringSubmatch(str)
73-
for _, match := range matches {
74-
str = strings.Replace(str, "${"+match+"}", os.Getenv(match), 1)
69+
70+
substituted, err := envsubst.String(str)
71+
if err != nil {
72+
if IsDebug() {
73+
DebugPrint("Error while substituting environment variables for value '%s': %v", str, err)
74+
}
75+
76+
return str
7577
}
7678

77-
return str
79+
return substituted
7880
}
7981
return obj
8082
}

0 commit comments

Comments
 (0)