github.com/stefanmcshane/helm@v0.0.0-20221213002717-88a4a2c6e77d/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 "github.com/stefanmcshane/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 // Pull out the dependencies of a v1 Chart, since there's no way 46 // to tell the serializer to skip a field for just this use case 47 savedDependencies := cf.Dependencies 48 if cf.APIVersion == chart.APIVersionV1 { 49 cf.Dependencies = nil 50 } 51 out, err := yaml.Marshal(cf) 52 if cf.APIVersion == chart.APIVersionV1 { 53 cf.Dependencies = savedDependencies 54 } 55 if err != nil { 56 return err 57 } 58 return ioutil.WriteFile(filename, out, 0644) 59 } 60 61 // IsChartDir validate a chart directory. 62 // 63 // Checks for a valid Chart.yaml. 64 func IsChartDir(dirName string) (bool, error) { 65 if fi, err := os.Stat(dirName); err != nil { 66 return false, err 67 } else if !fi.IsDir() { 68 return false, errors.Errorf("%q is not a directory", dirName) 69 } 70 71 chartYaml := filepath.Join(dirName, ChartfileName) 72 if _, err := os.Stat(chartYaml); os.IsNotExist(err) { 73 return false, errors.Errorf("no %s exists in directory %q", ChartfileName, dirName) 74 } 75 76 chartYamlContent, err := ioutil.ReadFile(chartYaml) 77 if err != nil { 78 return false, errors.Errorf("cannot read %s in directory %q", ChartfileName, dirName) 79 } 80 81 chartContent := new(chart.Metadata) 82 if err := yaml.Unmarshal(chartYamlContent, &chartContent); err != nil { 83 return false, err 84 } 85 if chartContent == nil { 86 return false, errors.Errorf("chart metadata (%s) missing", ChartfileName) 87 } 88 if chartContent.Name == "" { 89 return false, errors.Errorf("invalid chart (%s): name must not be empty", ChartfileName) 90 } 91 92 return true, nil 93 }