github.com/umeshredd/helm@v3.0.0-alpha.1+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 "strings" 24 25 "github.com/ghodss/yaml" 26 "github.com/pkg/errors" 27 28 "helm.sh/helm/pkg/chart" 29 ) 30 31 // LoadChartfile loads a Chart.yaml file into a *chart.Metadata. 32 func LoadChartfile(filename string) (*chart.Metadata, error) { 33 b, err := ioutil.ReadFile(filename) 34 if err != nil { 35 return nil, err 36 } 37 y := new(chart.Metadata) 38 err = yaml.Unmarshal(b, y) 39 return y, err 40 } 41 42 // SaveChartfile saves the given metadata as a Chart.yaml file at the given path. 43 // 44 // 'filename' should be the complete path and filename ('foo/Chart.yaml') 45 func SaveChartfile(filename string, cf *chart.Metadata) error { 46 out, err := yaml.Marshal(cf) 47 if err != nil { 48 return err 49 } 50 return ioutil.WriteFile(filename, out, 0644) 51 } 52 53 // IsChartDir validate a chart directory. 54 // 55 // Checks for a valid Chart.yaml. 56 func IsChartDir(dirName string) (bool, error) { 57 if fi, err := os.Stat(dirName); err != nil { 58 return false, err 59 } else if !fi.IsDir() { 60 return false, errors.Errorf("%q is not a directory", dirName) 61 } 62 63 chartYaml := filepath.Join(dirName, "Chart.yaml") 64 if _, err := os.Stat(chartYaml); os.IsNotExist(err) { 65 return false, errors.Errorf("no Chart.yaml exists in directory %q", dirName) 66 } 67 68 chartYamlContent, err := ioutil.ReadFile(chartYaml) 69 if err != nil { 70 return false, errors.Errorf("cannot read Chart.Yaml in directory %q", dirName) 71 } 72 73 chartContent := new(chart.Metadata) 74 if err := yaml.Unmarshal(chartYamlContent, &chartContent); err != nil { 75 return false, err 76 } 77 if chartContent == nil { 78 return false, errors.New("chart metadata (Chart.yaml) missing") 79 } 80 if chartContent.Name == "" { 81 return false, errors.New("invalid chart (Chart.yaml): name must not be empty") 82 } 83 84 return true, nil 85 } 86 87 // IsChartInstallable validates if a chart can be installed 88 // 89 // Application chart type is only installable 90 func IsChartInstallable(chart *chart.Chart) (bool, error) { 91 if IsLibraryChart(chart) { 92 return false, errors.New("Library charts are not installable") 93 } 94 validChartType, _ := IsValidChartType(chart) 95 if !validChartType { 96 return false, errors.New("Invalid chart types are not installable") 97 } 98 return true, nil 99 } 100 101 // IsValidChartType validates the chart type 102 // 103 // Valid types are: application or library 104 func IsValidChartType(chart *chart.Chart) (bool, error) { 105 chartType := chart.Metadata.Type 106 if chartType != "" && !strings.EqualFold(chartType, "library") && 107 !strings.EqualFold(chartType, "application") { 108 return false, errors.New("Invalid chart type. Valid types are: application or library") 109 } 110 return true, nil 111 } 112 113 // IsLibraryChart returns true if the chart is a library chart 114 func IsLibraryChart(c *chart.Chart) bool { 115 return strings.EqualFold(c.Metadata.Type, "library") 116 } 117 118 // IsTemplateValid returns true if the template is valid for the chart type 119 func IsTemplateValid(templateName string, isLibChart bool) bool { 120 if isLibChart { 121 return strings.HasPrefix(filepath.Base(templateName), "_") 122 } 123 return true 124 }