...

Source file src/cuelang.org/go/cue/load/match.go

Documentation: cuelang.org/go/cue/load

     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"
    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  // A match represents the result of matching a single package pattern.
    28  type match struct {
    29  	Pattern string // the pattern itself
    30  	Literal bool   // whether it is a literal (no wildcards)
    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  // matchFile determines whether the file with the given name in the given directory
    45  // should be included in the package being constructed.
    46  // It returns the data read from the file.
    47  // If returnImports is true and name denotes a CUE file, matchFile reads
    48  // until the end of the imports (and returns that data) even though it only
    49  // considers text until the first non-comment.
    50  // If allTags is non-nil, matchFile records any encountered build tag
    51  // by setting allTags[tag] = true.
    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 // not a CUE file, don't record.
    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 // don't check shouldBuild for stdin
    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