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 2, 2024
1 parent 2821e2c commit 2d728e7
Show file tree
Hide file tree
Showing 8 changed files with 259 additions and 149 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)
}
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
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 func(key string) (string, bool)) (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)
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)
}
3 changes: 3 additions & 0 deletions schema/compose-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,9 @@
"path": {
"type": "string"
},
"format": {
"type": "string"
},
"required": {
"type": ["boolean", "string"],
"default": true
Expand Down
Loading

0 comments on commit 2d728e7

Please sign in to comment.