refactor: move V1 code into v1/ subdirectory
- git mv cmd/ internal/ pkg/ test/ go.mod go.sum Makefile build.sh docs/ v1/ - Create v1/README.md with V1 documentation - Update root README for V1 + V2 structure - V1 still builds (cd v1 && go build ./cmd/hostkeeper) and 105 tests pass - Root is now clean for V2 development
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
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/knownhosts"
|
||||
"git.tukangketik.id/swanadiva/hostkeeper/pkg/storage"
|
||||
"git.tukangketik.id/swanadiva/hostkeeper/pkg/tui"
|
||||
)
|
||||
|
||||
var encryptFlag bool
|
||||
|
||||
// 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)
|
||||
tuiCmd.Flags().BoolVar(&encryptFlag, "encrypt", false, "Enable AES-256-GCM encryption for sensitive data")
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
// Initialize known_hosts
|
||||
kh, err := knownhosts.New(cfg.GetDataDir())
|
||||
if err != nil {
|
||||
fmt.Printf("Warning: failed to load known_hosts: %v\n", err)
|
||||
}
|
||||
|
||||
// Auto-detect encrypted data files
|
||||
needPassword := encryptFlag || store.IsDataEncrypted()
|
||||
|
||||
model := tui.New()
|
||||
model.SetDataDir(cfg.GetDataDir())
|
||||
if kh != nil {
|
||||
model.SetKnownHosts(kh)
|
||||
}
|
||||
|
||||
if needPassword {
|
||||
// Show password prompt first — hosts will be loaded after password is set
|
||||
model.ShowEncryptPrompt()
|
||||
} else {
|
||||
// No encryption — load hosts normally
|
||||
ctx := context.Background()
|
||||
hosts, err := store.ListHosts(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to load hosts: %w", err)
|
||||
}
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user