Merge pull request #2279 from sstarcher/search_constraint

Search constraint and Install/Upgrade default constraint
pull/2294/head
Matt Butcher 8 years ago committed by GitHub
commit fe1104056c

@ -21,6 +21,7 @@ import (
"io" "io"
"strings" "strings"
"github.com/Masterminds/semver"
"github.com/gosuri/uitable" "github.com/gosuri/uitable"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -45,6 +46,7 @@ type searchCmd struct {
versions bool versions bool
regexp bool regexp bool
version string
} }
func newSearchCmd(out io.Writer) *cobra.Command { func newSearchCmd(out io.Writer) *cobra.Command {
@ -62,6 +64,7 @@ func newSearchCmd(out io.Writer) *cobra.Command {
f := cmd.Flags() f := cmd.Flags()
f.BoolVarP(&sc.regexp, "regexp", "r", false, "use regular expressions for searching") f.BoolVarP(&sc.regexp, "regexp", "r", false, "use regular expressions for searching")
f.BoolVarP(&sc.versions, "versions", "l", false, "show the long listing, with each version of each chart on its own line") f.BoolVarP(&sc.versions, "versions", "l", false, "show the long listing, with each version of each chart on its own line")
f.StringVarP(&sc.version, "version", "v", "", "search using semantic versioning constraints")
return cmd return cmd
} }
@ -72,27 +75,47 @@ func (s *searchCmd) run(args []string) error {
return err return err
} }
var res []*search.Result
if len(args) == 0 { if len(args) == 0 {
s.showAllCharts(index) res = index.All()
return nil } else {
q := strings.Join(args, " ")
res, err = index.Search(q, searchMaxScore, s.regexp)
if err != nil {
return nil
}
} }
q := strings.Join(args, " ") search.SortScore(res)
res, err := index.Search(q, searchMaxScore, s.regexp) data, err := s.applyConstraint(res)
if err != nil { if err != nil {
return nil return err
} }
search.SortScore(res)
fmt.Fprintln(s.out, s.formatSearchResults(res)) fmt.Fprintln(s.out, s.formatSearchResults(data))
return nil return nil
} }
func (s *searchCmd) showAllCharts(i *search.Index) { func (s *searchCmd) applyConstraint(res []*search.Result) ([]*search.Result, error) {
res := i.All() if len(s.version) == 0 {
search.SortScore(res) return res, nil
fmt.Fprintln(s.out, s.formatSearchResults(res)) }
constraint, err := semver.NewConstraint(s.version)
if err != nil {
return res, fmt.Errorf("an invalid version/constraint format: %s", err)
}
data := res[:0]
for _, r := range res {
v, err := semver.NewVersion(r.Chart.Version)
if err != nil || constraint.Check(v) {
data = append(data, r)
}
}
return data, nil
} }
func (s *searchCmd) formatSearchResults(res []*search.Result) string { func (s *searchCmd) formatSearchResults(res []*search.Result) string {

@ -19,8 +19,9 @@ helm search [keyword]
### Options ### Options
``` ```
-r, --regexp use regular expressions for searching -r, --regexp use regular expressions for searching
-l, --versions show the long listing, with each version of each chart on its own line -v, --version string search using semantic versioning constraints
-l, --versions show the long listing, with each version of each chart on its own line
``` ```
### Options inherited from parent commands ### Options inherited from parent commands
@ -36,4 +37,4 @@ helm search [keyword]
### SEE ALSO ### SEE ALSO
* [helm](helm.md) - The Helm package manager for Kubernetes. * [helm](helm.md) - The Helm package manager for Kubernetes.
###### Auto generated by spf13/cobra on 16-Apr-2017 ###### Auto generated by spf13/cobra on 18-Apr-2017

@ -27,6 +27,10 @@ Repositories are managed with 'helm repo' commands.
\fB\-r\fP, \fB\-\-regexp\fP[=false] \fB\-r\fP, \fB\-\-regexp\fP[=false]
use regular expressions for searching use regular expressions for searching
.PP
\fB\-v\fP, \fB\-\-version\fP=""
search using semantic versioning constraints
.PP .PP
\fB\-l\fP, \fB\-\-versions\fP[=false] \fB\-l\fP, \fB\-\-versions\fP[=false]
show the long listing, with each version of each chart on its own line show the long listing, with each version of each chart on its own line
@ -61,4 +65,4 @@ Repositories are managed with 'helm repo' commands.
.SH HISTORY .SH HISTORY
.PP .PP
16\-Apr\-2017 Auto generated by spf13/cobra 18\-Apr\-2017 Auto generated by spf13/cobra

@ -155,12 +155,25 @@ func (i IndexFile) Get(name, version string) (*ChartVersion, error) {
if len(vs) == 0 { if len(vs) == 0 {
return nil, ErrNoChartVersion return nil, ErrNoChartVersion
} }
var constraint *semver.Constraints
if len(version) == 0 { if len(version) == 0 {
return vs[0], nil constraint, _ = semver.NewConstraint("*")
} else {
var err error
constraint, err = semver.NewConstraint(version)
if err != nil {
return nil, err
}
} }
for _, ver := range vs { for _, ver := range vs {
// TODO: Do we need to normalize the version field with the SemVer lib? test, err := semver.NewVersion(ver.Version)
if ver.Version == version { if err != nil {
continue
}
if constraint.Check(test) {
return ver, nil return ver, nil
} }
} }

@ -1265,6 +1265,9 @@ _helm_search()
flags+=("--regexp") flags+=("--regexp")
flags+=("-r") flags+=("-r")
local_nonpersistent_flags+=("--regexp") local_nonpersistent_flags+=("--regexp")
flags+=("--version=")
two_word_flags+=("-v")
local_nonpersistent_flags+=("--version=")
flags+=("--versions") flags+=("--versions")
flags+=("-l") flags+=("-l")
local_nonpersistent_flags+=("--versions") local_nonpersistent_flags+=("--versions")

Loading…
Cancel
Save