From f1f8e4f3fb173f48de56bb40bda6d9fee07b2f43 Mon Sep 17 00:00:00 2001 From: David Allen Date: Mon, 16 Jun 2025 16:19:43 -0600 Subject: [PATCH] refactor: updated cache editor implementation --- cmd/cache.go | 25 +++++++++++++++ internal/cache/edit/table.go | 48 ----------------------------- internal/cache/{edit => }/modify.go | 0 pkg/list.go | 15 --------- 4 files changed, 25 insertions(+), 63 deletions(-) delete mode 100644 internal/cache/edit/table.go rename internal/cache/{edit => }/modify.go (100%) diff --git a/cmd/cache.go b/cmd/cache.go index 316be8d..cbe8167 100644 --- a/cmd/cache.go +++ b/cmd/cache.go @@ -270,6 +270,31 @@ var cacheInfoCmd = &cobra.Command{ }, } +var cacheEditCmd = &cobra.Command{ + Use: "edit", + Short: "Modify cache data either interactively or non-interactively.", + Run: func(cmd *cobra.Command, args []string) { + // start the interactive editor + if interactive { + p := tea.NewProgram(cache.NewModel()) + if _, err := p.Run(); err != nil { + fmt.Printf("failed to start the cache editor: %v", err) + os.Exit(1) + } + } else { + // only edit data with arguments + } + }, +} + +var cacheInfoCmd = &cobra.Command{ + Use: "info", + Short: "Show cache-related information.", + Run: func(cmd *cobra.Command, args []string) { + printCacheInfo(cacheOutputFormat) + }, +} + func init() { cacheEditCmd.Flags().StringVar(&host, "host", "", "Set the new host value.") cacheEditCmd.Flags().IntSliceVar(&ports, "port", nil, "Set the new port values as comma-separated list.") diff --git a/internal/cache/edit/table.go b/internal/cache/edit/table.go deleted file mode 100644 index 7759bd7..0000000 --- a/internal/cache/edit/table.go +++ /dev/null @@ -1,48 +0,0 @@ -package cache - -import ( - "github.com/charmbracelet/bubbles/table" - tea "github.com/charmbracelet/bubbletea" - "github.com/charmbracelet/lipgloss" -) - -var baseStyle = lipgloss.NewStyle(). - BorderStyle(lipgloss.NormalBorder()). - BorderForeground(lipgloss.Color("240")) - -type Model struct { - selected int - Table table.Model -} - -func (m Model) Init() tea.Cmd { return nil } - -func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { - var cmd tea.Cmd - switch msg := msg.(type) { - case tea.WindowSizeMsg: - // m.Table = m.Table.Width(msg.Width) - // m.Table = m.Table.Height(msg.Height) - case tea.KeyMsg: - switch msg.String() { - case "esc": - if m.Table.Focused() { - m.Table.Blur() - } else { - m.Table.Focus() - } - case "q", "ctrl+c": - return m, tea.Quit - case "enter": - return m, tea.Batch( - tea.Printf("Selected host '%s'", m.Table.SelectedRow()[0]), - ) - } - } - m.Table, cmd = m.Table.Update(msg) - return m, cmd -} - -func (m Model) View() string { - return baseStyle.Render(m.Table.View()) + "\n" -} diff --git a/internal/cache/edit/modify.go b/internal/cache/modify.go similarity index 100% rename from internal/cache/edit/modify.go rename to internal/cache/modify.go diff --git a/pkg/list.go b/pkg/list.go index fdea0ba..43e1f23 100644 --- a/pkg/list.go +++ b/pkg/list.go @@ -27,21 +27,6 @@ func PrintRemoteAssets(data []RemoteAsset, format string) { } } -func PrintMapWithFormat(data map[string]any, format string) { - switch strings.ToLower(format) { - case "json": - util.PrintJSON(data) - case "yaml": - util.PrintYAML(data) - case "list": - for k, v := range data { - fmt.Printf("%s: %v\n", k, v) - } - default: - log.Error().Msg("PrintMapWithFormat: unrecognized format") - } -} - func ListDrives(cc *crawler.CrawlerConfig) ([]*redfish.Drive, error) { user, err := cc.GetUserPass() if err != nil {