|
1 | 1 | package hub
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "encoding/json" |
5 |
| - "fmt" |
6 |
| - "io" |
7 |
| - "net/http" |
8 |
| - "os/exec" |
9 |
| - |
10 | 4 | "github.com/charmbracelet/bubbles/help"
|
11 |
| - "github.com/charmbracelet/bubbles/key" |
12 | 5 | "github.com/charmbracelet/bubbles/table"
|
13 | 6 | "github.com/charmbracelet/bubbles/textinput"
|
14 | 7 | "github.com/charmbracelet/bubbles/viewport"
|
15 | 8 | tea "github.com/charmbracelet/bubbletea"
|
16 | 9 | "github.com/charmbracelet/lipgloss"
|
17 | 10 | "github.com/pidanou/helmtui/components"
|
18 |
| - "github.com/pidanou/helmtui/helpers" |
19 | 11 | "github.com/pidanou/helmtui/styles"
|
20 | 12 | "github.com/pidanou/helmtui/types"
|
21 | 13 | )
|
22 | 14 |
|
23 |
| -type keyMap struct { |
24 |
| - AddRepo key.Binding |
25 |
| - Search key.Binding |
26 |
| - Show key.Binding |
27 |
| - Cancel key.Binding |
28 |
| -} |
29 |
| - |
30 |
| -func (k keyMap) ShortHelp() []key.Binding { |
31 |
| - return []key.Binding{k.AddRepo, k.Show, k.Search, k.Cancel} |
32 |
| -} |
33 |
| - |
34 |
| -// FullHelp returns keybindings for the expanded help view. It's part of the |
35 |
| -// key.Map interface. |
36 |
| -func (k keyMap) FullHelp() [][]key.Binding { |
37 |
| - return [][]key.Binding{} |
38 |
| -} |
39 |
| - |
40 |
| -var defaultKeysHelp = keyMap{ |
41 |
| - Search: key.NewBinding(key.WithKeys("/"), key.WithHelp("/", "Search")), |
42 |
| - Show: key.NewBinding(key.WithKeys("enter"), key.WithHelp("enter", "Focus table")), |
43 |
| -} |
44 |
| - |
45 |
| -var tableKeysHelp = keyMap{ |
46 |
| - Show: key.NewBinding(key.WithKeys("v"), key.WithHelp("v", "Show default values")), |
47 |
| - Search: key.NewBinding(key.WithKeys("/"), key.WithHelp("/", "Search")), |
48 |
| - AddRepo: key.NewBinding(key.WithKeys("a"), key.WithHelp("a", "Add repo")), |
49 |
| -} |
50 |
| - |
51 |
| -var searchKeyHelp = keyMap{ |
52 |
| - Search: key.NewBinding(key.WithKeys("enter"), key.WithHelp("enter", "Search")), |
53 |
| -} |
54 |
| - |
55 |
| -var addRepoKeyHelp = keyMap{ |
56 |
| - Search: key.NewBinding(key.WithKeys("enter"), key.WithHelp("enter", "Search")), |
57 |
| -} |
58 |
| - |
59 |
| -var defaultValuesKeyHelp = keyMap{ |
60 |
| - Search: key.NewBinding(key.WithKeys("/"), key.WithHelp("/", "Search")), |
61 |
| - Cancel: key.NewBinding(key.WithKeys("esc"), key.WithHelp("esc", "Cancel")), |
62 |
| -} |
63 |
| - |
64 | 15 | type HubModel struct {
|
65 | 16 | searchBar textinput.Model
|
66 | 17 | resultTable table.Model
|
@@ -192,162 +143,3 @@ func (m HubModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
192 | 143 | cmds = append(cmds, cmd)
|
193 | 144 | return m, tea.Batch(cmds...)
|
194 | 145 | }
|
195 |
| - |
196 |
| -func (m HubModel) View() string { |
197 |
| - header := styles.InactiveStyle.Border(styles.Border).Render(m.searchBar.View()) |
198 |
| - remainingHeight := m.height - lipgloss.Height(header) - 2 - 1 // searchbar padding + releaseTable padding + helper |
199 |
| - if m.repoAddInput.Focused() { |
200 |
| - remainingHeight -= 3 |
201 |
| - } |
202 |
| - if m.view == defaultValueView { |
203 |
| - m.defaultValueVP.Height = m.height - 2 - 1 |
204 |
| - return m.renderDefaultValueView() |
205 |
| - } |
206 |
| - m.resultTable.SetHeight(remainingHeight) |
207 |
| - if m.searchBar.Focused() { |
208 |
| - header = styles.ActiveStyle.Border(styles.Border).Render(m.searchBar.View()) |
209 |
| - } |
210 |
| - helperStyle := m.help.Styles.ShortSeparator |
211 |
| - helpView := m.help.View(defaultKeysHelp) |
212 |
| - if m.searchBar.Focused() { |
213 |
| - helpView = m.help.View(searchKeyHelp) |
214 |
| - } |
215 |
| - if m.resultTable.Focused() { |
216 |
| - helpView = m.help.View(tableKeysHelp) |
217 |
| - } |
218 |
| - if m.repoAddInput.Focused() { |
219 |
| - helpView = m.help.View(addRepoKeyHelp) |
220 |
| - } |
221 |
| - helpView += helperStyle.Render(" • ") + m.help.View(helpers.CommonKeys) |
222 |
| - style := styles.ActiveStyle.Border(styles.Border) |
223 |
| - if m.repoAddInput.Focused() { |
224 |
| - return header + "\n" + m.renderSearchTableView() + "\n" + style.Render(m.repoAddInput.View()) + "\n" + helpView |
225 |
| - } |
226 |
| - return header + "\n" + m.renderSearchTableView() + "\n" + helpView |
227 |
| -} |
228 |
| - |
229 |
| -func (m HubModel) renderSearchTableView() string { |
230 |
| - var releasesTopBorder string |
231 |
| - tableView := m.resultTable.View() |
232 |
| - var baseStyle lipgloss.Style |
233 |
| - releasesTopBorder = styles.GenerateTopBorderWithTitle(" Results ", m.resultTable.Width(), styles.Border, styles.InactiveStyle) |
234 |
| - baseStyle = styles.InactiveStyle.Border(styles.Border, false, true, true) |
235 |
| - if m.resultTable.Focused() { |
236 |
| - releasesTopBorder = styles.GenerateTopBorderWithTitle(" Results ", m.resultTable.Width(), styles.Border, styles.ActiveStyle.Foreground(styles.HighlightColor)) |
237 |
| - baseStyle = styles.ActiveStyle.Border(styles.Border, false, true, true) |
238 |
| - } |
239 |
| - tableView = baseStyle.Render(tableView) |
240 |
| - return lipgloss.JoinVertical(lipgloss.Top, releasesTopBorder, tableView) |
241 |
| -} |
242 |
| - |
243 |
| -func (m HubModel) renderDefaultValueView() string { |
244 |
| - defaultValueTopBorder := styles.GenerateTopBorderWithTitle(" Default Values ", m.defaultValueVP.Width, styles.Border, styles.InactiveStyle) |
245 |
| - baseStyle := styles.InactiveStyle.Border(styles.Border, false, true, true) |
246 |
| - helperStyle := m.help.Styles.ShortSeparator |
247 |
| - helpView := helperStyle.Render(" • ") + m.help.View(helpers.CommonKeys) |
248 |
| - return lipgloss.JoinVertical(lipgloss.Top, defaultValueTopBorder, baseStyle.Render(m.defaultValueVP.View()), m.help.View(defaultValuesKeyHelp)+helpView) |
249 |
| -} |
250 |
| - |
251 |
| -func (m HubModel) searchHub() tea.Msg { |
252 |
| - type Package struct { |
253 |
| - ID string `json:"package_id"` |
254 |
| - NormalizedName string `json:"normalized_name"` |
255 |
| - Description string `json:"description"` |
256 |
| - Version string `json:"version"` |
257 |
| - Repository struct { |
258 |
| - Name string `json:"name"` |
259 |
| - URL string `json:"url"` |
260 |
| - } `json:"repository"` |
261 |
| - } |
262 |
| - |
263 |
| - type Response struct { |
264 |
| - Packages []Package `json:"packages"` |
265 |
| - } |
266 |
| - url := fmt.Sprintf("https://artifacthub.io/api/v1/packages/search?offset=0&limit=20&facets=false&ts_query_web=%s&kind=0&deprecated=false&sort=relevance", m.searchBar.Value()) |
267 |
| - |
268 |
| - // Create a new HTTP client |
269 |
| - client := &http.Client{} |
270 |
| - |
271 |
| - // Create a new GET request |
272 |
| - req, err := http.NewRequest("GET", url, nil) |
273 |
| - if err != nil { |
274 |
| - return types.HubSearchResultMsg{Err: err} |
275 |
| - } |
276 |
| - |
277 |
| - // Set the request header |
278 |
| - req.Header.Set("Accept", "application/json") |
279 |
| - |
280 |
| - // Perform the request |
281 |
| - resp, err := client.Do(req) |
282 |
| - if err != nil { |
283 |
| - return types.HubSearchResultMsg{Err: err} |
284 |
| - } |
285 |
| - defer resp.Body.Close() |
286 |
| - |
287 |
| - // Read the response body |
288 |
| - body, err := io.ReadAll(resp.Body) |
289 |
| - if err != nil { |
290 |
| - return types.HubSearchResultMsg{Err: err} |
291 |
| - } |
292 |
| - var response Response |
293 |
| - err = json.Unmarshal(body, &response) |
294 |
| - if err != nil { |
295 |
| - return types.HubSearchResultMsg{Err: err} |
296 |
| - } |
297 |
| - |
298 |
| - var rows []table.Row |
299 |
| - for _, pkg := range response.Packages { |
300 |
| - row := []string{} |
301 |
| - row = append(row, pkg.ID, pkg.Version, pkg.NormalizedName, pkg.Repository.Name, pkg.Repository.URL, pkg.Description) |
302 |
| - rows = append(rows, row) |
303 |
| - } |
304 |
| - |
305 |
| - return types.HubSearchResultMsg{Content: rows} |
306 |
| -} |
307 |
| - |
308 |
| -func (m HubModel) searchDefaultValue() tea.Msg { |
309 |
| - if m.resultTable.SelectedRow() == nil { |
310 |
| - return nil |
311 |
| - } |
312 |
| - |
313 |
| - url := fmt.Sprintf("https://artifacthub.io/api/v1/packages/%s/%s/values", m.resultTable.SelectedRow()[0], m.resultTable.SelectedRow()[1]) |
314 |
| - |
315 |
| - // Create a new HTTP client |
316 |
| - client := &http.Client{} |
317 |
| - |
318 |
| - // Create a new GET request |
319 |
| - req, err := http.NewRequest("GET", url, nil) |
320 |
| - if err != nil { |
321 |
| - return types.HubSearchDefaultValueMsg{Err: err} |
322 |
| - } |
323 |
| - |
324 |
| - // Set the request header |
325 |
| - req.Header.Set("Accept", "application/yaml") |
326 |
| - |
327 |
| - // Perform the request |
328 |
| - resp, err := client.Do(req) |
329 |
| - if err != nil { |
330 |
| - return types.HubSearchDefaultValueMsg{Err: err} |
331 |
| - } |
332 |
| - defer resp.Body.Close() |
333 |
| - |
334 |
| - // Read the response body |
335 |
| - body, err := io.ReadAll(resp.Body) |
336 |
| - if err != nil { |
337 |
| - return types.HubSearchDefaultValueMsg{Err: err} |
338 |
| - } |
339 |
| - |
340 |
| - return types.HubSearchDefaultValueMsg{Content: string(body)} |
341 |
| -} |
342 |
| - |
343 |
| -func (m HubModel) addRepo() tea.Msg { |
344 |
| - if m.repoAddInput.Value() == "" || m.resultTable.SelectedRow() == nil { |
345 |
| - return nil |
346 |
| - } |
347 |
| - cmd := exec.Command("helm", "repo", "add", m.repoAddInput.Value(), m.resultTable.SelectedRow()[4]) |
348 |
| - err := cmd.Run() |
349 |
| - if err != nil { |
350 |
| - return types.AddRepoMsg{Err: err} |
351 |
| - } |
352 |
| - return types.AddRepoMsg{Err: nil} |
353 |
| -} |
0 commit comments