43858dda87
- KeyListTab: view/add/edit/delete SSH keys (Ctrl+K from host list) - KeyFormTab: add/edit key form with name, type, private key, passphrase - SnippetListTab: view/add/edit/delete snippets (Ctrl+P from host list) - SnippetFormTab: add/edit snippet form with name, command, description, tags - Updated status bar with new hints - Save/delete commands for both key pairs and snippets
193 lines
3.8 KiB
Go
193 lines
3.8 KiB
Go
package tui
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
"git.tukangketik.id/swanadiva/hostkeeper/internal/models"
|
|
"git.tukangketik.id/swanadiva/hostkeeper/pkg/storage"
|
|
)
|
|
|
|
type keyListState int
|
|
|
|
const (
|
|
keyListLoading keyListState = iota
|
|
keyListReady
|
|
keyListError
|
|
)
|
|
|
|
// KeyListTab displays and manages SSH key pairs
|
|
type KeyListTab struct {
|
|
dataDir string
|
|
keys []*models.KeyPair
|
|
selected int
|
|
state keyListState
|
|
err error
|
|
width int
|
|
height int
|
|
mu sync.Mutex
|
|
}
|
|
|
|
func NewKeyListTab(dataDir string) *KeyListTab {
|
|
return &KeyListTab{
|
|
dataDir: dataDir,
|
|
state: keyListLoading,
|
|
}
|
|
}
|
|
|
|
func (t *KeyListTab) Name() string { return "SSH Keys" }
|
|
|
|
func (t *KeyListTab) Init() tea.Cmd {
|
|
return func() tea.Msg {
|
|
store, err := storage.NewJSONStorage(t.dataDir)
|
|
if err != nil {
|
|
return keyListLoadedMsg{err: err}
|
|
}
|
|
keys, err := store.ListKeyPairs(context.Background())
|
|
if err != nil {
|
|
return keyListLoadedMsg{err: err}
|
|
}
|
|
return keyListLoadedMsg{keys: keys}
|
|
}
|
|
}
|
|
|
|
func (t *KeyListTab) Update(msg tea.Msg) (Tab, tea.Cmd) {
|
|
switch msg := msg.(type) {
|
|
case tea.WindowSizeMsg:
|
|
t.mu.Lock()
|
|
t.width = msg.Width
|
|
t.height = msg.Height
|
|
t.mu.Unlock()
|
|
|
|
case keyListLoadedMsg:
|
|
t.mu.Lock()
|
|
if msg.err != nil {
|
|
t.state = keyListError
|
|
t.err = msg.err
|
|
} else {
|
|
t.state = keyListReady
|
|
t.keys = msg.keys
|
|
}
|
|
t.mu.Unlock()
|
|
|
|
case saveKeyResultMsg:
|
|
return t, t.Init()
|
|
|
|
case deleteKeyResultMsg:
|
|
if msg.err != nil {
|
|
t.mu.Lock()
|
|
t.err = msg.err
|
|
t.mu.Unlock()
|
|
}
|
|
return t, t.Init()
|
|
|
|
case tea.KeyMsg:
|
|
switch msg.String() {
|
|
case "up", "k":
|
|
t.mu.Lock()
|
|
if t.selected > 0 {
|
|
t.selected--
|
|
}
|
|
t.mu.Unlock()
|
|
|
|
case "down", "j":
|
|
t.mu.Lock()
|
|
if t.selected < len(t.keys)-1 {
|
|
t.selected++
|
|
}
|
|
t.mu.Unlock()
|
|
|
|
case "ctrl+n":
|
|
return t, func() tea.Msg { return openKeyFormMsg{} }
|
|
|
|
case "ctrl+e":
|
|
t.mu.Lock()
|
|
keys := t.keys
|
|
idx := t.selected
|
|
t.mu.Unlock()
|
|
if len(keys) > 0 && idx >= 0 && idx < len(keys) {
|
|
return t, func() tea.Msg { return openKeyFormMsg{editing: keys[idx]} }
|
|
}
|
|
|
|
case "delete", "d":
|
|
t.mu.Lock()
|
|
keys := t.keys
|
|
idx := t.selected
|
|
t.mu.Unlock()
|
|
if len(keys) > 0 && idx >= 0 && idx < len(keys) {
|
|
return t, deleteKeyCmd(keys[idx].ID, t.dataDir)
|
|
}
|
|
|
|
case "esc":
|
|
return t, func() tea.Msg { return closeFormMsg{} }
|
|
}
|
|
}
|
|
|
|
return t, nil
|
|
}
|
|
|
|
func (t *KeyListTab) View() string {
|
|
t.mu.Lock()
|
|
defer t.mu.Unlock()
|
|
|
|
var b strings.Builder
|
|
|
|
b.WriteString(AppTitleStyle.Render("SSH Key Pairs"))
|
|
b.WriteString("\n\n")
|
|
|
|
switch t.state {
|
|
case keyListLoading:
|
|
b.WriteString(SubtitleStyle.Render("Loading keys..."))
|
|
return b.String()
|
|
case keyListError:
|
|
b.WriteString(ErrorStyle.Render(fmt.Sprintf("Error: %v", t.err)))
|
|
b.WriteString("\n\n")
|
|
b.WriteString(SubtitleStyle.Render("Press Esc to go back"))
|
|
return b.String()
|
|
}
|
|
|
|
if t.err != nil {
|
|
b.WriteString(ErrorStyle.Render(fmt.Sprintf("Error: %v", t.err)))
|
|
b.WriteString("\n\n")
|
|
}
|
|
|
|
if len(t.keys) == 0 {
|
|
b.WriteString(SubtitleStyle.Render("No SSH keys stored."))
|
|
b.WriteString("\n")
|
|
b.WriteString(SubtitleStyle.Render("Press Ctrl+N to add a new key."))
|
|
} else {
|
|
for i, key := range t.keys {
|
|
var line string
|
|
if key.Type != "" {
|
|
line = fmt.Sprintf(" %s (%s)", key.Name, key.Type)
|
|
} else {
|
|
line = fmt.Sprintf(" %s", key.Name)
|
|
}
|
|
|
|
if i == t.selected {
|
|
b.WriteString(SelectedStyle.Render(line))
|
|
} else {
|
|
b.WriteString(HostNameStyle.Render(line))
|
|
}
|
|
b.WriteString("\n")
|
|
}
|
|
}
|
|
|
|
b.WriteString("\n")
|
|
b.WriteString(SubtitleStyle.Render("Ctrl+N:add Ctrl+E:edit D:delete Esc:back"))
|
|
|
|
return b.String()
|
|
}
|
|
|
|
func (t *KeyListTab) Close() {}
|
|
|
|
// keyListLoadedMsg carries the loaded key list
|
|
type keyListLoadedMsg struct {
|
|
keys []*models.KeyPair
|
|
err error
|
|
}
|