this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

cue/interpreter/embed: fix handling of dir matching glob

Currently, the MVP embed implementation sees @embed(glob=dir/*) and
tries to embed dir/subdir. However '*' should only match files, and so
subdir should be skipped.

This CL fixes the implementation.

Per the design in https://cuelang.org/discussion/3264, we will likely
add support for '**' to match 0 or more path elements later. For now the
use of '**' results in an error, but we also update that error message
to indicate that we don't "yet" support '**'.

Fixes #3267.

Signed-off-by: Paul Jolly <paul@myitcv.io>
Change-Id: I6e17aaf4e39b1b794951a9e934b7dd47e052ad4c
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1197338
Reviewed-by: Marcel van Lohuizen <mpvl@gmail.com>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>

+16 -8
+3 -2
cmd/cue/cmd/testdata/script/embed.txtar
··· 23 23 24 24 c: _ @embed(file="test.json", type=text) 25 25 26 - d: _ @embed(glob="y/*.*", type=yaml) 26 + d: _ @embed(glob="y/*", type=yaml) 27 27 28 28 d: _ @embed(glob="x/*.yaml") // merge into the same map 29 29 30 30 f: _ @embed(file="openapi.json", type=openapi) 31 31 32 32 g: _ @embed(file="openapi.json") // test no auto mode! 33 - 34 33 35 34 special: { 36 35 // These are all valid. ··· 61 60 { "z": 47 } 62 61 -- .y/test.json -- 63 62 { "z": 48 } 63 + -- y/subdir/another.json -- 64 + { "z": 49 } 64 65 -- _z/test.json -- 65 66 -- x/input.yaml -- 66 67 a1: 2
+1 -3
cmd/cue/cmd/testdata/script/embed_err.txtar
··· 86 86 ./test.cue:11:8 87 87 @embed: open y/test.json: no such file or directory: 88 88 ./test.cue:13:13 89 - @embed: cannot embed directories: 90 - ./test.cue:15:8 91 89 @embed: path not normalized, use "x" instead: 92 90 ./test.cue:17:13 93 91 @embed: cannot refer to parent directory: 94 92 ./test.cue:19:15 95 93 @embed: only relative files are allowed: 96 94 ./test.cue:21:12 97 - @embed: double star not supported in glob: 95 + @embed: double star not (yet) supported in glob: 98 96 ./test.cue:23:15 99 97 @embed: streaming not implemented: found more than one value in file: 100 98 ./test.cue:25:11
+12 -3
cue/interpreter/embed/embed.go
··· 139 139 140 140 // file system cache 141 141 dir string 142 - fs fs.FS 142 + fs fs.StatFS 143 143 pos token.Pos 144 144 } 145 145 ··· 178 178 // TODO: obtain a fs.FS from load or something similar. 179 179 dir := filepath.Dir(pos.File().Name()) 180 180 if c.dir != dir { 181 - c.fs = os.DirFS(dir) 181 + c.fs = os.DirFS(dir).(fs.StatFS) // Documented as implementing fs.StatFS 182 182 c.dir = dir 183 183 } 184 184 ··· 213 213 } 214 214 215 215 if strings.Contains(glob, "**") { 216 - return nil, errors.Newf(c.pos, "double star not supported in glob") 216 + return nil, errors.Newf(c.pos, "double star not (yet) supported in glob") 217 217 } 218 218 219 219 m := &adt.StructLit{} ··· 224 224 } 225 225 226 226 for _, f := range matches { 227 + // TODO: lots of stat calls happening in this MVP so another won't hurt. 228 + // We don't support '**' initially, and '*' only matches files, so skip 229 + // any directories. 230 + if fi, err := c.fs.Stat(f); err != nil { 231 + return nil, errors.Newf(c.pos, "failed to stat %s: %v", f, err) 232 + } else if fi.IsDir() { 233 + continue 234 + } 235 + 227 236 expr, err := c.decodeFile(f, scope, schema) 228 237 if err != nil { 229 238 return nil, err