From be8b6f42c4611eff036e5389f079f74c089e62fe Mon Sep 17 00:00:00 2001 From: David Allen Date: Tue, 17 Jun 2025 22:05:47 -0600 Subject: [PATCH] Update cache cmd implementation --- cmd/cache.go | 74 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 64 insertions(+), 10 deletions(-) diff --git a/cmd/cache.go b/cmd/cache.go index cbe8167..bed812c 100644 --- a/cmd/cache.go +++ b/cmd/cache.go @@ -271,25 +271,79 @@ var cacheInfoCmd = &cobra.Command{ } var cacheEditCmd = &cobra.Command{ - Use: "edit", - Short: "Modify cache data either interactively or non-interactively.", + Use: "edit", + Example: ` magellan cache edit + magellan cache edit --host https://172.16.0.101 --port 443 --protocol udp + magellan cache edit --host https://172.16.0.101 + `, + Args: cobra.ExactArgs(0), + Short: "Edit existing cache data.", Run: func(cmd *cobra.Command, args []string) { - // start the interactive editor + var ( + columns []table.Column + rows []table.Row + styles table.Styles + ) + if interactive { - p := tea.NewProgram(cache.NewModel()) - if _, err := p.Run(); err != nil { - fmt.Printf("failed to start the cache editor: %v", err) + // load the assets found from scan + scannedResults, err := sqlite.GetScannedAssets(cachePath) + if err != nil { + log.Error().Err(err).Str("path", cachePath).Msg("failed to get scanned assets from cache") + } + + // set columns to cache headers + columns = []table.Column{ + {Title: "hosts", Width: 20}, + {Title: "ports", Width: 5}, + {Title: "protocol", Width: 8}, + {Title: "timestamp", Width: 12}, + } + + // set rows to cache data + for _, asset := range scannedResults { + rows = append(rows, table.Row{ + asset.Host, + fmt.Sprintf("%d", asset.Port), + asset.Protocol, + fmt.Sprintf("%d", asset.Timestamp.Unix()), + }) + } + + // new table + assetsTable := table.New( + table.WithColumns(columns), + table.WithRows(rows), + table.WithFocused(true), + table.WithHeight(10), + ) + + // set up table styling + styles = table.DefaultStyles() + styles.Header = styles.Header. + BorderStyle(lipgloss.NormalBorder()). + BorderForeground(lipgloss.Color("240")). + BorderBottom(true). + Bold(false) + styles.Selected = styles.Selected. + Foreground(lipgloss.Color("229")). + Background(lipgloss.Color("57")). + Bold(false) + assetsTable.SetStyles(styles) + + m := cache.Model{Table: assetsTable} + if _, err := tea.NewProgram(m, tea.WithAltScreen()).Run(); err != nil { + fmt.Println("Error running program:", err) os.Exit(1) } - } else { - // only edit data with arguments } }, } var cacheInfoCmd = &cobra.Command{ - Use: "info", - Short: "Show cache-related information.", + Use: "info", + Short: "Show cache-related information and exit.", + Example: ` magellan cache info`, Run: func(cmd *cobra.Command, args []string) { printCacheInfo(cacheOutputFormat) },