github.com/kruglovmax/helm@v3.0.0-beta.3+incompatible/pkg/chart/loader/directory.go (about)

     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  	"io/ioutil"
    21  	"os"
    22  	"path/filepath"
    23  	"strings"
    24  
    25  	"github.com/pkg/errors"
    26  
    27  	"helm.sh/helm/internal/ignore"
    28  	"helm.sh/helm/internal/sympath"
    29  	"helm.sh/helm/pkg/chart"
    30  )
    31  
    32  // DirLoader loads a chart from a directory
    33  type DirLoader string
    34  
    35  // Load loads the chart
    36  func (l DirLoader) Load() (*chart.Chart, error) {
    37  	return LoadDir(string(l))
    38  }
    39  
    40  // LoadDir loads from a directory.
    41  //
    42  // This loads charts only from directories.
    43  func LoadDir(dir string) (*chart.Chart, error) {
    44  	topdir, err := filepath.Abs(dir)
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  
    49  	// Just used for errors.
    50  	c := &chart.Chart{}
    51  
    52  	rules := ignore.Empty()
    53  	ifile := filepath.Join(topdir, ignore.HelmIgnore)
    54  	if _, err := os.Stat(ifile); err == nil {
    55  		r, err := ignore.ParseFile(ifile)
    56  		if err != nil {
    57  			return c, err
    58  		}
    59  		rules = r
    60  	}
    61  	rules.AddDefaults()
    62  
    63  	files := []*BufferedFile{}
    64  	topdir += string(filepath.Separator)
    65  
    66  	walk := func(name string, fi os.FileInfo, err error) error {
    67  		n := strings.TrimPrefix(name, topdir)
    68  		if n == "" {
    69  			// No need to process top level. Avoid bug with helmignore .* matching
    70  			// empty names. See issue 1779.
    71  			return nil
    72  		}
    73  
    74  		// Normalize to / since it will also work on Windows
    75  		n = filepath.ToSlash(n)
    76  
    77  		if err != nil {
    78  			return err
    79  		}
    80  		if fi.IsDir() {
    81  			// Directory-based ignore rules should involve skipping the entire
    82  			// contents of that directory.
    83  			if rules.Ignore(n, fi) {
    84  				return filepath.SkipDir
    85  			}
    86  			return nil
    87  		}
    88  
    89  		// If a .helmignore file matches, skip this file.
    90  		if rules.Ignore(n, fi) {
    91  			return nil
    92  		}
    93  
    94  		data, err := ioutil.ReadFile(name)
    95  		if err != nil {
    96  			return errors.Wrapf(err, "error reading %s", n)
    97  		}
    98  
    99  		files = append(files, &BufferedFile{Name: n, Data: data})
   100  		return nil
   101  	}
   102  	if err = sympath.Walk(topdir, walk); err != nil {
   103  		return c, err
   104  	}
   105  
   106  	return LoadFiles(files)
   107  }