-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.py
179 lines (142 loc) · 4.51 KB
/
search.py
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
#!/usr/bin/env python3
# encoding: utf-8
#
# Copyright (c) 2022 Bumsoo Kim <[email protected]>
#
# MIT Licence http://opensource.org/licenses/MIT
from __future__ import annotations
import sys
from workflow import ICON_INFO
from api import ArtifactHubClient, ChartCenterClient, HubClient
from migration import migrate
from utils import (
create_workflow,
is_artifacthub_enabled,
is_chartcenter_enabled,
is_hub_enabled,
)
def add_default_item(wf):
if is_artifacthub_enabled(wf):
wf.add_item(
title='Go to ArtifactHub',
subtitle=ArtifactHubClient.BASE_URL,
arg=ArtifactHubClient.BASE_URL,
valid=True,
icon=wf.workflowfile(ArtifactHubClient.ICON_PATH),
)
if is_chartcenter_enabled(wf):
wf.add_item(
title='Go to ChartCenter',
subtitle=ChartCenterClient.BASE_URL,
arg=ChartCenterClient.BASE_URL,
valid=True,
icon=wf.workflowfile(ChartCenterClient.ICON_PATH),
)
if is_hub_enabled(wf):
wf.add_item(
title='Go to Helm Hub',
subtitle=HubClient.HUB_BASE_URL,
arg=HubClient.HUB_BASE_URL,
valid=True,
icon=wf.workflowfile(HubClient.ICON_PATH),
)
def generate_search_key(chart):
wf.logger.info(chart)
elements = [chart.chart_id]
return ' '.join(elements)
def main(wf):
log = wf.logger
if len(wf.args):
query = wf.args[0]
else:
query = None
log.info('user query: "%s"', query)
# do workflow migration
migrate(wf)
if wf.update_available:
wf.add_item(
'New version is available',
subtitle='Click to install the update',
autocomplete='workflow:update',
icon=ICON_INFO,
)
if not query:
add_default_item(wf)
wf.send_feedback()
return 0
charts = []
if is_artifacthub_enabled(wf):
def search():
api = ArtifactHubClient()
return api.get_charts(query)
charts.extend(
wf.cached_data(
ArtifactHubClient.get_cache_key(query), search, max_age=30
)
)
if is_chartcenter_enabled(wf):
def search():
api = ChartCenterClient()
return api.get_charts(query)
charts.extend(
wf.cached_data(
ChartCenterClient.get_cache_key(query), search, max_age=30
)
)
if is_hub_enabled(wf):
def search():
api = HubClient()
q = query
if '/' in q:
q = q.split('/')[-1]
return api.get_charts(q)
charts.extend(
wf.cached_data(HubClient.get_cache_key(query), search, max_age=30)
)
log.info('%d charts found', len(charts))
if query:
# filter results that query only appears in the chart id
charts = wf.filter(query, charts, key=generate_search_key)
for chart in charts:
wf.add_item(
title='{0} ({1})'.format(chart.chart_id, chart.version),
subtitle=chart.description,
arg=chart.web_url,
valid=True,
icon=wf.workflowfile(chart.icon),
)
if not charts:
if '/' in query:
chart_name = query.split('/')[-1]
wf.add_item(
title='Do you want to try with "{0}"?'.format(chart_name),
subtitle='Try to search without a repository name',
autocomplete=chart_name,
)
if is_artifacthub_enabled(wf):
wf.add_item(
'No charts found for "{0}"'.format(query),
subtitle='Click to search in ArtifactHub',
arg=ArtifactHubClient.BASE_URL,
valid=True,
)
if is_chartcenter_enabled(wf):
wf.add_item(
'No charts found for "{0}"'.format(query),
subtitle='Click to search in ChartCenter',
arg=ChartCenterClient.BASE_URL,
valid=True,
)
if is_hub_enabled(wf):
wf.add_item(
'No charts found for "{0}"'.format(query),
subtitle='Click to see the results in Helm Hub',
arg='{0}/charts?q={1}'.format(HubClient.HUB_BASE_URL, query),
valid=True,
)
else:
add_default_item(wf)
wf.send_feedback()
if __name__ == '__main__':
wf = create_workflow()
sys.exit(wf.run(main))