55b5c7b5a0
- Added program *tea.Program to SFTPBrowserTab and Model
- SetProgram() allows goroutines to send messages to Bubble Tea
- After transfer: program.Send(sftpRefreshMsg{}) triggers re-render
- Model stores program reference, passes to SFTP tab on creation
60 lines
1.3 KiB
Go
60 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"github.com/spf13/cobra"
|
|
|
|
"git.tukangketik.id/swanadiva/hostkeeper/pkg/config"
|
|
"git.tukangketik.id/swanadiva/hostkeeper/pkg/storage"
|
|
"git.tukangketik.id/swanadiva/hostkeeper/pkg/tui"
|
|
)
|
|
|
|
// tuiCmd represents the tui command
|
|
var tuiCmd = &cobra.Command{
|
|
Use: "tui",
|
|
Short: "Launch terminal user interface",
|
|
Long: `Launch an interactive terminal user interface for managing SSH hosts and connections.`,
|
|
RunE: runTUI,
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(tuiCmd)
|
|
}
|
|
|
|
func runTUI(cmd *cobra.Command, args []string) error {
|
|
cfg := appCfg
|
|
if cfg == nil {
|
|
var err error
|
|
cfg, err = config.New()
|
|
if err != nil {
|
|
return fmt.Errorf("failed to initialize config: %w", err)
|
|
}
|
|
}
|
|
|
|
store, err := storage.NewJSONStorage(cfg.GetDataDir())
|
|
if err != nil {
|
|
return fmt.Errorf("failed to initialize storage: %w", err)
|
|
}
|
|
|
|
ctx := context.Background()
|
|
hosts, err := store.ListHosts(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to load hosts: %w", err)
|
|
}
|
|
|
|
model := tui.New()
|
|
model.SetDataDir(cfg.GetDataDir())
|
|
model.LoadHosts(hosts)
|
|
|
|
p := tea.NewProgram(model)
|
|
model.SetProgram(p)
|
|
if _, err := p.Run(); err != nil {
|
|
return fmt.Errorf("failed to run TUI: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|