-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add plugins management * fix: remove code duplication * fix: empty input field after install plugin
- Loading branch information
Showing
18 changed files
with
329 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package plugins | ||
|
||
import ( | ||
"github.com/charmbracelet/bubbles/help" | ||
"github.com/charmbracelet/bubbles/key" | ||
"github.com/charmbracelet/bubbles/table" | ||
"github.com/charmbracelet/bubbles/textinput" | ||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/pidanou/helmtui/components" | ||
"github.com/pidanou/helmtui/types" | ||
) | ||
|
||
var pluginsCols = []components.ColumnDefinition{ | ||
{Title: "Name", FlexFactor: 1}, | ||
{Title: "Version", FlexFactor: 1}, | ||
{Title: "description", FlexFactor: 3}, | ||
} | ||
|
||
type PluginsModel struct { | ||
pluginsTable table.Model | ||
installPluginInput textinput.Model | ||
help help.Model | ||
keys keyMap | ||
width int | ||
height int | ||
} | ||
|
||
func InitModel() PluginsModel { | ||
table := components.GenerateTable() | ||
input := textinput.New() | ||
input.Placeholder = "Enter plugin path/url" | ||
return PluginsModel{pluginsTable: table, help: help.New(), keys: overviewKeys, installPluginInput: input} | ||
} | ||
|
||
func (m PluginsModel) Init() tea.Cmd { | ||
return m.list | ||
} | ||
|
||
func (m PluginsModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
var cmd tea.Cmd | ||
var cmds []tea.Cmd | ||
|
||
switch msg := msg.(type) { | ||
case tea.WindowSizeMsg: | ||
m.width = msg.Width | ||
m.height = msg.Height | ||
components.SetTable(&m.pluginsTable, pluginsCols, m.width) | ||
case types.PluginsListMsg: | ||
m.pluginsTable.SetRows(msg.Content) | ||
case types.PluginInstallMsg: | ||
m.installPluginInput.Blur() | ||
m.installPluginInput.SetValue("") | ||
return m, m.list | ||
case types.PluginUninstallMsg: | ||
return m, m.list | ||
case tea.KeyMsg: | ||
switch { | ||
case key.Matches(msg, m.keys.Install): | ||
cmds = append(cmds, m.installPluginInput.Focus()) | ||
return m, tea.Batch(cmds...) | ||
case key.Matches(msg, m.keys.Uninstall): | ||
if !m.installPluginInput.Focused() { | ||
return m, m.uninstall | ||
} | ||
case key.Matches(msg, m.keys.Update): | ||
if !m.installPluginInput.Focused() { | ||
return m, m.update | ||
} | ||
case key.Matches(msg, m.keys.Cancel): | ||
m.installPluginInput.Blur() | ||
return m, tea.Batch(cmds...) | ||
case key.Matches(msg, m.keys.Refresh): | ||
return m, m.list | ||
case msg.String() == "enter": | ||
if m.installPluginInput.Focused() { | ||
return m, m.install | ||
} | ||
} | ||
} | ||
m.installPluginInput, cmd = m.installPluginInput.Update(msg) | ||
cmds = append(cmds, cmd) | ||
return m, tea.Batch(cmds...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package plugins | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/charmbracelet/bubbles/table" | ||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/pidanou/helmtui/types" | ||
) | ||
|
||
func (m PluginsModel) list() tea.Msg { | ||
var stdout bytes.Buffer | ||
var rows = []table.Row{} | ||
|
||
// Create the command | ||
cmd := exec.Command("helm", "plugin", "ls") | ||
cmd.Stdout = &stdout | ||
err := cmd.Run() | ||
if err != nil { | ||
return types.ListReleasesMsg{Err: err} | ||
} | ||
|
||
lines := strings.Split(stdout.String(), "\n") | ||
lines = lines[1 : len(lines)-1] | ||
|
||
for _, line := range lines { | ||
fields := strings.Fields(line) | ||
name := fields[0] | ||
version := fields[1] | ||
description := strings.Join(fields[2:], " ") | ||
row := []string{name, version, description} | ||
rows = append(rows, row) | ||
} | ||
return types.PluginsListMsg{Content: rows} | ||
} | ||
|
||
func (m PluginsModel) install() tea.Msg { | ||
pluginName := m.installPluginInput.Value() | ||
if pluginName == "" { | ||
return types.PluginInstallMsg{Err: errors.New("No plugin")} | ||
} | ||
cmd := exec.Command("helm", "plugin", "install", strings.TrimSpace(pluginName)) | ||
err := cmd.Run() | ||
if err != nil { | ||
return types.PluginInstallMsg{Err: errors.New("Cannot install plugin")} | ||
} | ||
return types.PluginInstallMsg{Err: nil} | ||
} | ||
|
||
func (m PluginsModel) update() tea.Msg { | ||
if m.pluginsTable.SelectedRow() == nil { | ||
return types.PluginUpdateMsg{Err: errors.New("No plugin selected")} | ||
} | ||
pluginName := m.pluginsTable.SelectedRow()[0] | ||
cmd := exec.Command("helm", "plugin", "update", pluginName) | ||
err := cmd.Run() | ||
|
||
if err != nil { | ||
return types.PluginUpdateMsg{Err: errors.New("Cannot update plugin")} | ||
} | ||
return types.PluginUpdateMsg{Err: nil} | ||
} | ||
|
||
func (m PluginsModel) uninstall() tea.Msg { | ||
if m.pluginsTable.SelectedRow() == nil { | ||
return types.PluginUninstallMsg{Err: errors.New("No plugin selected")} | ||
} | ||
pluginName := m.pluginsTable.SelectedRow()[0] | ||
cmd := exec.Command("helm", "plugin", "uninstall", strings.TrimSpace(pluginName)) | ||
err := cmd.Run() | ||
if err != nil { | ||
return types.PluginUninstallMsg{Err: errors.New("Cannot update plugin")} | ||
} | ||
return types.PluginUninstallMsg{Err: nil} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package plugins | ||
|
||
import "github.com/charmbracelet/bubbles/key" | ||
|
||
type keyMap struct { | ||
Install key.Binding | ||
Update key.Binding | ||
Uninstall key.Binding | ||
Cancel key.Binding | ||
Refresh key.Binding | ||
} | ||
|
||
var overviewKeys = keyMap{ | ||
Uninstall: key.NewBinding(key.WithKeys("U"), key.WithHelp("U", "Uninstall")), | ||
Install: key.NewBinding(key.WithKeys("i"), key.WithHelp("i", "Install")), | ||
Update: key.NewBinding(key.WithKeys("u"), key.WithHelp("u", "Update")), | ||
Cancel: key.NewBinding(key.WithKeys("esc"), key.WithHelp("esc", "Cancel")), | ||
Refresh: key.NewBinding(key.WithKeys("r"), key.WithHelp("r", "Refresh")), | ||
} | ||
|
||
func (k keyMap) ShortHelp() []key.Binding { | ||
return []key.Binding{k.Update, k.Install, k.Uninstall, k.Refresh, k.Cancel} | ||
} | ||
|
||
// FullHelp returns keybindings for the expanded help view. It's part of the | ||
// key.Map interface. | ||
func (k keyMap) FullHelp() [][]key.Binding { | ||
return [][]key.Binding{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package plugins |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package plugins | ||
|
||
import ( | ||
"github.com/charmbracelet/lipgloss" | ||
"github.com/pidanou/helmtui/components" | ||
"github.com/pidanou/helmtui/helpers" | ||
"github.com/pidanou/helmtui/styles" | ||
) | ||
|
||
func (m PluginsModel) View() string { | ||
var remainingHeight = m.height | ||
if m.installPluginInput.Focused() { | ||
remainingHeight -= 3 | ||
} | ||
helperStyle := m.help.Styles.ShortSeparator | ||
helpView := m.help.View(m.keys) + helperStyle.Render(" • ") + m.help.View(helpers.CommonKeys) | ||
view := components.RenderTable(m.pluginsTable, remainingHeight-3, m.width-2) | ||
m.installPluginInput.Width = m.width - 5 | ||
if m.installPluginInput.Focused() { | ||
view += "\n" + styles.ActiveStyle.Border(styles.Border).Render(m.installPluginInput.View()) | ||
} | ||
view = lipgloss.JoinVertical(lipgloss.Left, view, helpView) | ||
return view | ||
} |
Oops, something went wrong.