github.com/sri09kanth/helm@v3.0.0-beta.3+incompatible/pkg/chartutil/chartfile.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/ioutil" 21 "os" 22 "path/filepath" 23 24 "github.com/pkg/errors" 25 "sigs.k8s.io/yaml" 26 27 "helm.sh/helm/pkg/chart" 28 ) 29 30 // LoadChartfile loads a Chart.yaml file into a *chart.Metadata. 31 func LoadChartfile(filename string) (*chart.Metadata, error) { 32 b, err := ioutil.ReadFile(filename) 33 if err != nil { 34 return nil, err 35 } 36 y := new(chart.Metadata) 37 err = yaml.Unmarshal(b, y) 38 return y, err 39 } 40 41 // SaveChartfile saves the given metadata as a Chart.yaml file at the given path. 42 // 43 // 'filename' should be the complete path and filename ('foo/Chart.yaml') 44 func SaveChartfile(filename string, cf *chart.Metadata) error { 45 out, err := yaml.Marshal(cf) 46 if err != nil { 47 return err 48 } 49 return ioutil.WriteFile(filename, out, 0644) 50 } 51 52 // IsChartDir validate a chart directory. 53 // 54 // Checks for a valid Chart.yaml. 55 func IsChartDir(dirName string) (bool, error) { 56 if fi, err := os.Stat(dirName); err != nil { 57 return false, err 58 } else if !fi.IsDir() { 59 return false, errors.Errorf("%q is not a directory", dirName) 60 } 61 62 chartYaml := filepath.Join(dirName, "Chart.yaml") 63 if _, err := os.Stat(chartYaml); os.IsNotExist(err) { 64 return false, errors.Errorf("no Chart.yaml exists in directory %q", dirName) 65 } 66 67 chartYamlContent, err := ioutil.ReadFile(chartYaml) 68 if err != nil { 69 return false, errors.Errorf("cannot read Chart.Yaml in directory %q", dirName) 70 } 71 72 chartContent := new(chart.Metadata) 73 if err := yaml.Unmarshal(chartYamlContent, &chartContent); err != nil { 74 return false, err 75 } 76 if chartContent == nil { 77 return false, errors.New("chart metadata (Chart.yaml) missing") 78 } 79 if chartContent.Name == "" { 80 return false, errors.New("invalid chart (Chart.yaml): name must not be empty") 81 } 82 83 return true, nil 84 }