...

Source file src/helm.sh/helm/v3/pkg/chart/loader/directory.go

Documentation: helm.sh/helm/v3/pkg/chart/loader

     1  /*
     2  Copyright The Helm Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package loader
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"os"
    23  	"path/filepath"
    24  	"strings"
    25  
    26  	"github.com/pkg/errors"
    27  
    28  	"helm.sh/helm/v3/internal/sympath"
    29  	"helm.sh/helm/v3/pkg/chart"
    30  	"helm.sh/helm/v3/pkg/ignore"
    31  )
    32  
    33  var utf8bom = []byte{0xEF, 0xBB, 0xBF}
    34  
    35  // DirLoader loads a chart from a directory
    36  type DirLoader string
    37  
    38  // Load loads the chart
    39  func (l DirLoader) Load() (*chart.Chart, error) {
    40  	return LoadDir(string(l))
    41  }
    42  
    43  // LoadDir loads from a directory.
    44  //
    45  // This loads charts only from directories.
    46  func LoadDir(dir string) (*chart.Chart, error) {
    47  	topdir, err := filepath.Abs(dir)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  
    52  	// Just used for errors.
    53  	c := &chart.Chart{}
    54  
    55  	rules := ignore.Empty()
    56  	ifile := filepath.Join(topdir, ignore.HelmIgnore)
    57  	if _, err := os.Stat(ifile); err == nil {
    58  		r, err := ignore.ParseFile(ifile)
    59  		if err != nil {
    60  			return c, err
    61  		}
    62  		rules = r
    63  	}
    64  	rules.AddDefaults()
    65  
    66  	files := []*BufferedFile{}
    67  	topdir += string(filepath.Separator)
    68  
    69  	walk := func(name string, fi os.FileInfo, err error) error {
    70  		n := strings.TrimPrefix(name, topdir)
    71  		if n == "" {
    72  			// No need to process top level. Avoid bug with helmignore .* matching
    73  			// empty names. See issue 1779.
    74  			return nil
    75  		}
    76  
    77  		// Normalize to / since it will also work on Windows
    78  		n = filepath.ToSlash(n)
    79  
    80  		if err != nil {
    81  			return err
    82  		}
    83  		if fi.IsDir() {
    84  			// Directory-based ignore rules should involve skipping the entire
    85  			// contents of that directory.
    86  			if rules.Ignore(n, fi) {
    87  				return filepath.SkipDir
    88  			}
    89  			return nil
    90  		}
    91  
    92  		// If a .helmignore file matches, skip this file.
    93  		if rules.Ignore(n, fi) {
    94  			return nil
    95  		}
    96  
    97  		// Irregular files include devices, sockets, and other uses of files that
    98  		// are not regular files. In Go they have a file mode type bit set.
    99  		// See https://golang.org/pkg/os/#FileMode for examples.
   100  		if !fi.Mode().IsRegular() {
   101  			return fmt.Errorf("cannot load irregular file %s as it has file mode type bits set", name)
   102  		}
   103  
   104  		data, err := os.ReadFile(name)
   105  		if err != nil {
   106  			return errors.Wrapf(err, "error reading %s", n)
   107  		}
   108  
   109  		data = bytes.TrimPrefix(data, utf8bom)
   110  
   111  		files = append(files, &BufferedFile{Name: n, Data: data})
   112  		return nil
   113  	}
   114  	if err = sympath.Walk(topdir, walk); err != nil {
   115  		return c, err
   116  	}
   117  
   118  	return LoadFiles(files)
   119  }
   120  

View as plain text