fix(config): Replace environment variables if existing but empty (#13570)
This commit is contained in:
parent
00b0ae68e0
commit
1dd45b1d1c
|
|
@ -858,8 +858,8 @@ func removeComments(contents []byte) ([]byte, error) {
|
|||
func substituteEnvironment(contents []byte, oldReplacementBehavior bool) ([]byte, error) {
|
||||
options := []template.Option{
|
||||
template.WithReplacementFunction(func(s string, m template.Mapping, cfg *template.Config) (string, error) {
|
||||
result, err := template.DefaultReplacementFunc(s, m, cfg)
|
||||
if err == nil && result == "" {
|
||||
result, applied, err := template.DefaultReplacementAppliedFunc(s, m, cfg)
|
||||
if err == nil && !applied {
|
||||
// Keep undeclared environment-variable patterns to reproduce
|
||||
// pre-v1.27 behavior
|
||||
return s, nil
|
||||
|
|
|
|||
|
|
@ -55,6 +55,14 @@ func TestEnvironmentSubstitution(t *testing.T) {
|
|||
contents: "Env var ${MY_VAR} is set, with $MY_VAR syntax and default on this ${MY_VAR1:-Substituted}, no default on this ${MY_VAR2:-NoDefault}",
|
||||
expected: "Env var VALUE is set, with VALUE syntax and default on this Substituted, no default on this VALUE2",
|
||||
},
|
||||
{
|
||||
name: "empty but set",
|
||||
setEnv: func(t *testing.T) {
|
||||
t.Setenv("EMPTY", "")
|
||||
},
|
||||
contents: "Contains ${EMPTY} nothing",
|
||||
expected: "Contains nothing",
|
||||
},
|
||||
{
|
||||
name: "Default has special chars",
|
||||
contents: `Not recommended but supported ${MY_VAR:-Default with special chars Supported#$\"}`,
|
||||
|
|
@ -181,12 +189,18 @@ func TestEnvironmentSubstitutionOldBehavior(t *testing.T) {
|
|||
contents: `${1}`,
|
||||
expected: `${1}`,
|
||||
},
|
||||
{
|
||||
name: "empty but set",
|
||||
contents: "Contains ${EMPTY} nothing",
|
||||
expected: "Contains nothing",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Setenv("VAR", "foobar")
|
||||
t.Setenv("FALLBACK", "default")
|
||||
t.Setenv("EMPTY", "")
|
||||
actual, err := substituteEnvironment([]byte(tt.contents), true)
|
||||
require.NoError(t, err)
|
||||
require.EqualValues(t, tt.expected, string(actual))
|
||||
|
|
|
|||
2
go.mod
2
go.mod
|
|
@ -55,7 +55,7 @@ require (
|
|||
github.com/caio/go-tdigest v3.1.0+incompatible
|
||||
github.com/cisco-ie/nx-telemetry-proto v0.0.0-20230117155933-f64c045c77df
|
||||
github.com/clarify/clarify-go v0.2.4
|
||||
github.com/compose-spec/compose-go v1.15.0
|
||||
github.com/compose-spec/compose-go v1.16.0
|
||||
github.com/coocood/freecache v1.2.3
|
||||
github.com/coreos/go-semver v0.3.1
|
||||
github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f
|
||||
|
|
|
|||
4
go.sum
4
go.sum
|
|
@ -388,8 +388,8 @@ github.com/cncf/xds/go v0.0.0-20211001041855-01bcc9b48dfe/go.mod h1:eXthEFrGJvWH
|
|||
github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
|
||||
github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I=
|
||||
github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ=
|
||||
github.com/compose-spec/compose-go v1.15.0 h1:rv3TTgbS3U4Y8sRTngrcxDmpbz+fq26wTqHculSCi6s=
|
||||
github.com/compose-spec/compose-go v1.15.0/go.mod h1:3yngGBGfls6FHGQsg4B1z6gz8ej9SOvmAJtxCwgbcnc=
|
||||
github.com/compose-spec/compose-go v1.16.0 h1:HYk4uYWXgArHh6NG+WE4yGYayOXw+hjqJ+eJxpjWWjk=
|
||||
github.com/compose-spec/compose-go v1.16.0/go.mod h1:3yngGBGfls6FHGQsg4B1z6gz8ej9SOvmAJtxCwgbcnc=
|
||||
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=
|
||||
github.com/containerd/containerd v1.6.18 h1:qZbsLvmyu+Vlty0/Ex5xc0z2YtKpIsb5n45mAMI+2Ns=
|
||||
github.com/containerd/containerd v1.6.18/go.mod h1:1RdCUu95+gc2v9t3IL+zIlpClSmew7/0YS8O5eQZrOw=
|
||||
|
|
|
|||
Loading…
Reference in New Issue