github.com/joomcode/cue@v0.4.4-0.20221111115225-539fe3512047/cue/load/match.go (about) 1 // Copyright 2018 The CUE Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package load 16 17 import ( 18 "io/ioutil" 19 "path/filepath" 20 "strings" 21 22 "github.com/joomcode/cue/cue/build" 23 "github.com/joomcode/cue/cue/errors" 24 "github.com/joomcode/cue/cue/token" 25 ) 26 27 var errExclude = errors.New("file rejected") 28 29 type cueError = errors.Error 30 type excludeError struct { 31 cueError 32 } 33 34 func (e excludeError) Is(err error) bool { return err == errExclude } 35 36 // matchFile determines whether the file with the given name in the given directory 37 // should be included in the package being constructed. 38 // It returns the data read from the file. 39 // If returnImports is true and name denotes a CUE file, matchFile reads 40 // until the end of the imports (and returns that data) even though it only 41 // considers text until the first non-comment. 42 // If allTags is non-nil, matchFile records any encountered build tag 43 // by setting allTags[tag] = true. 44 func matchFile(cfg *Config, file *build.File, returnImports, allFiles bool, allTags map[string]bool) (match bool, data []byte, err errors.Error) { 45 if fi := cfg.fileSystem.getOverlay(file.Filename); fi != nil { 46 if fi.file != nil { 47 file.Source = fi.file 48 } else { 49 file.Source = fi.contents 50 } 51 } 52 53 if file.Encoding != build.CUE { 54 return false, nil, nil // not a CUE file, don't record. 55 } 56 57 if file.Filename == "-" { 58 b, err2 := ioutil.ReadAll(cfg.stdin()) 59 if err2 != nil { 60 err = errors.Newf(token.NoPos, "read stdin: %v", err) 61 return 62 } 63 file.Source = b 64 return true, b, nil // don't check shouldBuild for stdin 65 } 66 67 name := filepath.Base(file.Filename) 68 if !cfg.filesMode && strings.HasPrefix(name, ".") { 69 return false, nil, &excludeError{ 70 errors.Newf(token.NoPos, "filename starts with a '.'"), 71 } 72 } 73 74 if strings.HasPrefix(name, "_") { 75 return false, nil, &excludeError{ 76 errors.Newf(token.NoPos, "filename starts with a '_"), 77 } 78 } 79 80 f, err := cfg.fileSystem.openFile(file.Filename) 81 if err != nil { 82 return false, nil, err 83 } 84 85 data, err = readImports(f, false, nil) 86 f.Close() 87 if err != nil { 88 return false, nil, 89 errors.Newf(token.NoPos, "read %s: %v", file.Filename, err) 90 } 91 92 return true, data, nil 93 }