forked from helm/helm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
257 lines (207 loc) · 6.67 KB
/
search.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
Copyright The Helm Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"io"
"strings"
"github.com/Masterminds/semver"
"github.com/gosuri/uitable"
"github.com/spf13/cobra"
"k8s.io/helm/cmd/helm/search"
"k8s.io/helm/pkg/helm/helmpath"
"k8s.io/helm/pkg/repo"
)
const searchDesc = `
Search reads through all of the repositories configured on the system, and
looks for matches.
It will display the latest stable versions of the charts found. If you
specify the --devel flag, the output will include pre-release versions.
If you want to search using a version constraint, use --version.
Examples:
# Search for stable release versions matching the keyword "nginx"
helm search nginx
# Search for release versions matching the keyword "nginx", including pre-release versions
helm search nginx --devel
# Search for the latest stable release for nginx-ingress with a major version of 1
helm search nginx-ingress --version ^1.0.0
Repositories are managed with 'helm repo' commands.
To look for charts with a particular name (such as stable/mysql), try
searching using vertical tabs (\v). Vertical tabs are used as the delimiter
between search fields. For example:
helm search --regexp '\vstable/mysql\v'
To search for charts using common keywords (such as "database" or
"key-value store"), use
helm search database
or
helm search key-value store
`
// searchMaxScore suggests that any score higher than this is not considered a match.
const searchMaxScore = 25
type searchCmd struct {
out io.Writer
helmhome helmpath.Home
devel bool
versions bool
regexp bool
version string
colWidth uint
output string
}
type chartElement struct {
Name string
Version string
AppVersion string
Description string
}
func newSearchCmd(out io.Writer) *cobra.Command {
sc := &searchCmd{out: out}
cmd := &cobra.Command{
Use: "search [keyword]",
Short: "Search for a keyword in charts",
Long: searchDesc,
RunE: func(cmd *cobra.Command, args []string) error {
sc.helmhome = settings.Home
return sc.run(args)
},
}
f := cmd.Flags()
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.BoolVar(&sc.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.StringVarP(&sc.version, "version", "v", "", "Search using semantic versioning constraints")
f.UintVar(&sc.colWidth, "col-width", 60, "Specifies the max column width of output")
bindOutputFlag(cmd, &sc.output)
return cmd
}
func (s *searchCmd) run(args []string) error {
s.setupSearchedVersion()
index, err := s.buildIndex()
if err != nil {
return err
}
var res []*search.Result
if len(args) == 0 {
res = index.All()
} else {
q := strings.Join(args, " ")
res, err = index.Search(q, searchMaxScore, s.regexp)
if err != nil {
return err
}
}
search.SortScore(res)
data, err := s.applyConstraint(res)
if err != nil {
return err
}
return write(s.out, &searchWriter{data, s.colWidth}, outputFormat(s.output))
}
func (s *searchCmd) setupSearchedVersion() {
debug("Original chart version: %q", s.version)
if s.version != "" {
return
}
if s.devel { // search for releases and prereleases (alpha, beta, and release candidate releases).
debug("setting version to >0.0.0-0")
s.version = ">0.0.0-0"
} else { // search only for stable releases, prerelease versions will be skip
debug("setting version to >0.0.0")
s.version = ">0.0.0"
}
}
func (s *searchCmd) applyConstraint(res []*search.Result) ([]*search.Result, error) {
if len(s.version) == 0 {
return res, nil
}
constraint, err := semver.NewConstraint(s.version)
if err != nil {
return res, fmt.Errorf("an invalid version/constraint format: %s", err)
}
data := res[:0]
foundNames := map[string]bool{}
for _, r := range res {
if _, found := foundNames[r.Name]; found {
continue
}
v, err := semver.NewVersion(r.Chart.Version)
if err != nil || constraint.Check(v) {
data = append(data, r)
if !s.versions {
foundNames[r.Name] = true // If user hasn't requested all versions, only show the latest that matches
}
}
}
return data, nil
}
func (s *searchCmd) buildIndex() (*search.Index, error) {
// Load the repositories.yaml
rf, err := repo.LoadRepositoriesFile(s.helmhome.RepositoryFile())
if err != nil {
return nil, err
}
i := search.NewIndex()
for _, re := range rf.Repositories {
n := re.Name
f := s.helmhome.CacheIndex(n)
ind, err := repo.LoadIndexFile(f)
if err != nil {
fmt.Fprintf(s.out, "WARNING: Repo %q is corrupt or missing. Try 'helm repo update'.\n", n)
continue
}
i.AddRepo(n, ind, s.versions || len(s.version) > 0)
}
return i, nil
}
//////////// Printer implementation below here
type searchWriter struct {
results []*search.Result
columnWidth uint
}
func (r *searchWriter) WriteTable(out io.Writer) error {
if len(r.results) == 0 {
_, err := out.Write([]byte("No results found\n"))
if err != nil {
return fmt.Errorf("unable to write results: %s", err)
}
return nil
}
table := uitable.New()
table.MaxColWidth = r.columnWidth
table.AddRow("NAME", "CHART VERSION", "APP VERSION", "DESCRIPTION")
for _, r := range r.results {
table.AddRow(r.Name, r.Chart.Version, r.Chart.AppVersion, r.Chart.Description)
}
return encodeTable(out, table)
}
func (r *searchWriter) WriteJSON(out io.Writer) error {
return r.encodeByFormat(out, outputJSON)
}
func (r *searchWriter) WriteYAML(out io.Writer) error {
return r.encodeByFormat(out, outputYAML)
}
func (r *searchWriter) encodeByFormat(out io.Writer, format outputFormat) error {
var chartList []chartElement
for _, r := range r.results {
chartList = append(chartList, chartElement{r.Name, r.Chart.Version, r.Chart.AppVersion, r.Chart.Description})
}
switch format {
case outputJSON:
return encodeJSON(out, chartList)
case outputYAML:
return encodeYAML(out, chartList)
}
// Because this is a non-exported function and only called internally by
// WriteJSON and WriteYAML, we shouldn't get invalid types
return nil
}