Skip to content

Commit 7a30469

Browse files
authored
fix: change structure of file and folders (#15)
* fix: change structure of file and folders * fix: reduce code duplication
1 parent 351b925 commit 7a30469

34 files changed

+1354
-1246
lines changed

helpers/editor.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package helpers
2+
3+
import (
4+
"os"
5+
"os/exec"
6+
7+
tea "github.com/charmbracelet/bubbletea"
8+
"github.com/pidanou/helmtui/types"
9+
)
10+
11+
func WriteAndOpenFile(content []byte, file string) tea.Cmd {
12+
err := os.WriteFile(file, content, 0644)
13+
14+
if err != nil {
15+
16+
return func() tea.Msg {
17+
18+
return types.EditorFinishedMsg{Err: err}
19+
20+
}
21+
22+
}
23+
editor := os.Getenv("EDITOR")
24+
if editor == "" {
25+
editor = "vim"
26+
}
27+
c := exec.Command(editor, file)
28+
return tea.ExecProcess(c, func(err error) tea.Msg {
29+
return types.EditorFinishedMsg{Err: err}
30+
})
31+
}

hub/hub.go

-208
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,17 @@
11
package hub
22

33
import (
4-
"encoding/json"
5-
"fmt"
6-
"io"
7-
"net/http"
8-
"os/exec"
9-
104
"github.com/charmbracelet/bubbles/help"
11-
"github.com/charmbracelet/bubbles/key"
125
"github.com/charmbracelet/bubbles/table"
136
"github.com/charmbracelet/bubbles/textinput"
147
"github.com/charmbracelet/bubbles/viewport"
158
tea "github.com/charmbracelet/bubbletea"
169
"github.com/charmbracelet/lipgloss"
1710
"github.com/pidanou/helmtui/components"
18-
"github.com/pidanou/helmtui/helpers"
1911
"github.com/pidanou/helmtui/styles"
2012
"github.com/pidanou/helmtui/types"
2113
)
2214

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-
6415
type HubModel struct {
6516
searchBar textinput.Model
6617
resultTable table.Model
@@ -192,162 +143,3 @@ func (m HubModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
192143
cmds = append(cmds, cmd)
193144
return m, tea.Batch(cmds...)
194145
}
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-
}

hub/hub_commands.go

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package hub
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io"
7+
"net/http"
8+
"os/exec"
9+
10+
"github.com/charmbracelet/bubbles/table"
11+
tea "github.com/charmbracelet/bubbletea"
12+
"github.com/pidanou/helmtui/types"
13+
)
14+
15+
func (m HubModel) searchHub() tea.Msg {
16+
type Package struct {
17+
ID string `json:"package_id"`
18+
NormalizedName string `json:"normalized_name"`
19+
Description string `json:"description"`
20+
Version string `json:"version"`
21+
Repository struct {
22+
Name string `json:"name"`
23+
URL string `json:"url"`
24+
} `json:"repository"`
25+
}
26+
27+
type Response struct {
28+
Packages []Package `json:"packages"`
29+
}
30+
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())
31+
32+
// Create a new HTTP client
33+
client := &http.Client{}
34+
35+
// Create a new GET request
36+
req, err := http.NewRequest("GET", url, nil)
37+
if err != nil {
38+
return types.HubSearchResultMsg{Err: err}
39+
}
40+
41+
// Set the request header
42+
req.Header.Set("Accept", "application/json")
43+
44+
// Perform the request
45+
resp, err := client.Do(req)
46+
if err != nil {
47+
return types.HubSearchResultMsg{Err: err}
48+
}
49+
defer resp.Body.Close()
50+
51+
// Read the response body
52+
body, err := io.ReadAll(resp.Body)
53+
if err != nil {
54+
return types.HubSearchResultMsg{Err: err}
55+
}
56+
var response Response
57+
err = json.Unmarshal(body, &response)
58+
if err != nil {
59+
return types.HubSearchResultMsg{Err: err}
60+
}
61+
62+
var rows []table.Row
63+
for _, pkg := range response.Packages {
64+
row := []string{}
65+
row = append(row, pkg.ID, pkg.Version, pkg.NormalizedName, pkg.Repository.Name, pkg.Repository.URL, pkg.Description)
66+
rows = append(rows, row)
67+
}
68+
69+
return types.HubSearchResultMsg{Content: rows}
70+
}
71+
72+
func (m HubModel) searchDefaultValue() tea.Msg {
73+
if m.resultTable.SelectedRow() == nil {
74+
return nil
75+
}
76+
77+
url := fmt.Sprintf("https://artifacthub.io/api/v1/packages/%s/%s/values", m.resultTable.SelectedRow()[0], m.resultTable.SelectedRow()[1])
78+
79+
// Create a new HTTP client
80+
client := &http.Client{}
81+
82+
// Create a new GET request
83+
req, err := http.NewRequest("GET", url, nil)
84+
if err != nil {
85+
return types.HubSearchDefaultValueMsg{Err: err}
86+
}
87+
88+
// Set the request header
89+
req.Header.Set("Accept", "application/yaml")
90+
91+
// Perform the request
92+
resp, err := client.Do(req)
93+
if err != nil {
94+
return types.HubSearchDefaultValueMsg{Err: err}
95+
}
96+
defer resp.Body.Close()
97+
98+
// Read the response body
99+
body, err := io.ReadAll(resp.Body)
100+
if err != nil {
101+
return types.HubSearchDefaultValueMsg{Err: err}
102+
}
103+
104+
return types.HubSearchDefaultValueMsg{Content: string(body)}
105+
}
106+
107+
func (m HubModel) addRepo() tea.Msg {
108+
if m.repoAddInput.Value() == "" || m.resultTable.SelectedRow() == nil {
109+
return nil
110+
}
111+
cmd := exec.Command("helm", "repo", "add", m.repoAddInput.Value(), m.resultTable.SelectedRow()[4])
112+
err := cmd.Run()
113+
if err != nil {
114+
return types.AddRepoMsg{Err: err}
115+
}
116+
return types.AddRepoMsg{Err: nil}
117+
}

0 commit comments

Comments
 (0)