Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keep empty environment variables as those must be UNSET in container #654

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions loader/normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ func Normalize(dict map[string]any, env types.Mapping) (map[string]any, error) {
}

if a, ok := build["args"]; ok {
build["args"], _ = resolve(a, fn)
build["args"], _ = resolve(a, fn, false)
}

service["build"] = build
}

if e, ok := service["environment"]; ok {
service["environment"], _ = resolve(e, fn)
service["environment"], _ = resolve(e, fn, true)
}

var dependsOn map[string]any
Expand Down Expand Up @@ -178,12 +178,12 @@ func normalizeNetworks(dict map[string]any) {
}
}

func resolve(a any, fn func(s string) (string, bool)) (any, bool) {
func resolve(a any, fn func(s string) (string, bool), keepEmpty bool) (any, bool) {
switch v := a.(type) {
case []any:
var resolved []any
for _, val := range v {
if r, ok := resolve(val, fn); ok {
if r, ok := resolve(val, fn, keepEmpty); ok {
resolved = append(resolved, r)
}
}
Expand All @@ -197,6 +197,8 @@ func resolve(a any, fn func(s string) (string, bool)) (any, bool) {
}
if s, ok := fn(key); ok {
resolved[key] = s
} else if keepEmpty {
resolved[key] = nil
}
}
return resolved, true
Expand All @@ -205,6 +207,9 @@ func resolve(a any, fn func(s string) (string, bool)) (any, bool) {
if val, ok := fn(v); ok {
return fmt.Sprintf("%s=%s", v, val), true
}
if keepEmpty {
return v, true
}
return "", false
}
return v, true
Expand Down
40 changes: 40 additions & 0 deletions loader/normalize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,43 @@ services:
assert.NilError(t, err)
assert.DeepEqual(t, expect, model)
}

func TestNormalizeEnvironment(t *testing.T) {
project := `
name: myProject
services:
test:
environment:
- FOO
- BAR
- ZOT=QIX
`

expected := `
name: myProject
networks:
default:
name: myProject_default
services:
test:
environment:
- FOO
- BAR=bar
- ZOT=QIX
networks:
default: null
`
var model map[string]any
err := yaml.Unmarshal([]byte(project), &model)
assert.NilError(t, err)
model, err = Normalize(model, map[string]string{
"BAR": "bar",
"ZOT": "zot",
})
assert.NilError(t, err)

var expect map[string]any
err = yaml.Unmarshal([]byte(expected), &expect)
assert.NilError(t, err)
assert.DeepEqual(t, expect, model)
}