github.com/migueleliasweb/helm@v2.6.1+incompatible/pkg/chartutil/chartfile.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     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  	"fmt"
    22  	"io/ioutil"
    23  	"os"
    24  	"path/filepath"
    25  
    26  	"github.com/ghodss/yaml"
    27  
    28  	"k8s.io/helm/pkg/proto/hapi/chart"
    29  )
    30  
    31  // ApiVersionV1 is the API version number for version 1.
    32  //
    33  // This is ApiVersionV1 instead of APIVersionV1 to match the protobuf-generated name.
    34  const ApiVersionV1 = "v1"
    35  
    36  // UnmarshalChartfile takes raw Chart.yaml data and unmarshals it.
    37  func UnmarshalChartfile(data []byte) (*chart.Metadata, error) {
    38  	y := &chart.Metadata{}
    39  	err := yaml.Unmarshal(data, y)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  	return y, nil
    44  }
    45  
    46  // LoadChartfile loads a Chart.yaml file into a *chart.Metadata.
    47  func LoadChartfile(filename string) (*chart.Metadata, error) {
    48  	b, err := ioutil.ReadFile(filename)
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  	return UnmarshalChartfile(b)
    53  }
    54  
    55  // SaveChartfile saves the given metadata as a Chart.yaml file at the given path.
    56  //
    57  // 'filename' should be the complete path and filename ('foo/Chart.yaml')
    58  func SaveChartfile(filename string, cf *chart.Metadata) error {
    59  	out, err := yaml.Marshal(cf)
    60  	if err != nil {
    61  		return err
    62  	}
    63  	return ioutil.WriteFile(filename, out, 0644)
    64  }
    65  
    66  // IsChartDir validate a chart directory.
    67  //
    68  // Checks for a valid Chart.yaml.
    69  func IsChartDir(dirName string) (bool, error) {
    70  	if fi, err := os.Stat(dirName); err != nil {
    71  		return false, err
    72  	} else if !fi.IsDir() {
    73  		return false, fmt.Errorf("%q is not a directory", dirName)
    74  	}
    75  
    76  	chartYaml := filepath.Join(dirName, "Chart.yaml")
    77  	if _, err := os.Stat(chartYaml); os.IsNotExist(err) {
    78  		return false, fmt.Errorf("no Chart.yaml exists in directory %q", dirName)
    79  	}
    80  
    81  	chartYamlContent, err := ioutil.ReadFile(chartYaml)
    82  	if err != nil {
    83  		return false, fmt.Errorf("cannot read Chart.Yaml in directory %q", dirName)
    84  	}
    85  
    86  	chartContent, err := UnmarshalChartfile(chartYamlContent)
    87  	if err != nil {
    88  		return false, err
    89  	}
    90  	if chartContent == nil {
    91  		return false, errors.New("chart metadata (Chart.yaml) missing")
    92  	}
    93  	if chartContent.Name == "" {
    94  		return false, errors.New("invalid chart (Chart.yaml): name must not be empty")
    95  	}
    96  
    97  	return true, nil
    98  }