1
1
mirror of https://github.com/wader/fq.git synced 2024-11-23 18:56:52 +03:00

interp: Fix file leak and always cache for now

This commit is contained in:
Mattias Wadman 2021-10-26 11:50:59 +02:00
parent 826c8bd555
commit 80a69977a1

View File

@ -565,16 +565,15 @@ func (i *Interp) Eval(ctx context.Context, c interface{}, src string, srcFilenam
pathPrefixes := []struct { pathPrefixes := []struct {
prefix string prefix string
cache bool fn func(filename string) (io.ReadCloser, error)
fn func(filename string) (io.Reader, error)
}{ }{
{ {
"@builtin/", true, func(filename string) (io.Reader, error) { "@builtin/", func(filename string) (io.ReadCloser, error) {
return builtinFS.Open(filename) return builtinFS.Open(filename)
}, },
}, },
{ {
"@config/", false, func(filename string) (io.Reader, error) { "@config/", func(filename string) (io.ReadCloser, error) {
configDir, err := i.os.ConfigDir() configDir, err := i.os.ConfigDir()
if err != nil { if err != nil {
return nil, err return nil, err
@ -583,7 +582,7 @@ func (i *Interp) Eval(ctx context.Context, c interface{}, src string, srcFilenam
}, },
}, },
{ {
"", false, func(filename string) (io.Reader, error) { "", func(filename string) (io.ReadCloser, error) {
// TODO: jq $ORIGIN // TODO: jq $ORIGIN
if filepath.IsAbs(filename) { if filepath.IsAbs(filename) {
@ -606,10 +605,8 @@ func (i *Interp) Eval(ctx context.Context, c interface{}, src string, srcFilenam
continue continue
} }
if p.cache { if q, ok := ni.includeCache[filename]; ok {
if q, ok := ni.includeCache[filename]; ok { return q, nil
return q, nil
}
} }
filenamePart := strings.TrimPrefix(filename, p.prefix) filenamePart := strings.TrimPrefix(filename, p.prefix)
@ -618,8 +615,9 @@ func (i *Interp) Eval(ctx context.Context, c interface{}, src string, srcFilenam
if !isTry { if !isTry {
return nil, err return nil, err
} }
f = &bytes.Buffer{} f = io.NopCloser(&bytes.Buffer{})
} }
defer f.Close()
b, err := io.ReadAll(f) b, err := io.ReadAll(f)
if err != nil { if err != nil {
@ -691,9 +689,7 @@ func (i *Interp) Eval(ctx context.Context, c interface{}, src string, srcFilenam
i.ImportPath = rewritePath(basePath, i.ImportPath) i.ImportPath = rewritePath(basePath, i.ImportPath)
} }
if p.cache { i.includeCache[filename] = q
i.includeCache[filename] = q
}
return q, nil return q, nil
} }