Merge pull request #2283 from sushilkm/issues/2276

Validates chart-dir before running dependency list command
pull/2294/head
Matt Butcher 8 years ago committed by GitHub
commit d4e129d55c

@ -17,7 +17,11 @@ limitations under the License.
package chartutil
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/ghodss/yaml"
@ -58,3 +62,37 @@ func SaveChartfile(filename string, cf *chart.Metadata) error {
}
return ioutil.WriteFile(filename, out, 0644)
}
// IsChartDir validate a chart directory.
//
// Checks for a valid Chart.yaml.
func IsChartDir(dirName string) (bool, error) {
if fi, err := os.Stat(dirName); err != nil {
return false, err
} else if !fi.IsDir() {
return false, fmt.Errorf("%q is not a directory", dirName)
}
chartYaml := filepath.Join(dirName, "Chart.yaml")
if _, err := os.Stat(chartYaml); os.IsNotExist(err) {
return false, fmt.Errorf("no Chart.yaml exists in directory %q", dirName)
}
chartYamlContent, err := ioutil.ReadFile(chartYaml)
if err != nil {
return false, fmt.Errorf("cannot read Chart.Yaml in directory %q", dirName)
}
chartContent, err := UnmarshalChartfile(chartYamlContent)
if err != nil {
return false, err
}
if chartContent == nil {
return false, errors.New("chart metadata (Chart.yaml) missing")
}
if chartContent.Name == "" {
return false, errors.New("invalid chart (Chart.yaml): name must not be empty")
}
return true, nil
}

@ -95,3 +95,16 @@ func verifyChartfile(t *testing.T, f *chart.Metadata, name string) {
}
}
}
func TestIsChartDir(t *testing.T) {
validChartDir, err := IsChartDir("testdata/frobnitz")
if !validChartDir {
t.Errorf("unexpected error while reading chart-directory: (%v)", err)
return
}
validChartDir, err = IsChartDir("testdata")
if validChartDir || err == nil {
t.Errorf("expected error but did not get any")
return
}
}

@ -48,6 +48,9 @@ func Load(name string) (*chart.Chart, error) {
return nil, err
}
if fi.IsDir() {
if validChart, err := IsChartDir(name); !validChart {
return nil, err
}
return LoadDir(name)
}
return LoadFile(name)

Loading…
Cancel
Save