github.com/stefanmcshane/helm@v0.0.0-20221213002717-88a4a2c6e77d/pkg/chartutil/expand.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 chartutil
    18  
    19  import (
    20  	"io"
    21  	"io/ioutil"
    22  	"os"
    23  	"path/filepath"
    24  
    25  	securejoin "github.com/cyphar/filepath-securejoin"
    26  	"github.com/pkg/errors"
    27  	"sigs.k8s.io/yaml"
    28  
    29  	"github.com/stefanmcshane/helm/pkg/chart"
    30  	"github.com/stefanmcshane/helm/pkg/chart/loader"
    31  )
    32  
    33  // Expand uncompresses and extracts a chart into the specified directory.
    34  func Expand(dir string, r io.Reader) error {
    35  	files, err := loader.LoadArchiveFiles(r)
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	// Get the name of the chart
    41  	var chartName string
    42  	for _, file := range files {
    43  		if file.Name == "Chart.yaml" {
    44  			ch := &chart.Metadata{}
    45  			if err := yaml.Unmarshal(file.Data, ch); err != nil {
    46  				return errors.Wrap(err, "cannot load Chart.yaml")
    47  			}
    48  			chartName = ch.Name
    49  		}
    50  	}
    51  	if chartName == "" {
    52  		return errors.New("chart name not specified")
    53  	}
    54  
    55  	// Find the base directory
    56  	chartdir, err := securejoin.SecureJoin(dir, chartName)
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	// Copy all files verbatim. We don't parse these files because parsing can remove
    62  	// comments.
    63  	for _, file := range files {
    64  		outpath, err := securejoin.SecureJoin(chartdir, file.Name)
    65  		if err != nil {
    66  			return err
    67  		}
    68  
    69  		// Make sure the necessary subdirs get created.
    70  		basedir := filepath.Dir(outpath)
    71  		if err := os.MkdirAll(basedir, 0755); err != nil {
    72  			return err
    73  		}
    74  
    75  		if err := ioutil.WriteFile(outpath, file.Data, 0644); err != nil {
    76  			return err
    77  		}
    78  	}
    79  
    80  	return nil
    81  }
    82  
    83  // ExpandFile expands the src file into the dest directory.
    84  func ExpandFile(dest, src string) error {
    85  	h, err := os.Open(src)
    86  	if err != nil {
    87  		return err
    88  	}
    89  	defer h.Close()
    90  	return Expand(dest, h)
    91  }