From 93bafb47e8b49d170596c314976bcd88fb970394 Mon Sep 17 00:00:00 2001 From: Philippe Caseiro Date: Mon, 10 Jul 2023 16:59:00 +0200 Subject: [PATCH] fix(config): supporting multiple env variables in a value. ref #9 --- go.mod | 1 + go.sum | 2 ++ internal/config/environment.go | 16 +++++++++++++--- internal/config/testdata/config.yml | 4 ++-- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 1446537..baed41a 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( forge.cadoles.com/Cadoles/go-proxy v0.0.0-20230701194111-c6b3d482cca6 github.com/Masterminds/sprig/v3 v3.2.3 github.com/btcsuite/btcd/btcutil v1.1.3 + github.com/drone/envsubst v1.0.3 github.com/getsentry/sentry-go v0.22.0 github.com/go-chi/chi/v5 v5.0.8 github.com/jedib0t/go-pretty/v6 v6.4.6 diff --git a/go.sum b/go.sum index 2d32448..942e850 100644 --- a/go.sum +++ b/go.sum @@ -163,6 +163,8 @@ github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKoh github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw= github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= +github.com/drone/envsubst v1.0.3 h1:PCIBwNDYjs50AsLZPYdfhSATKaRg/FJmDc2D6+C2x8g= +github.com/drone/envsubst v1.0.3/go.mod h1:N2jZmlMufstn1KEqvbHjw40h1KyTmnVzHcSc9bFiJ2g= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= diff --git a/internal/config/environment.go b/internal/config/environment.go index 201f9a8..be9bc56 100644 --- a/internal/config/environment.go +++ b/internal/config/environment.go @@ -6,11 +6,13 @@ import ( "strconv" "time" + "github.com/drone/envsubst" "github.com/pkg/errors" "gopkg.in/yaml.v3" ) -var reVar = regexp.MustCompile(`^\${(\w+)}$`) +// var reVar = regexp.MustCompile(`^\${(\w+)}$`) +var reVar = regexp.MustCompile(`\${(.*?)}`) type InterpolatedString string @@ -130,14 +132,22 @@ type InterpolatedStringSlice []string func (iss *InterpolatedStringSlice) UnmarshalYAML(value *yaml.Node) error { var data []string + var evErr error if err := value.Decode(&data); err != nil { return errors.Wrapf(err, "could not decode value '%v' (line '%d') into map", value.Value, value.Line) } for index, value := range data { - if match := reVar.FindStringSubmatch(value); len(match) > 0 { - value = os.Getenv(match[1]) + //match := reVar.FindStringSubmatch(value) + re := regexp.MustCompile(`\${(.*?)}`) + + res := re.FindAllStringSubmatch(value, 10) + if len(res) > 0 { + value, evErr = envsubst.EvalEnv(value) + if evErr != nil { + return evErr + } } data[index] = value diff --git a/internal/config/testdata/config.yml b/internal/config/testdata/config.yml index e06a61b..030a087 100644 --- a/internal/config/testdata/config.yml +++ b/internal/config/testdata/config.yml @@ -2,5 +2,5 @@ logger: level: 0 format: human http: - host: "0.0.0.0" - port: 3000 \ No newline at end of file + host: "${LISTEN_ADDR}" + port: 3000 -- 2.17.1