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