Skip to content

Commit 7ecd36f

Browse files
ndeloofglours
authored andcommitted
Report file being process in dotenv parsing error
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
1 parent 2955d4e commit 7ecd36f

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

dotenv/format.go

+16-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,17 @@ import (
2121
"io"
2222
)
2323

24-
var formats = map[string]Parser{}
24+
const DotEnv = ".env"
25+
26+
var formats = map[string]Parser{
27+
DotEnv: func(r io.Reader, filename string, lookup func(key string) (string, bool)) (map[string]string, error) {
28+
m, err := ParseWithLookup(r, lookup)
29+
if err != nil {
30+
return nil, fmt.Errorf("failed to read %s: %w", filename, err)
31+
}
32+
return m, nil
33+
},
34+
}
2535

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

@@ -30,9 +40,12 @@ func RegisterFormat(format string, p Parser) {
3040
}
3141

3242
func ParseWithFormat(r io.Reader, filename string, resolve LookupFn, format string) (map[string]string, error) {
33-
parser, ok := formats[format]
43+
if format == "" {
44+
format = DotEnv
45+
}
46+
fn, ok := formats[format]
3447
if !ok {
3548
return nil, fmt.Errorf("unsupported env_file format %q", format)
3649
}
37-
return parser(r, filename, resolve)
50+
return fn(r, filename, resolve)
3851
}

types/project.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -722,12 +722,7 @@ func loadMappingFile(path string, format string, resolve dotenv.LookupFn) (Mappi
722722
}
723723
defer file.Close()
724724

725-
var fileVars map[string]string
726-
if format != "" {
727-
fileVars, err = dotenv.ParseWithFormat(file, path, resolve, format)
728-
} else {
729-
fileVars, err = dotenv.ParseWithLookup(file, resolve)
730-
}
725+
fileVars, err := dotenv.ParseWithFormat(file, path, resolve, format)
731726
if err != nil {
732727
return nil, err
733728
}

0 commit comments

Comments
 (0)