Added flag to show cache info with list command and other minor changes

This commit is contained in:
David Allen 2024-08-09 07:42:28 -06:00
parent 46fc35d4ad
commit 8a2541717d
No known key found for this signature in database
GPG key ID: 717C593FF60A2ACC
3 changed files with 24 additions and 9 deletions

View file

@ -12,6 +12,10 @@ import (
"github.com/spf13/cobra"
)
var (
showCache bool
)
// The `list` command provides an easy way to show what was found
// and stored in a cache database from a scan. The data that's stored
// is what is consumed by the `collect` command with the --cache flag.
@ -24,6 +28,13 @@ var listCmd = &cobra.Command{
" magellan list\n" +
" magellan list --cache ./assets.db",
Run: func(cmd *cobra.Command, args []string) {
// check if we just want to show cache-related info and exit
if showCache {
fmt.Printf("cache: %s\n", cachePath)
return
}
// load the assets found from scan
scannedResults, err := sqlite.GetScannedAssets(cachePath)
if err != nil {
log.Error().Err(err).Msg("failed to get scanned assets")
@ -37,7 +48,7 @@ var listCmd = &cobra.Command{
fmt.Printf("%s\n", string(b))
} else {
for _, r := range scannedResults {
fmt.Printf("%s:%d (%s) @ %s\n", r.Host, r.Port, r.Protocol, r.Timestamp.Format(time.UnixDate))
fmt.Printf("%s:%d (%s) @%s\n", r.Host, r.Port, r.Protocol, r.Timestamp.Format(time.UnixDate))
}
}
},
@ -45,5 +56,6 @@ var listCmd = &cobra.Command{
func init() {
listCmd.Flags().StringVar(&format, "format", "", "set the output format (json|default)")
listCmd.Flags().BoolVar(&showCache, "cache-info", false, "show cache information and exit")
rootCmd.AddCommand(listCmd)
}