...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package load
16
17 import (
18 "io"
19 "path/filepath"
20 "strings"
21
22 "cuelang.org/go/cue/build"
23 "cuelang.org/go/cue/errors"
24 "cuelang.org/go/cue/token"
25 )
26
27
28 type match struct {
29 Pattern string
30 Literal bool
31 Pkgs []*build.Instance
32 Err errors.Error
33 }
34
35 var errExclude = errors.New("file rejected")
36
37 type cueError = errors.Error
38 type excludeError struct {
39 cueError
40 }
41
42 func (e excludeError) Is(err error) bool { return err == errExclude }
43
44
45
46
47
48
49
50
51
52 func matchFile(cfg *Config, file *build.File, returnImports, allFiles bool, allTags map[string]bool) (match bool, data []byte, err errors.Error) {
53 if fi := cfg.fileSystem.getOverlay(file.Filename); fi != nil {
54 if fi.file != nil {
55 file.Source = fi.file
56 } else {
57 file.Source = fi.contents
58 }
59 }
60
61 if file.Encoding != build.CUE {
62 return false, nil, nil
63 }
64
65 if file.Filename == "-" {
66 b, err2 := io.ReadAll(cfg.stdin())
67 if err2 != nil {
68 err = errors.Newf(token.NoPos, "read stdin: %v", err)
69 return
70 }
71 file.Source = b
72 return true, b, nil
73 }
74
75 name := filepath.Base(file.Filename)
76 if !cfg.filesMode {
77 for _, prefix := range []string{".", "_"} {
78 if strings.HasPrefix(name, prefix) {
79 return false, nil, &excludeError{
80 errors.Newf(token.NoPos, "filename starts with a '%s'", prefix),
81 }
82 }
83 }
84 }
85
86 f, err := cfg.fileSystem.openFile(file.Filename)
87 if err != nil {
88 return false, nil, err
89 }
90
91 data, err = readImports(f, false, nil)
92 f.Close()
93 if err != nil {
94 return false, nil,
95 errors.Newf(token.NoPos, "read %s: %v", file.Filename, err)
96 }
97
98 return true, data, nil
99 }
100
View as plain text