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 1, 2024
1 parent 2821e2c commit 32bb40f
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 6 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
37 changes: 37 additions & 0 deletions dotenv/format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
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"
)

var formats = map[string]Parser{}

type Parser func(filename string, lookup func(key string) (string, bool)) (map[string]string, error)

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

func ParseWithFormat(filename string, resolve LookupFn, format string) (map[string]string, error) {
parser, ok := formats[format]
if !ok {
return nil, fmt.Errorf("unknown env_file format %q", format)
}
return parser(filename, resolve)
}
2 changes: 1 addition & 1 deletion dotenv/godotenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
42 changes: 42 additions & 0 deletions dotenv/godotenv_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dotenv

import (
"bufio"
"bytes"
"errors"
"os"
Expand Down Expand Up @@ -708,3 +709,44 @@ 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(f string, lookup LookupFn) (map[string]string, error) {
b, err := os.ReadFile(f)
if err != nil {
return nil, err
}
r := bytes.NewReader(b)
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)

Check failure on line 741 in dotenv/godotenv_test.go

View workflow job for this annotation

GitHub Actions / test (1.21, ubuntu-latest)

cannot use custom (variable of type func(f string, lookup LookupFn) (map[string]string, error)) as Parser value in argument to RegisterFormat (typecheck)
env, err := ParseWithFormat(envFileName, func(s string) (string, bool) {
switch s {
case "ZOT":
return "QIX", true
default:
return "", false
}
}, "custom")
assert.NilError(t, err)
assert.DeepEqual(t, expectedValues, env)
}
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
11 changes: 6 additions & 5 deletions types/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,12 +622,13 @@ func (p Project) WithServicesEnvironmentResolved(discardEnvFiles bool) (*Project
}
continue
}
b, err := os.ReadFile(envFile.Path)
if err != nil {
return nil, fmt.Errorf("failed to load %s: %w", envFile.Path, err)
var fileVars map[string]string
var err error
if envFile.Format != "" {
fileVars, err = dotenv.ParseWithFormat(envFile.Path, resolve, envFile.Format)
} else {
fileVars, err = dotenv.ReadFile(envFile.Path, resolve)
}

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

0 comments on commit 32bb40f

Please sign in to comment.