Skip to content

Commit

Permalink
env_file can be declared optional
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <[email protected]>
  • Loading branch information
ndeloof committed Dec 20, 2023
1 parent 81e1e90 commit 2ebf7a5
Show file tree
Hide file tree
Showing 20 changed files with 288 additions and 41 deletions.
4 changes: 2 additions & 2 deletions cli/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func TestProjectWithDiscardEnvFile(t *testing.T) {
service, err := p.GetService("simple")
assert.NilError(t, err)
assert.Equal(t, *service.Environment["DEFAULT_PORT"], "8080")
assert.Assert(t, service.EnvFile == nil)
assert.Assert(t, len(service.EnvFiles) == 0)
assert.Equal(t, service.Ports[0].Published, "8000")
}

Expand All @@ -287,7 +287,7 @@ func TestProjectWithMultipleEnvFile(t *testing.T) {
service, err := p.GetService("simple")
assert.NilError(t, err)
assert.Equal(t, *service.Environment["DEFAULT_PORT"], "9090")
assert.Assert(t, service.EnvFile == nil)
assert.Assert(t, len(service.EnvFiles) == 0)
assert.Equal(t, service.Ports[0].Published, "9000")
}

Expand Down
3 changes: 2 additions & 1 deletion loader/full-example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ services:
# env_file: .env
env_file:
- ./example1.env
- ./example2.env
- path: ./example2.env
required: false

