chore: tidy up rebasing

This commit is contained in:
David Allen 2025-07-10 21:51:50 -06:00
parent 8407876c9e
commit 1ee16153a0
Signed by: towk
GPG key ID: 0430CDBE22619155
3 changed files with 1 additions and 219 deletions

View file

@ -270,148 +270,6 @@ var cacheInfoCmd = &cobra.Command{
},
}
var cacheEditCmd = &cobra.Command{
Use: "edit",
Example: ` // star the cache editor
magellan cache edit -i
// edit a single entry only changing values specified (e.g. port and protocol)
magellan cache edit https://172.16.0.101 --port 443 --protocol udp
// edit two entries' time stamps
magellan cache edit https://172.16.0.101 https://172.16.0.102 --timestamp 06/25/2025
`,
Args: cobra.ExactArgs(0),
Short: "Edit existing cache data.",
Run: func(cmd *cobra.Command, args []string) {
var (
columns []table.Column
rows []table.Row
styles table.Styles
)
if interactive {
// load the assets found from scan
scannedResults, err := sqlite.GetRemoteAssets(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: "host", Width: 30},
{Title: "ports", Width: 8},
{Title: "protocol", Width: 10},
{Title: "timestamp", Width: 20},
}
// 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()),
})
}
// create a 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.NewModel(cachePath, &assetsTable)
if _, err := tea.NewProgram(m, tea.WithAltScreen()).Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
} else {
// non-interactive editting
for _, host := range args {
// get the asset from cache for host
asset, err := sqlite.GetRemoteAsset(cachePath, host)
if err != nil {
log.Warn().Err(err).
Str("host", host).
Str("path", cachePath).
Msg("failed to get asset from cache")
continue
}
if asset == nil {
log.Warn().Err(err).
Str("host", host).
Str("path", cachePath).
Msg("found asset is not valid")
continue
}
// only modify values that are set
if host != "" {
asset.Host = host
}
if protocol != "" {
asset.Protocol = protocol
}
if timestampf != "" {
newTimestamp, err := dateparse.ParseAny(timestampf)
if err != nil {
log.Error().Err(err).Msg("failed to parse timestamp value")
} else {
asset.Timestamp = newTimestamp
}
}
// reinsert the asset into cache for each port
for _, port := range ports {
newAsset := *asset
newAsset.Port = port
err = sqlite.DeleteRemoteAssetsByHost(cachePath, host)
if err != nil {
log.Error().Err(err).
Str("host", host).
Str("path", cachePath).
Msg("failed to delete asset in cache")
continue
}
err = sqlite.InsertRemoteAssets(cachePath, newAsset)
if err != nil {
log.Error().Err(err).
Str("host", host).
Str("path", cachePath).
Msg("failed to re-insert asset into cache")
}
}
}
}
},
}
var cacheInfoCmd = &cobra.Command{
Use: "info",
Short: "Show cache-related information and exit.",
Example: ` magellan cache info`,
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.")