mirror of
https://github.com/davidallendj/magellan.git
synced 2025-12-19 19:17:02 -07:00
Update cache cmd implementation
This commit is contained in:
parent
f1f8e4f3fb
commit
be8b6f42c4
1 changed files with 64 additions and 10 deletions
74
cmd/cache.go
74
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)
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue