@ -19,6 +19,7 @@ package main
import (
import (
"fmt"
"fmt"
"io"
"io"
"os"
"strings"
"strings"
"github.com/gosuri/uitable"
"github.com/gosuri/uitable"
@ -54,6 +55,7 @@ type searchHubOptions struct {
maxColWidth uint
maxColWidth uint
outputFormat output . Format
outputFormat output . Format
listRepoURL bool
listRepoURL bool
noResultsFail bool
}
}
func newSearchHubCmd ( out io . Writer ) * cobra . Command {
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 . 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 . 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 . 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 )
bindOutputFlag ( cmd , & o . outputFormat )
return cmd
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 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 {
type hubChartRepo struct {
@ -108,12 +110,13 @@ type hubChartElement struct {
}
}
type hubSearchWriter struct {
type hubSearchWriter struct {
elements [ ] hubChartElement
elements [ ] hubChartElement
columnWidth uint
columnWidth uint
listRepoURL bool
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
var elements [ ] hubChartElement
for _ , r := range results {
for _ , r := range results {
// Backwards compatibility for Monocular
// 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 } } )
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 {
func ( h * hubSearchWriter ) WriteTable ( out io . Writer ) error {
@ -135,6 +138,9 @@ func (h *hubSearchWriter) WriteTable(out io.Writer) error {
if err != nil {
if err != nil {
return fmt . Errorf ( "unable to write results: %s" , err )
return fmt . Errorf ( "unable to write results: %s" , err )
}
}
if h . noResultsFail {
os . Exit ( 1 )
}
return nil
return nil
}
}
table := uitable . New ( )
table := uitable . New ( )