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

@ -40,8 +40,8 @@ var collectCmd = &cobra.Command{
if accessToken == "" { if accessToken == "" {
var err error var err error
accessToken, err = util.LoadAccessToken(tokenPath) accessToken, err = util.LoadAccessToken(tokenPath)
if err != nil { if err != nil && verbose {
log.Error().Err(err).Msgf("failed to load access token") log.Warn().Err(err).Msgf("could not load access token")
} }
} }
@ -72,8 +72,7 @@ var collectCmd = &cobra.Command{
func init() { func init() {
currentUser, _ = user.Current() currentUser, _ = user.Current()
collectCmd.PersistentFlags().StringVar(&client.Host, "host", client.Host, "set the host to the SMD API") collectCmd.PersistentFlags().StringVar(&client.Host, "host", "", "set the host:port to the SMD API")
collectCmd.PersistentFlags().IntVarP(&client.Port, "port", "p", client.Port, "set the port to the SMD API")
collectCmd.PersistentFlags().StringVar(&username, "username", "", "set the BMC user") collectCmd.PersistentFlags().StringVar(&username, "username", "", "set the BMC user")
collectCmd.PersistentFlags().StringVar(&password, "password", "", "set the BMC password") collectCmd.PersistentFlags().StringVar(&password, "password", "", "set the BMC password")
collectCmd.PersistentFlags().StringVar(&scheme, "scheme", "https", "set the scheme used to query") collectCmd.PersistentFlags().StringVar(&scheme, "scheme", "https", "set the scheme used to query")

View file

@ -12,6 +12,10 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var (
showCache bool
)
// The `list` command provides an easy way to show what was found // 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 // 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. // 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\n" +
" magellan list --cache ./assets.db", " magellan list --cache ./assets.db",
Run: func(cmd *cobra.Command, args []string) { 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) scannedResults, err := sqlite.GetScannedAssets(cachePath)
if err != nil { if err != nil {
log.Error().Err(err).Msg("failed to get scanned assets") log.Error().Err(err).Msg("failed to get scanned assets")
@ -37,7 +48,7 @@ var listCmd = &cobra.Command{
fmt.Printf("%s\n", string(b)) fmt.Printf("%s\n", string(b))
} else { } else {
for _, r := range scannedResults { 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() { func init() {
listCmd.Flags().StringVar(&format, "format", "", "set the output format (json|default)") 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) rootCmd.AddCommand(listCmd)
} }

View file

@ -22,6 +22,7 @@ import (
magellan "github.com/OpenCHAMI/magellan/internal" magellan "github.com/OpenCHAMI/magellan/internal"
"github.com/OpenCHAMI/magellan/pkg/client" "github.com/OpenCHAMI/magellan/pkg/client"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -76,7 +77,7 @@ func init() {
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "set to enable/disable verbose output") rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "set to enable/disable verbose output")
rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "set to enable/disable debug messages") rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "set to enable/disable debug messages")
rootCmd.PersistentFlags().StringVar(&accessToken, "access-token", "", "set the access token") rootCmd.PersistentFlags().StringVar(&accessToken, "access-token", "", "set the access token")
rootCmd.PersistentFlags().StringVar(&cachePath, "cache", fmt.Sprintf("/tmp/%smagellan/magellan.db", currentUser.Username+"/"), "set the scanning result cache path") rootCmd.PersistentFlags().StringVar(&cachePath, "cache", fmt.Sprintf("/tmp/%s/magellan/assets.db", currentUser.Username), "set the scanning result cache path")
// bind viper config flags with cobra // bind viper config flags with cobra
viper.BindPFlag("concurrency", rootCmd.Flags().Lookup("concurrency")) viper.BindPFlag("concurrency", rootCmd.Flags().Lookup("concurrency"))
@ -92,7 +93,10 @@ func init() {
// See the 'LoadConfig' function in 'internal/config' for details. // See the 'LoadConfig' function in 'internal/config' for details.
func InitializeConfig() { func InitializeConfig() {
if configPath != "" { if configPath != "" {
magellan.LoadConfig(configPath) err := magellan.LoadConfig(configPath)
if err != nil {
log.Error().Err(err).Msg("failed to load config")
}
} }
} }
@ -102,12 +106,13 @@ func InitializeConfig() {
// TODO: This function should probably be moved to 'internal/config.go' // TODO: This function should probably be moved to 'internal/config.go'
// instead of in this file. // instead of in this file.
func SetDefaults() { func SetDefaults() {
currentUser, _ = user.Current()
viper.SetDefault("threads", 1) viper.SetDefault("threads", 1)
viper.SetDefault("timeout", 5) viper.SetDefault("timeout", 5)
viper.SetDefault("config", "") viper.SetDefault("config", "")
viper.SetDefault("verbose", false) viper.SetDefault("verbose", false)
viper.SetDefault("debug", false) viper.SetDefault("debug", false)
viper.SetDefault("cache", "/tmp/magellan/magellan.db") viper.SetDefault("cache", fmt.Sprintf("/tmp/%s/magellan/magellan.db", currentUser.Username))
viper.SetDefault("scan.hosts", []string{}) viper.SetDefault("scan.hosts", []string{})
viper.SetDefault("scan.ports", []int{}) viper.SetDefault("scan.ports", []int{})
viper.SetDefault("scan.subnets", []string{}) viper.SetDefault("scan.subnets", []string{})
@ -115,7 +120,6 @@ func SetDefaults() {
viper.SetDefault("scan.disable-probing", false) viper.SetDefault("scan.disable-probing", false)
viper.SetDefault("collect.driver", []string{"redfish"}) viper.SetDefault("collect.driver", []string{"redfish"})
viper.SetDefault("collect.host", client.Host) viper.SetDefault("collect.host", client.Host)
viper.SetDefault("collect.port", client.Port)
viper.SetDefault("collect.user", "") viper.SetDefault("collect.user", "")
viper.SetDefault("collect.pass", "") viper.SetDefault("collect.pass", "")
viper.SetDefault("collect.protocol", "tcp") viper.SetDefault("collect.protocol", "tcp")