Skip to content

Commit

Permalink
fix: require env var substitutions to be set (#59)
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Meier <[email protected]>
  • Loading branch information
astromechza authored Mar 8, 2024
1 parent 87f7c72 commit 61f8d18
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
3 changes: 2 additions & 1 deletion internal/command/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ func TestRunExample03(t *testing.T) {
entrypoint:
- /bin/sh
environment:
CONNECTION_STRING: postgresql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}
CONNECTION_STRING: postgresql://${DB_USER?required}:${DB_PASSWORD?required}@${DB_HOST?required}:${DB_PORT?required}/${DB_NAME?required}
FRIEND: ${NAME}
image: busybox
`
Expand Down Expand Up @@ -426,6 +426,7 @@ func TestRunExample03(t *testing.T) {
cmd.Dir = td
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = append(os.Environ(), "DB_HOST=host", "DB_PORT=80", "DB_NAME=default", "DB_USER=myuser", "DB_PASSWORD=password")
assert.NoError(t, cmd.Run())
})
}
Expand Down
4 changes: 2 additions & 2 deletions internal/compose/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ func TestScoreConvert(t *testing.T) {
Environment: compose.MappingWithEquals{
"DEBUG": stringPtr("${DEBUG}"),
"LOGS_LEVEL": stringPtr("${LOGS_LEVEL}"),
"DOMAIN_NAME": stringPtr("${SOME_DNS_DOMAIN_NAME}"),
"CONNECTION_STRING": stringPtr("postgresql://${APP_DB_HOST}:${APP_DB_PORT}/${APP_DB_NAME}"),
"DOMAIN_NAME": stringPtr("${SOME_DNS_DOMAIN_NAME?required}"),
"CONNECTION_STRING": stringPtr("postgresql://${APP_DB_HOST?required}:${APP_DB_PORT?required}/${APP_DB_NAME?required}"),
},
Volumes: []compose.ServiceVolumeConfig{
{
Expand Down
11 changes: 9 additions & 2 deletions internal/compose/envvar_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (e *EnvVarTracker) Accessed() map[string]string {
// the env var tracker is a resource itself (an environment resource)
var _ ResourceWithOutputs = (*EnvVarTracker)(nil)

func (e *EnvVarTracker) LookupOutput(keys ...string) (interface{}, error) {
func (e *EnvVarTracker) lookupOutput(required bool, keys ...string) (interface{}, error) {
if len(keys) == 0 {
panic("requires at least 1 key")
}
Expand All @@ -46,9 +46,16 @@ func (e *EnvVarTracker) LookupOutput(keys ...string) (interface{}, error) {
} else {
e.accessed[envVarKey] = ""
}
if required {
envVarKey += "?required"
}
return "${" + envVarKey + "}", nil
}

func (e *EnvVarTracker) LookupOutput(keys ...string) (interface{}, error) {
return e.lookupOutput(false, keys...)
}

func (e *EnvVarTracker) GenerateResource(resName string) ResourceWithOutputs {
return &envVarResourceTracker{
inner: e,
Expand All @@ -69,5 +76,5 @@ func (e *envVarResourceTracker) LookupOutput(keys ...string) (interface{}, error
for i, k := range keys {
next[1+i] = k
}
return e.inner.LookupOutput(next...)
return e.inner.lookupOutput(true, next...)
}
10 changes: 5 additions & 5 deletions internal/compose/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ func TestMapVar(t *testing.T) {
{Input: "resources.env.DEBUG", Expected: "${DEBUG}"},
{Input: "resources.missing", ExpectedError: "invalid ref 'resources.missing': no known resource 'missing'"},
{Input: "resources.db", Expected: "db"},
{Input: "resources.db.host", Expected: "${DB_HOST}"},
{Input: "resources.db.port", Expected: "${DB_PORT}"},
{Input: "resources.db.name", Expected: "${DB_NAME}"},
{Input: "resources.db.name.user", Expected: "${DB_NAME_USER}"},
{Input: "resources.db.host", Expected: "${DB_HOST?required}"},
{Input: "resources.db.port", Expected: "${DB_PORT?required}"},
{Input: "resources.db.name", Expected: "${DB_NAME?required}"},
{Input: "resources.db.name.user", Expected: "${DB_NAME_USER?required}"},
{Input: "resources.static", Expected: "static"},
{Input: "resources.static.x", Expected: "a"},
{Input: "resources.static.y", ExpectedError: "invalid ref 'resources.static.y': key 'y' not found"},
Expand Down Expand Up @@ -108,7 +108,7 @@ func TestSubstitute(t *testing.T) {
{Input: "my name is ${metadata.thing\\.two}", ExpectedError: "invalid ref 'metadata.thing\\.two': key 'thing.two' not found"},
{
Input: "postgresql://${resources.db.user}:${resources.db.password}@${resources.db.host}:${resources.db.port}/${resources.db.name}",
Expected: "postgresql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}",
Expected: "postgresql://${DB_USER?required}:${DB_PASSWORD?required}@${DB_HOST?required}:${DB_PORT?required}/${DB_NAME?required}",
},
} {
t.Run(tc.Input, func(t *testing.T) {
Expand Down

0 comments on commit 61f8d18

Please sign in to comment.