Skip to content

Commit

Permalink
fix: don't unescape dollars in environment variable placeholders (#241)
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Meier <[email protected]>
  • Loading branch information
astromechza authored Jan 15, 2025
1 parent 935a22a commit a4fe4a4
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 3 deletions.
2 changes: 2 additions & 0 deletions examples/02-environment/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ containers:
GREETING: Hello
NAME: ${resources.env.NAME}
WORKLOAD_NAME: ${metadata.name}
ESCAPED: $$_$${fizzbuzz}

resources:
env:
Expand Down Expand Up @@ -48,6 +49,7 @@ services:
entrypoint:
- /bin/sh
environment:
ESCAPED: $$_$${fizzbuzz}
GREETING: Hello
NAME: ${NAME}
WORKLOAD_NAME: hello-world
Expand Down
1 change: 1 addition & 0 deletions examples/02-environment/score.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ containers:
GREETING: Hello
NAME: ${resources.env.NAME}
WORKLOAD_NAME: ${metadata.name}
ESCAPED: $$_$${fizzbuzz}

resources:
env:
Expand Down
1 change: 1 addition & 0 deletions internal/command/generate_examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ services:
entrypoint:
- /bin/sh
environment:
ESCAPED: $$_$${fizzbuzz}
GREETING: Hello
NAME: ${NAME}
WORKLOAD_NAME: hello-world
Expand Down
9 changes: 8 additions & 1 deletion internal/compose/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,20 @@ func ConvertSpec(state *project.State, spec *score.Workload) (*compose.Project,
}
sort.Strings(containerNames)

variablesSubstitutor := framework.Substituter{
Replacer: deferredSubstitutionFunction,
UnEscaper: func(s string) (string, error) {
return s, nil
},
}

var firstService string
for _, containerName := range containerNames {
cSpec := spec.Containers[containerName]

var env = make(compose.MappingWithEquals, len(cSpec.Variables))
for key, val := range cSpec.Variables {
resolved, err := framework.SubstituteString(val, deferredSubstitutionFunction)
resolved, err := variablesSubstitutor.SubstituteString(val)
if err != nil {
return nil, fmt.Errorf("containers.%s.variables.%s: %w", containerName, key, err)
}
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 @@ -156,7 +156,7 @@ func TestScoreConvert(t *testing.T) {
Image: "busybox",
Environment: compose.MappingWithEquals{
"DEBUG": stringPtr("${DEBUG}"),
"LOGS_LEVEL": stringPtr("${LOGS_LEVEL}"),
"LOGS_LEVEL": stringPtr("$${LOGS_LEVEL}"),
"DOMAIN_NAME": stringPtr("${SOME_DNS_DOMAIN_NAME?required}"),
"CONNECTION_STRING": stringPtr("postgresql://${APP_DB_HOST?required}:${APP_DB_PORT?required}/${APP_DB_NAME?required}"),
},
Expand Down Expand Up @@ -229,7 +229,7 @@ func TestScoreConvert(t *testing.T) {
Image: "busybox",
Environment: compose.MappingWithEquals{
"DEBUG": stringPtr("${DEBUG}"),
"LOGS_LEVEL": stringPtr("${LOGS_LEVEL}"),
"LOGS_LEVEL": stringPtr("$${LOGS_LEVEL}"),
"DOMAIN_NAME": stringPtr("${SOME_DNS_DOMAIN_NAME?required}"),
"CONNECTION_STRING": stringPtr("mysql://${APP_DB_HOST?required}:${APP_DB_PORT?required}/${APP_DB_NAME?required}"),
},
Expand Down

0 comments on commit a4fe4a4

Please sign in to comment.