From 2596e8945f6b13d9eb17128ad4a7ce71f4006e3c Mon Sep 17 00:00:00 2001 From: David Allen Date: Sat, 3 May 2025 16:58:17 -0600 Subject: [PATCH] Add support for storage command and crawler output Partially addresses issue #3 by adding a simple `magellan list devices` command to list storage devices. To close the issue, this PR still requires including storage device information in the `crawler`'s output. Reviewed-on: https://git.towk2.me/towk/magellan-ng/pulls/5 --- go.sum | 2 ++ internal/cache/edit/modify.go | 1 + internal/cache/edit/table.go | 48 +++++++++++++++++++++++++++++++++++ internal/util/strings.go | 10 ++++++++ pkg/list.go | 17 ++++++++++++- 5 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 internal/cache/edit/modify.go create mode 100644 internal/cache/edit/table.go create mode 100644 internal/util/strings.go diff --git a/go.sum b/go.sum index 0ce89d4..f024163 100644 --- a/go.sum +++ b/go.sum @@ -192,6 +192,8 @@ golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU= golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU= golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc= +golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= diff --git a/internal/cache/edit/modify.go b/internal/cache/edit/modify.go new file mode 100644 index 0000000..08bf029 --- /dev/null +++ b/internal/cache/edit/modify.go @@ -0,0 +1 @@ +package cache diff --git a/internal/cache/edit/table.go b/internal/cache/edit/table.go new file mode 100644 index 0000000..7759bd7 --- /dev/null +++ b/internal/cache/edit/table.go @@ -0,0 +1,48 @@ +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/util/strings.go b/internal/util/strings.go new file mode 100644 index 0000000..93a046f --- /dev/null +++ b/internal/util/strings.go @@ -0,0 +1,10 @@ +package util + +import "strings" + +func TidyJSON(s string) string { + s = strings.ReplaceAll(s, "\n", "") + s = strings.ReplaceAll(s, "\t", "") + s = strings.ReplaceAll(s, " ", "") + return strings.ReplaceAll(s, "\"", "'") +} diff --git a/pkg/list.go b/pkg/list.go index 43e1f23..abda898 100644 --- a/pkg/list.go +++ b/pkg/list.go @@ -23,7 +23,22 @@ func PrintRemoteAssets(data []RemoteAsset, format string) { fmt.Printf("%s:%d (%s) @%s\n", r.Host, r.Port, r.Protocol, r.Timestamp.Format(time.UnixDate)) } default: - log.Error().Msg("PrintRemoteAssets: unrecognized format") + log.Error().Msg("unrecognized format") + } +} + +func PrintMapFormat(data map[string]any, format string) { + switch strings.ToLower(format) { + case "json": + util.PrintJSON(data) + case "yaml": + util.PrintYAML(data) + case "none": + for k, v := range data { + fmt.Printf("%s: %v\n", k, v) + } + default: + log.Error().Msg("unrecognized format") } }