# Mapping or list
# Mapping values can be strings, numbers or null
Expand Down
20 changes: 15 additions & 5 deletions loader/full-struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,15 @@ func services(workingDir, homeDir string) types.Services {
"ENV.WITH.DOT": strPtr("ok"),
"ENV_WITH_UNDERSCORE": strPtr("ok"),
},
EnvFile: []string{
filepath.Join(workingDir, "example1.env"),
filepath.Join(workingDir, "example2.env"),
EnvFiles: []types.EnvFile{
{
Path: filepath.Join(workingDir, "example1.env"),
Required: true,
},
{
Path: filepath.Join(workingDir, "example2.env"),
Required: false,
},
},
Expose: []string{"3000", "8000"},
ExternalLinks: []string{
Expand Down Expand Up @@ -729,7 +735,8 @@ services:
QUX: qux_from_environment
env_file:
- %s
- %s
- path: %s
required: false
expose:
- "3000"
- "8000"
Expand Down Expand Up @@ -1324,7 +1331,10 @@ func fullExampleJSON(workingDir, homeDir string) string {
},
"env_file": [
"%s",
"%s"
{
"path": "%s",
"required": false
}
],
"expose": [
"3000",
Expand Down
42 changes: 31 additions & 11 deletions loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,8 @@ services:
image: nginx
env_file:
- example1.env
- example2.env
- path: example2.env
required: false
`
expectedEnvironmentMap := types.MappingWithEquals{
"FOO": strPtr("foo_from_env_file"),
Expand All @@ -925,14 +926,21 @@ services:
options.ResolvePaths = false
})
assert.NilError(t, err)
assert.DeepEqual(t, configWithEnvFiles.Services["web"].EnvFile, types.StringList{"example1.env",
"example2.env"})
assert.DeepEqual(t, configWithEnvFiles.Services["web"].EnvFiles, []types.EnvFile{
{
Path: "example1.env",
Required: true,
},
{
Path: "example2.env",
Required: false,
}})
assert.DeepEqual(t, configWithEnvFiles.Services["web"].Environment, expectedEnvironmentMap)

// Custom behavior removes the `env_file` entries
configWithoutEnvFiles, err := Load(configDetails, WithDiscardEnvFiles)
assert.NilError(t, err)
assert.DeepEqual(t, configWithoutEnvFiles.Services["web"].EnvFile, types.StringList(nil))
assert.Equal(t, len(configWithoutEnvFiles.Services["web"].EnvFiles), 0)
assert.DeepEqual(t, configWithoutEnvFiles.Services["web"].Environment, expectedEnvironmentMap)
}

Expand Down Expand Up @@ -1929,7 +1937,11 @@ func TestLoadWithExtends(t *testing.T) {
Environment: types.MappingWithEquals{
"SOURCE": strPtr("extends"),
},
EnvFile: []string{expectedEnvFilePath},
EnvFiles: []types.EnvFile{
{
Path: expectedEnvFilePath,
Required: true,
}},
Networks: map[string]*types.ServiceNetworkConfig{"default": nil},
Volumes: []types.ServiceVolumeConfig{{
Type: "bind",
Expand Down Expand Up @@ -2090,8 +2102,10 @@ func TestLoadServiceWithEnvFile(t *testing.T) {
},
Services: types.Services{
"test": {
Name: "test",
EnvFile: []string{file.Name()},
Name: "test",
EnvFiles: []types.EnvFile{
{Path: file.Name(), Required: true},
},
},
},
}
Expand Down Expand Up @@ -2450,8 +2464,11 @@ services:
Name: "imported",
ContainerName: "extends", // as defined by ./testdata/subdir/extra.env
Environment: types.MappingWithEquals{"SOURCE": strPtr("extends")},
EnvFile: types.StringList{
filepath.Join(workingDir, "testdata", "subdir", "extra.env"),
EnvFiles: []types.EnvFile{
{
Path: filepath.Join(workingDir, "testdata", "subdir", "extra.env"),
Required: true,
},
},
Image: "nginx",
Volumes: []types.ServiceVolumeConfig{
Expand Down Expand Up @@ -2630,8 +2647,11 @@ services:
Name: "foo",
Image: "foo",
Environment: types.MappingWithEquals{"FOO": strPtr("BAR")},
EnvFile: types.StringList{
filepath.Join(config.WorkingDir, "testdata", "remote", "env"),
EnvFiles: []types.EnvFile{
{
Path: filepath.Join(config.WorkingDir, "testdata", "remote", "env"),
Required: true,
},
},
Volumes: []types.ServiceVolumeConfig{
{
Expand Down
2 changes: 1 addition & 1 deletion paths/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func ResolveRelativePaths(project map[string]any, base string) error {
r.resolvers = map[tree.Path]resolver{
"services.*.build.context": r.absContextPath,
"services.*.build.additional_contexts.*": r.absContextPath,
"services.*.env_file": r.absPath,
"services.*.env_file.*.path": r.absPath,
"services.*.extends.file": r.absPath,
"services.*.develop.watch.*.path": r.absPath,
"services.*.volumes.*": r.absVolumeMount,
Expand Down
32 changes: 31 additions & 1 deletion schema/compose-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@
"dns_search": {"$ref": "#/definitions/string_or_list"},
"domainname": {"type": "string"},
"entrypoint": {"$ref": "#/definitions/command"},
"env_file": {"$ref": "#/definitions/string_or_list"},
"env_file": {"$ref": "#/definitions/env_file"},
"environment": {"$ref": "#/definitions/list_or_dict"},

"expose": {
Expand Down Expand Up @@ -775,6 +775,36 @@
]
},

"env_file": {
"oneOf": [
{"type": "string"},
{
"type": "array",
"items": {
"oneOf": [
{"type": "string"},
{
"type": "object",
"additionalProperties": false,
"properties": {
"path": {
"type": "string"
},
"required": {
"type": "boolean",
"default": true
}
},
"required": [
"path"
]
}
]
}
}
]
},

"string_or_list": {
"oneOf": [
{"type": "string"},
Expand Down
2 changes: 1 addition & 1 deletion transform/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ func transformBuild(data any, p tree.Path) (any, error) {
"context": v,
}, nil
default:
return data, errors.Errorf("invalid type %T for build", v)
return data, errors.Errorf("%s: invalid type %T for build", p, v)
}
}
1 change: 1 addition & 0 deletions transform/canonical.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func init() {
transformers["services.*"] = transformService
transformers["services.*.build.secrets.*"] = transformFileMount
transformers["services.*.depends_on"] = transformDependsOn
transformers["services.*.env_file"] = transformEnvFile
transformers["services.*.extends"] = transformExtends
transformers["services.*.networks"] = transformServiceNetworks
transformers["services.*.volumes.*"] = transformVolumeMount
Expand Down
2 changes: 1 addition & 1 deletion transform/dependson.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ func transformDependsOn(data any, p tree.Path) (any, error) {
}
return d, nil
default:
return data, errors.Errorf("invalid type %T for depend_on", v)
return data, errors.Errorf("%s: invalid type %T for depend_on", p, v)
}
}
54 changes: 54 additions & 0 deletions transform/envfile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Copyright 2020 The Compose Specification Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package transform

import (
"github.com/compose-spec/compose-go/v2/tree"
"github.com/pkg/errors"
)

func transformEnvFile(data any, p tree.Path) (any, error) {
switch v := data.(type) {
case string:
return []any{
transformEnvFileValue(v),
}, nil
case []any:
for i, e := range v {
v[i] = transformEnvFileValue(e)
}
return v, nil
default:
return nil, errors.Errorf("%s: invalid type %T for env_file", p, v)
}
}

func transformEnvFileValue(data any) any {
switch v := data.(type) {
case string:
return map[string]any{
"path": v,
"required": true,
}
case map[string]any:
if _, ok := v["required"]; !ok {
v["required"] = true
}
return v
}
return nil
}
79 changes: 79 additions & 0 deletions transform/envfile_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Copyright 2020 The Compose Specification Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package transform

import (
"testing"

"github.com/compose-spec/compose-go/v2/tree"
"gopkg.in/yaml.v3"
"gotest.tools/v3/assert"
)

func TestSingle(t *testing.T) {
env, err := transformEnvFile(".env", tree.NewPath("service.test.env_file"))
assert.NilError(t, err)
assert.DeepEqual(t, env, []any{
map[string]any{
"path": ".env",
"required": true,
},
})
}

func TestSequence(t *testing.T) {
var in any
err := yaml.Unmarshal([]byte(`
- .env
- other.env
`), &in)
assert.NilError(t, err)
env, err := transformEnvFile(in, tree.NewPath("service.test.env_file"))
assert.NilError(t, err)
assert.DeepEqual(t, env, []any{
map[string]any{
"path": ".env",
"required": true,
},
map[string]any{
"path": "other.env",
"required": true,
},
})
}

func TestOptional(t *testing.T) {
var in any
err := yaml.Unmarshal([]byte(`
- .env
- path: other.env
required: false
`), &in)
assert.NilError(t, err)
env, err := transformEnvFile(in, tree.NewPath("service.test.env_file"))
assert.NilError(t, err)
assert.DeepEqual(t, env, []any{
map[string]any{
"path": ".env",
"required": true,
},
map[string]any{
"path": "other.env",
"required": false,
},
})
}
2 changes: 1 addition & 1 deletion transform/extends.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ func transformExtends(data any, p tree.Path) (any, error) {
"service": v,
}, nil
default:
return data, errors.Errorf("invalid type %T for extends", v)
return data, errors.Errorf("%s: invalid type %T for extends", p, v)
}
}
4 changes: 2 additions & 2 deletions transform/include.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/pkg/errors"
)

func transformInclude(data any, _ tree.Path) (any, error) {
func transformInclude(data any, p tree.Path) (any, error) {
switch v := data.(type) {
case map[string]any:
return v, nil
Expand All @@ -30,6 +30,6 @@ func transformInclude(data any, _ tree.Path) (any, error) {
"path": v,
}, nil
default:
return data, errors.Errorf("invalid type %T for external", v)
return data, errors.Errorf("%s: invalid type %T for external", p, v)
}
}
Loading

0 comments on commit 2ebf7a5

Please sign in to comment.