Skip to content

Commit

Permalink
introduce ability to use custom env_file format
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <[email protected]>
  • Loading branch information
ndeloof committed Oct 7, 2024
1 parent 8158217 commit 49efa50
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 17 deletions.
2 changes: 2 additions & 0 deletions dotenv/fixtures/custom.format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FOO:BAR
ZOT:QIX
38 changes: 38 additions & 0 deletions dotenv/format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
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 dotenv

import (
"fmt"
"io"
)

var formats = map[string]Parser{}

type Parser func(r io.Reader, filename string, lookup func(key string) (string, bool)) (map[string]string, error)

func RegisterFormat(format string, p Parser) {
formats[format] = p
}

func ParseWithFormat(r io.Reader, filename string, resolve LookupFn, format string) (map[string]string, error) {
parser, ok := formats[format]
if !ok {
return nil, fmt.Errorf("unsupported env_file format %q", format)
}
return parser(r, filename, resolve)
}
6 changes: 3 additions & 3 deletions dotenv/godotenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func ReadWithLookup(lookupFn LookupFn, filenames ...string) (map[string]string,
envMap := make(map[string]string)

for _, filename := range filenames {
individualEnvMap, individualErr := readFile(filename, lookupFn)
individualEnvMap, individualErr := ReadFile(filename, lookupFn)

if individualErr != nil {
return envMap, individualErr
Expand Down Expand Up @@ -129,7 +129,7 @@ func filenamesOrDefault(filenames []string) []string {
}

func loadFile(filename string, overload bool) error {
envMap, err := readFile(filename, nil)
envMap, err := ReadFile(filename, nil)
if err != nil {
return err
}
Expand All @@ -150,7 +150,7 @@ func loadFile(filename string, overload bool) error {
return nil
}

func readFile(filename string, lookupFn LookupFn) (map[string]string, error) {
func ReadFile(filename string, lookupFn LookupFn) (map[string]string, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
Expand Down
34 changes: 34 additions & 0 deletions dotenv/godotenv_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package dotenv

import (
"bufio"
"bytes"
"errors"
"io"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -708,3 +710,35 @@ func TestGetEnvFromFile(t *testing.T) {
_, err = GetEnvFromFile(nil, []string{f})
assert.Check(t, strings.HasSuffix(err.Error(), ".env is a directory"))
}

func TestLoadWithFormat(t *testing.T) {
envFileName := "fixtures/custom.format"
expectedValues := map[string]string{
"FOO": "BAR",
"ZOT": "QIX",
}

custom := func(r io.Reader, f string, lookup func(key string) (string, bool)) (map[string]string, error) {
vars := map[string]string{}
scanner := bufio.NewScanner(r)
for scanner.Scan() {
key, value, found := strings.Cut(scanner.Text(), ":")
if !found {
value, found = lookup(key)
if !found {
continue
}
}
vars[key] = value
}
return vars, nil
}

RegisterFormat("custom", custom)

f, err := os.Open(envFileName)
assert.NilError(t, err)
env, err := ParseWithFormat(f, envFileName, nil, "custom")
assert.NilError(t, err)
assert.DeepEqual(t, expectedValues, env)
}
3 changes: 3 additions & 0 deletions schema/compose-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,9 @@
"path": {
"type": "string"
},
"format": {
"type": "string"
},
"required": {
"type": ["boolean", "string"],
"default": true
Expand Down
1 change: 1 addition & 0 deletions types/envfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
type EnvFile struct {
Path string `yaml:"path,omitempty" json:"path,omitempty"`
Required bool `yaml:"required" json:"required"`
Format string `yaml:"format,omitempty" json:"format,omitempty"`
}

// MarshalYAML makes EnvFile implement yaml.Marshaler
Expand Down
42 changes: 28 additions & 14 deletions types/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,22 +616,11 @@ func (p Project) WithServicesEnvironmentResolved(discardEnvFiles bool) (*Project
}

for _, envFile := range service.EnvFiles {
if _, err := os.Stat(envFile.Path); os.IsNotExist(err) {
if envFile.Required {
return nil, fmt.Errorf("env file %s not found: %w", envFile.Path, err)
}
continue
}
b, err := os.ReadFile(envFile.Path)
vars, err := loadEnvFile(envFile, resolve)
if err != nil {
return nil, fmt.Errorf("failed to load %s: %w", envFile.Path, err)
return nil, err
}

fileVars, err := dotenv.ParseWithLookup(bytes.NewBuffer(b), resolve)
if err != nil {
return nil, fmt.Errorf("failed to read %s: %w", envFile.Path, err)
}
environment.OverrideBy(Mapping(fileVars).ToMappingWithEquals())
environment.OverrideBy(vars.ToMappingWithEquals())
}

service.Environment = environment.OverrideBy(service.Environment)
Expand All @@ -644,6 +633,31 @@ func (p Project) WithServicesEnvironmentResolved(discardEnvFiles bool) (*Project
return newProject, nil
}

func loadEnvFile(envFile EnvFile, resolve dotenv.LookupFn) (Mapping, error) {
if _, err := os.Stat(envFile.Path); os.IsNotExist(err) {
if envFile.Required {
return nil, fmt.Errorf("env file %s not found: %w", envFile.Path, err)
}
return nil, nil
}
file, err := os.Open(envFile.Path)
if err != nil {
return nil, err
}
defer file.Close() //nolint:errcheck

var fileVars map[string]string
if envFile.Format != "" {
fileVars, err = dotenv.ParseWithFormat(file, envFile.Path, resolve, envFile.Format)
} else {
fileVars, err = dotenv.ParseWithLookup(file, resolve)
}
if err != nil {
return nil, err
}
return fileVars, nil
}

func (p *Project) deepCopy() *Project {
if p == nil {
return nil
Expand Down

0 comments on commit 49efa50

Please sign in to comment.