Add flag to exit with 1 if no results found

The `--fail-if-no-results-found` flag exit with status code 1 if no
search results found during `helm search repo` and `helm search hub`
commands.  This is useful for automation scripts.

closes #7197

Signed-off-by: Baiju Muthukadan <baiju.m.mail@gmail.com>
pull/9750/head
Baiju Muthukadan 4 years ago
parent c137bfb68b
commit 9db231d4c5

@ -19,6 +19,7 @@ package main
import (
"fmt"
"io"
"os"
"strings"
"github.com/gosuri/uitable"
@ -54,6 +55,7 @@ type searchHubOptions struct {
maxColWidth uint
outputFormat output.Format
listRepoURL bool
noResultsFail bool
}
func newSearchHubCmd(out io.Writer) *cobra.Command {
@ -72,7 +74,7 @@ func newSearchHubCmd(out io.Writer) *cobra.Command {
f.StringVar(&o.searchEndpoint, "endpoint", "https://hub.helm.sh", "Hub instance to query for charts")
f.UintVar(&o.maxColWidth, "max-col-width", 50, "maximum column width for output table")
f.BoolVar(&o.listRepoURL, "list-repo-url", false, "print charts repository URL")
f.BoolVarP(&o.noResultsFail, "fail-if-no-results-found", "f", false, "exit with status code 1 if no results found")
bindOutputFlag(cmd, &o.outputFormat)
return cmd
@ -91,7 +93,7 @@ func (o *searchHubOptions) run(out io.Writer, args []string) error {
return fmt.Errorf("unable to perform search against %q", o.searchEndpoint)
}
return o.outputFormat.Write(out, newHubSearchWriter(results, o.searchEndpoint, o.maxColWidth, o.listRepoURL))
return o.outputFormat.Write(out, newHubSearchWriter(results, o.searchEndpoint, o.maxColWidth, o.noResultsFail, o.listRepoURL))
}
type hubChartRepo struct {
@ -111,9 +113,10 @@ type hubSearchWriter struct {
elements []hubChartElement
columnWidth uint
listRepoURL bool
noResultsFail bool
}
func newHubSearchWriter(results []monocular.SearchResult, endpoint string, columnWidth uint, listRepoURL bool) *hubSearchWriter {
func newHubSearchWriter(results []monocular.SearchResult, endpoint string, columnWidth uint, listRepoURL, noResultsFail bool) *hubSearchWriter {
var elements []hubChartElement
for _, r := range results {
// Backwards compatibility for Monocular
@ -126,7 +129,7 @@ func newHubSearchWriter(results []monocular.SearchResult, endpoint string, colum
elements = append(elements, hubChartElement{url, r.Relationships.LatestChartVersion.Data.Version, r.Relationships.LatestChartVersion.Data.AppVersion, r.Attributes.Description, hubChartRepo{URL: r.Attributes.Repo.URL, Name: r.Attributes.Repo.Name}})
}
return &hubSearchWriter{elements, columnWidth, listRepoURL}
return &hubSearchWriter{elements, columnWidth, listRepoURL, noResultsFail}
}
func (h *hubSearchWriter) WriteTable(out io.Writer) error {
@ -135,6 +138,9 @@ func (h *hubSearchWriter) WriteTable(out io.Writer) error {
if err != nil {
return fmt.Errorf("unable to write results: %s", err)
}
if h.noResultsFail {
os.Exit(1)
}
return nil
}
table := uitable.New()

@ -72,6 +72,7 @@ type searchRepoOptions struct {
repoFile string
repoCacheDir string
outputFormat output.Format
noResultsFail bool
}
func newSearchRepoCmd(out io.Writer) *cobra.Command {
@ -94,6 +95,7 @@ func newSearchRepoCmd(out io.Writer) *cobra.Command {
f.BoolVar(&o.devel, "devel", false, "use development versions (alpha, beta, and release candidate releases), too. Equivalent to version '>0.0.0-0'. If --version is set, this is ignored")
f.StringVar(&o.version, "version", "", "search using semantic versioning constraints on repositories you have added")
f.UintVar(&o.maxColWidth, "max-col-width", 50, "maximum column width for output table")
f.BoolVarP(&o.noResultsFail, "fail-if-no-results-found", "f", false, "exit with status code 1 if no results found")
bindOutputFlag(cmd, &o.outputFormat)
return cmd
@ -124,7 +126,7 @@ func (o *searchRepoOptions) run(out io.Writer, args []string) error {
return err
}
return o.outputFormat.Write(out, &repoSearchWriter{data, o.maxColWidth})
return o.outputFormat.Write(out, &repoSearchWriter{data, o.maxColWidth, o.noResultsFail})
}
func (o *searchRepoOptions) setupSearchedVersion() {
@ -207,6 +209,7 @@ type repoChartElement struct {
type repoSearchWriter struct {
results []*search.Result
columnWidth uint
noResultsFail bool
}
func (r *repoSearchWriter) WriteTable(out io.Writer) error {
@ -215,6 +218,9 @@ func (r *repoSearchWriter) WriteTable(out io.Writer) error {
if err != nil {
return fmt.Errorf("unable to write results: %s", err)
}
if r.noResultsFail {
os.Exit(1)
}
return nil
}
table := uitable.New()

Loading…
Cancel
Save