Skip to content

Commit

Permalink
Merge pull request #115 from articulate/fix/env-expansion
Browse files Browse the repository at this point in the history
fix: env expansion
  • Loading branch information
mloberg authored Jan 31, 2023
2 parents 63b8c6e + 6c9b8e4 commit dece740
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
30 changes: 22 additions & 8 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"fmt"
"os"

"github.com/samber/lo"
)

// EnvMap represents a map for environment variables
Expand All @@ -28,13 +30,25 @@ func (e *EnvMap) Add(key, value string) {
}

// Environ returns the map in the format of "key=value", skipping any already set,
// non-empty environment variables
// non-empty environment variables, and expanding variables
func (e *EnvMap) Environ() []string {
env := []string{}
for k, v := range e.env {
if x := os.Getenv(k); x == "" {
env = append(env, fmt.Sprintf("%s=%s", k, v))
}
}
return env
env := lo.OmitBy(e.env, func(k string, _ string) bool {
return os.Getenv(k) != ""
})

env = lo.MapValues(env, func(v string, _ string) string {
return os.Expand(v, func(s string) string {
if l := os.Getenv(s); l != "" {
return l
}
if v, ok := e.env[s]; ok {
return v
}
return ""
})
})

return lo.MapToSlice(env, func(k string, v string) string {
return fmt.Sprintf("%s=%s", k, v)
})
}
4 changes: 4 additions & 0 deletions env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ func TestEnvMap(t *testing.T) {
"DOCKER_CONSUL_BOOTSTRAP_TEST_BAZ": "bar",
})
e.Add("DOCKER_CONSUL_BOOTSTRAP_TEST_BAR", "testing")
e.Add("DOCKER_CONSUL_BOOTSTRAP_EXPAND_ONE", "foo-${DOCKER_CONSUL_BOOTSTRAP_TEST_FOO}-bar")
e.Add("DOCKER_CONSUL_BOOTSTRAP_EXPAND_TWO", "foo-${DOCKER_CONSUL_BOOTSTRAP_TEST_BAZ}-baz")

assert.ElementsMatch(t, []string{
"DOCKER_CONSUL_BOOTSTRAP_TEST_TEST=testing",
"DOCKER_CONSUL_BOOTSTRAP_TEST_BAZ=bar",
"DOCKER_CONSUL_BOOTSTRAP_EXPAND_ONE=foo-ignore-bar",
"DOCKER_CONSUL_BOOTSTRAP_EXPAND_TWO=foo-bar-baz",
}, e.Environ())
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/hashicorp/vault/api/auth/aws v0.3.0
github.com/hashicorp/vault/api/auth/kubernetes v0.3.0
github.com/rs/zerolog v1.29.0
github.com/samber/lo v1.37.0
github.com/stretchr/testify v1.8.1
)

Expand Down Expand Up @@ -69,6 +70,7 @@ require (
github.com/stretchr/objx v0.5.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f // indirect
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 // indirect
golang.org/x/text v0.3.8 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb
github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/ryanuber/go-glob v1.0.0 h1:iQh3xXAumdQ+4Ufa5b25cRpC5TYKlno6hsv6Cb3pkBk=
github.com/ryanuber/go-glob v1.0.0/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc=
github.com/samber/lo v1.37.0 h1:XjVcB8g6tgUp8rsPsJ2CvhClfImrpL04YpQHXeHPhRw=
github.com/samber/lo v1.37.0/go.mod h1:9vaz2O4o8oOnK23pd2TrXufcbdbJIa3b6cstBWKpopA=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529 h1:nn5Wsu0esKSJiIVhscUtVbo7ada43DJhG55ua/hjS5I=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
Expand Down Expand Up @@ -330,6 +332,8 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 h1:/UOmuWzQfxxo9UtlXMwuQU8CMgg1eZXqTRwkSQJWKOI=
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 h1:3MTrJm4PyNL9NBqvYDSj3DHl46qQakyfqfWo4jgfaEM=
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"errors"
"os"
"os/exec"
"runtime"
"strconv"
"strings"

"github.com/rs/zerolog"
Expand Down Expand Up @@ -59,6 +61,8 @@ func main() {
logger.Warn().Msg("Not loading values from Vault. VAULT_ADDR is not set")
}

env.Add("PROCESSOR_COUNT", strconv.Itoa(runtime.NumCPU()))

exit := run(os.Args[1], os.Args[2:], env.Environ(), logger)
os.Exit(exit)
}
Expand Down

0 comments on commit dece740

Please sign in to comment.