From dde1524cabd1f74499cf1187027ca35a044c8c54 Mon Sep 17 00:00:00 2001 From: Matt Butcher Date: Mon, 13 Jun 2016 13:59:36 -0600 Subject: [PATCH] fix(helm): fix two panics in 'helm search' --- cmd/helm/helm.go | 9 +++++++++ cmd/helm/init.go | 13 +++++++++++++ cmd/helm/search.go | 12 ++++++++---- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/cmd/helm/helm.go b/cmd/helm/helm.go index b63a122ea..0fe1cd589 100644 --- a/cmd/helm/helm.go +++ b/cmd/helm/helm.go @@ -109,6 +109,15 @@ func checkArgsLength(expectedNum, actualNum int, requiredArgs ...string) error { return nil } +// requireInit is a PreRunE implementation for validating that $HELM_HOME is configured. +func requireInit(cmd *cobra.Command, args []string) error { + err := requireHome() + if err != nil { + return fmt.Errorf("%s (try running 'helm init')", err) + } + return nil +} + // prettyError unwraps or rewrites certain errors to make them more user-friendly. func prettyError(err error) error { // This is ridiculous. Why is 'grpc.rpcError' not exported? The least they diff --git a/cmd/helm/init.go b/cmd/helm/init.go index cb49b72f3..d5a41a108 100644 --- a/cmd/helm/init.go +++ b/cmd/helm/init.go @@ -76,6 +76,19 @@ func installTiller() error { return nil } +// requireHome checks to see if $HELM_HOME exists, and returns an error if it does not. +func requireHome() error { + dirs := []string{homePath(), repositoryDirectory(), cacheDirectory(), localRepoDirectory()} + for _, d := range dirs { + if fi, err := os.Stat(d); err != nil { + return fmt.Errorf("directory %q is not configured", d) + } else if !fi.IsDir() { + return fmt.Errorf("expected %q to be a directory", d) + } + } + return nil +} + // ensureHome checks to see if $HELM_HOME exists // // If $HELM_HOME does not exist, this function will create it. diff --git a/cmd/helm/search.go b/cmd/helm/search.go index 354a51b91..ff3b366b9 100644 --- a/cmd/helm/search.go +++ b/cmd/helm/search.go @@ -17,10 +17,11 @@ func init() { } var searchCmd = &cobra.Command{ - Use: "search [keyword]", - Short: "Search for a keyword in charts", - Long: "Searches the known repositories cache files for the specified search string, looks at name and keywords", - RunE: search, + Use: "search [keyword]", + Short: "Search for a keyword in charts", + Long: "Searches the known repositories cache files for the specified search string, looks at name and keywords", + RunE: search, + PreRunE: requireInit, } func search(cmd *cobra.Command, args []string) error { @@ -47,6 +48,9 @@ func searchChartRefsForPattern(search string, chartRefs map[string]*repo.ChartRe matches = append(matches, k) continue } + if c.Chartfile == nil { + continue + } for _, keyword := range c.Chartfile.Keywords { if strings.Contains(keyword, search) { matches = append(matches, k)