Skip to content

Commit

Permalink
Handle empty chart path
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob LeGrone <[email protected]>
  • Loading branch information
jlegrone committed Feb 14, 2019
1 parent e3d58a1 commit 4c20cbe
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
9 changes: 3 additions & 6 deletions pkg/chart/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,8 @@ type DirectoryLister interface {
// ChartUtils is the interface that wraps chart-related methods
//
// LookupChartDir looks up the chart's root directory based on some chart file that has changed
//
// ReadChartYaml reads the `Chart.yaml` from the specified directory
type ChartUtils interface {
LookupChartDir(chartDirs []string, dir string) (string, error)
ReadChartYaml(dir string) (*util.ChartYaml, error)
}

// AccountValidator is the interface that wraps Git account validation
Expand Down Expand Up @@ -524,7 +521,7 @@ func (t *Testing) GetOldChartVersion(chart string) (string, error) {
return "", errors.Wrap(err, "Error reading old Chart.yaml")
}

chartYaml, err := util.ReadChartYaml([]byte(chartYamlContents))
chartYaml, err := util.UnmarshalChartYaml([]byte(chartYamlContents))
if err != nil {
return "", errors.Wrap(err, "Error reading old chart version")
}
Expand All @@ -534,7 +531,7 @@ func (t *Testing) GetOldChartVersion(chart string) (string, error) {

// GetNewChartVersion gets the new version from the currently checked out Chart.yaml file.
func (t *Testing) GetNewChartVersion(chart string) (string, error) {
chartYaml, err := t.chartUtils.ReadChartYaml(chart)
chartYaml, err := util.ReadChartYaml(chart)
if err != nil {
return "", errors.Wrap(err, "Error reading new chart version")
}
Expand All @@ -546,7 +543,7 @@ func (t *Testing) GetNewChartVersion(chart string) (string, error) {
func (t *Testing) ValidateMaintainers(chart string) error {
fmt.Println("Validating maintainers...")

chartYaml, err := t.chartUtils.ReadChartYaml(chart)
chartYaml, err := util.ReadChartYaml(chart)
if err != nil {
return err
}
Expand Down
24 changes: 16 additions & 8 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ package util

import (
"fmt"
"github.com/Masterminds/semver"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
"io/ioutil"
"math/rand"
"os"
"path"
"path/filepath"
"strings"
"time"

"github.com/Masterminds/semver"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)

const chars = "1234567890abcdefghijklmnopqrstuvwxyz"
Expand Down Expand Up @@ -146,21 +147,24 @@ func (u ChartUtils) LookupChartDir(chartDirs []string, dir string) (string, erro
return "", errors.New("no chart directory")
}

func (u ChartUtils) ReadChartYaml(dir string) (*ChartYaml, error) {
// ReadChartYaml attempts to parse Chart.yaml within the specified directory
// and return a newly allocated ChartYaml object. If no Chart.yaml is present
// or there is an error unmarshalling the file, an error will be returned.
func ReadChartYaml(dir string) (*ChartYaml, error) {
yamlBytes, err := ioutil.ReadFile(path.Join(dir, "Chart.yaml"))
if err != nil {
return nil, errors.Wrap(err, "Could not read 'Chart.yaml'")
}
return ReadChartYaml(yamlBytes)
return UnmarshalChartYaml(yamlBytes)
}

func ReadChartYaml(yamlBytes []byte) (*ChartYaml, error) {
// UnmarshalChartYaml parses the yaml encoded data and returns a newly
// allocated ChartYaml object.
func UnmarshalChartYaml(yamlBytes []byte) (*ChartYaml, error) {
chartYaml := &ChartYaml{}

if err := yaml.Unmarshal(yamlBytes, chartYaml); err != nil {
return nil, errors.Wrap(err, "Could not unmarshal 'Chart.yaml'")
}

return chartYaml, nil
}

Expand All @@ -178,6 +182,10 @@ func CompareVersions(left string, right string) (int, error) {

func CreateInstallParams(chart string, buildId string) (release string, namespace string) {
release = path.Base(chart)
if release == "." || release == "/" {
yaml, _ := ReadChartYaml(chart)
release = yaml.Name
}
namespace = release
if buildId != "" {
namespace = fmt.Sprintf("%s-%s", namespace, buildId)
Expand Down

0 comments on commit 4c20cbe

Please sign in to comment.