Update cache cmd implementation

This commit is contained in:
David Allen 2025-06-17 22:05:47 -06:00
parent f1f8e4f3fb
commit be8b6f42c4
Signed by: towk
GPG key ID: 0430CDBE22619155

View file

@ -272,24 +272,78 @@ var cacheInfoCmd = &cobra.Command{
var cacheEditCmd = &cobra.Command{
Use: "edit",
Short: "Modify cache data either interactively or non-interactively.",
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.",
Short: "Show cache-related information and exit.",
Example: ` magellan cache info`,
Run: func(cmd *cobra.Command, args []string) {
printCacheInfo(cacheOutputFormat)
},