-
Notifications
You must be signed in to change notification settings - Fork 3
/
search.go
100 lines (95 loc) · 3.15 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
/*
* Copyright 2024 Marc Nuri
*
* 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 helm
import (
"bytes"
"fmt"
"github.com/Masterminds/semver/v3"
"github.com/pkg/errors"
"helm.sh/helm/v3/cmd/helm/search"
"helm.sh/helm/v3/pkg/helmpath"
"helm.sh/helm/v3/pkg/repo"
"net/url"
"path/filepath"
"strconv"
"strings"
)
type SearchOptions struct {
RepositoryConfig string
Keyword string
Regexp bool
Devel bool
Version string
}
// https://github.com/helm/helm/blob/0e72b64797da47c33537d0a8519c9c2e9e6c9362/cmd/helm/search_repo.go#L63
// Implementation logic is in cmd package
// searchMaxScore suggests that any score higher than this is not considered a match.
const searchMaxScore = 25
func SearchRepo(options *SearchOptions) (string, error) {
// https://github.com/helm/helm/blob/0e72b64797da47c33537d0a8519c9c2e9e6c9362/cmd/helm/search_repo.go#L104
// Implementation logic is in cmd package
// Setup version
if options.Version == "" {
if options.Devel {
options.Version = ">0.0.0-0"
} else {
options.Version = ">0.0.0"
}
}
repositoriesYaml, err := repo.LoadFile(repositoryConfig(&RepoOptions{
RepositoryConfig: options.RepositoryConfig,
}))
if err != nil {
return "", err
}
searchIndex := search.NewIndex()
for _, repository := range repositoriesYaml.Repositories {
name := repository.Name
indexPath := filepath.Join(helmpath.CachePath("repository"), helmpath.CacheIndexFile(name))
index, err := repo.LoadIndexFile(indexPath)
if err != nil {
// TODO: see how to propagate warnings to the Java implementation
continue
}
searchIndex.AddRepo(name, index, true)
}
var searchResults []*search.Result
if len(options.Keyword) == 0 {
searchResults = searchIndex.All()
} else {
searchResults, err = searchIndex.Search(options.Keyword, searchMaxScore, options.Regexp)
}
search.SortScore(searchResults)
constraint, err := semver.NewConstraint(options.Version)
if err != nil {
return "", errors.Wrap(err, "an invalid version/constraint format")
}
out := bytes.NewBuffer(make([]byte, 0))
for _, searchResult := range searchResults {
v, err := semver.NewVersion(searchResult.Chart.Version)
if err == nil && constraint.Check(v) {
values := make(url.Values)
values.Set("name", searchResult.Name)
values.Set("score", strconv.Itoa(searchResult.Score))
values.Set("chartVersion", searchResult.Chart.Version)
values.Set("appVersion", searchResult.Chart.AppVersion)
values.Set("description", searchResult.Chart.Description)
values.Set("keywords", strings.Join(searchResult.Chart.Metadata.Keywords, ","))
_, _ = fmt.Fprintln(out, values.Encode())
}
}
return out.String(), nil
}