...
1
16
17 package monocular
18
19 import (
20 "encoding/json"
21 "fmt"
22 "net/http"
23 "net/url"
24 "path"
25 "time"
26
27 "helm.sh/helm/v3/internal/version"
28 "helm.sh/helm/v3/pkg/chart"
29 )
30
31
32 const SearchPath = "api/chartsvc/v1/charts/search"
33
34
35
36
37
38
39
40
41 type SearchResult struct {
42 ID string `json:"id"`
43 ArtifactHub ArtifactHub `json:"artifactHub"`
44 Type string `json:"type"`
45 Attributes Chart `json:"attributes"`
46 Links Links `json:"links"`
47 Relationships Relationships `json:"relationships"`
48 }
49
50
51 type ArtifactHub struct {
52 PackageURL string `json:"packageUrl"`
53 }
54
55
56 type Chart struct {
57 Name string `json:"name"`
58 Repo Repo `json:"repo"`
59 Description string `json:"description"`
60 Home string `json:"home"`
61 Keywords []string `json:"keywords"`
62 Maintainers []chart.Maintainer `json:"maintainers"`
63 Sources []string `json:"sources"`
64 Icon string `json:"icon"`
65 }
66
67
68 type Repo struct {
69 Name string `json:"name"`
70 URL string `json:"url"`
71 }
72
73
74 type Links struct {
75 Self string `json:"self"`
76 }
77
78
79 type Relationships struct {
80 LatestChartVersion LatestChartVersion `json:"latestChartVersion"`
81 }
82
83
84 type LatestChartVersion struct {
85 Data ChartVersion `json:"data"`
86 Links Links `json:"links"`
87 }
88
89
90 type ChartVersion struct {
91 Version string `json:"version"`
92 AppVersion string `json:"app_version"`
93 Created time.Time `json:"created"`
94 Digest string `json:"digest"`
95 Urls []string `json:"urls"`
96 Readme string `json:"readme"`
97 Values string `json:"values"`
98 }
99
100
101 func (c *Client) Search(term string) ([]SearchResult, error) {
102
103
104
105
106 p, err := url.Parse(c.BaseURL)
107 if err != nil {
108 return nil, err
109 }
110
111
112 p.Path = path.Join(p.Path, SearchPath)
113
114 p.RawQuery = "q=" + url.QueryEscape(term)
115
116
117 req, err := http.NewRequest(http.MethodGet, p.String(), nil)
118 if err != nil {
119 return nil, err
120 }
121
122
123
124 req.Header.Set("User-Agent", version.GetUserAgent())
125
126 res, err := http.DefaultClient.Do(req)
127 if err != nil {
128 return nil, err
129 }
130 defer res.Body.Close()
131
132 if res.StatusCode != 200 {
133 return nil, fmt.Errorf("failed to fetch %s : %s", p.String(), res.Status)
134 }
135
136 result := &searchResponse{}
137
138 json.NewDecoder(res.Body).Decode(result)
139
140 return result.Data, nil
141 }
142
143 type searchResponse struct {
144 Data []SearchResult `json:"data"`
145 }
146
View as plain text