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 14, 2023
1 parent 82a567a commit 4467af4
Show file tree
Hide file tree
Showing 15 changed files with 220 additions and 20 deletions.
21 changes: 20 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,25 @@
]
},

"env_file": {
"oneOf": [
{"type": "string"},
{"$ref": "#/definitions/list_of_strings"},
{
"type": "array",
"items": {
"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, []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)
}
}
6 changes: 3 additions & 3 deletions transform/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/pkg/errors"
)

func transformPorts(data any, _ tree.Path) (any, error) {
func transformPorts(data any, p tree.Path) (any, error) {
switch entries := data.(type) {
case []any:
// We process the list instead of individual items here.
Expand Down Expand Up @@ -64,12 +64,12 @@ func transformPorts(data any, _ tree.Path) (any, error) {
case map[string]any:
ports = append(ports, value)
default:
return data, errors.Errorf("invalid type %T for port", value)
return data, errors.Errorf("%s: invalid type %T for port", p, value)
}
}
return ports, nil
default:
return data, errors.Errorf("invalid type %T for port", entries)
return data, errors.Errorf("%s: invalid type %T for port", p, entries)
}
}

Expand Down
4 changes: 2 additions & 2 deletions transform/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/pkg/errors"
)

func transformSSH(data any, _ tree.Path) (any, error) {
func transformSSH(data any, p tree.Path) (any, error) {
switch v := data.(type) {
case map[string]any:
return v, nil
Expand All @@ -47,6 +47,6 @@ func transformSSH(data any, _ tree.Path) (any, error) {
}
return result, nil
default:
return data, errors.Errorf("invalid type %T for ssh", v)
return data, errors.Errorf("%s: invalid type %T for ssh", p, v)
}
}
4 changes: 2 additions & 2 deletions transform/ulimits.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/pkg/errors"
)

func transformUlimits(data any, _ tree.Path) (any, error) {
func transformUlimits(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 transformUlimits(data any, _ tree.Path) (any, error) {
"single": 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)
}
}
4 changes: 2 additions & 2 deletions transform/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/pkg/errors"
)

func transformVolumeMount(data any, _ tree.Path) (any, error) {
func transformVolumeMount(data any, p tree.Path) (any, error) {
switch v := data.(type) {
case map[string]any:
return v, nil
Expand All @@ -34,6 +34,6 @@ func transformVolumeMount(data any, _ tree.Path) (any, error) {

return encode(volume)
default:
return data, errors.Errorf("invalid type %T for service volume mount", v)
return data, errors.Errorf("%s: invalid type %T for service volume mount", p, v)
}
}
41 changes: 41 additions & 0 deletions types/envfile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
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 types

import "encoding/json"

type EnvFile struct {
Path string `yaml:"path,omitempty" json:"path,omitempty"`
Required bool `yaml:"required" json:"required"`
}

// MarshalYAML makes EnvFile implement yaml.Marshaler
func (e *EnvFile) MarshalYAML() (interface{}, error) {
if e.Required {
return e.Path, nil
}
return e, nil
}

// MarshalYAML makes EnvFile implement json.Marshaler
func (e *EnvFile) MarshalJSON() ([]byte, error) {
if e.Required {
return []byte(e.Path), nil
}
// Pass as a value to avoid re-entering this method and use the default implementation
return json.Marshal(*e)
}
14 changes: 10 additions & 4 deletions types/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,10 +533,16 @@ func (p Project) ResolveServicesEnvironment(discardEnvFiles bool) error {
return p.Environment.Resolve(s)
}

for _, envFile := range service.EnvFile {
b, err := os.ReadFile(envFile)
for _, envFile := range service.EnvFiles {
if _, err := os.Stat(envFile.Path); os.IsNotExist(err) {
if envFile.Required {
return errors.Wrapf(err, "env file %s not found", envFile)
}
continue
}
b, err := os.ReadFile(envFile.Path)
if err != nil {
return errors.Wrapf(err, "Failed to load %s", envFile)
return errors.Wrapf(err, "failed to load %s", envFile)
}

fileVars, err := dotenv.ParseWithLookup(bytes.NewBuffer(b), resolve)
Expand All @@ -549,7 +555,7 @@ func (p Project) ResolveServicesEnvironment(discardEnvFiles bool) error {
service.Environment = environment.OverrideBy(service.Environment)

if discardEnvFiles {
service.EnvFile = nil
service.EnvFiles = nil
}
p.Services[i] = service
}
Expand Down
2 changes: 1 addition & 1 deletion types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ type ServiceConfig struct {
Entrypoint ShellCommand `yaml:"entrypoint,omitempty" json:"entrypoint"` // NOTE: we can NOT omitempty for JSON! see ShellCommand type for details.

Environment MappingWithEquals `yaml:"environment,omitempty" json:"environment,omitempty"`
EnvFile StringList `yaml:"env_file,omitempty" json:"env_file,omitempty"`
EnvFiles []EnvFile `yaml:"env_file,omitempty" json:"env_file,omitempty"`
Expose StringOrNumberList `yaml:"expose,omitempty" json:"expose,omitempty"`
Extends *ExtendsConfig `yaml:"extends,omitempty" json:"extends,omitempty"`
ExternalLinks []string `yaml:"external_links,omitempty" json:"external_links,omitempty"`
Expand Down

0 comments on commit 4467af4

Please sign in to comment.