Skip to content

Commit

Permalink
Added flag to show cache info with list command and other minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
davidallendj committed Aug 9, 2024
1 parent 46fc35d commit 8a25417
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
7 changes: 3 additions & 4 deletions cmd/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ var collectCmd = &cobra.Command{
if accessToken == "" {
var err error
accessToken, err = util.LoadAccessToken(tokenPath)
if err != nil {
log.Error().Err(err).Msgf("failed to load access token")
if err != nil && verbose {
log.Warn().Err(err).Msgf("could not load access token")
}
}

Expand Down Expand Up @@ -72,8 +72,7 @@ var collectCmd = &cobra.Command{

func init() {
currentUser, _ = user.Current()
collectCmd.PersistentFlags().StringVar(&client.Host, "host", client.Host, "set the host to the SMD API")
collectCmd.PersistentFlags().IntVarP(&client.Port, "port", "p", client.Port, "set the port to the SMD API")
collectCmd.PersistentFlags().StringVar(&client.Host, "host", "", "set the host:port to the SMD API")
collectCmd.PersistentFlags().StringVar(&username, "username", "", "set the BMC user")
collectCmd.PersistentFlags().StringVar(&password, "password", "", "set the BMC password")
collectCmd.PersistentFlags().StringVar(&scheme, "scheme", "https", "set the scheme used to query")
Expand Down
14 changes: 13 additions & 1 deletion cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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")
Expand All @@ -37,13 +48,14 @@ 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))
}
}
},
}

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)
}
12 changes: 8 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

magellan "github.com/OpenCHAMI/magellan/internal"
"github.com/OpenCHAMI/magellan/pkg/client"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -76,7 +77,7 @@ func init() {
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().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
viper.BindPFlag("concurrency", rootCmd.Flags().Lookup("concurrency"))
Expand All @@ -92,7 +93,10 @@ func init() {
// See the 'LoadConfig' function in 'internal/config' for details.
func InitializeConfig() {
if configPath != "" {
magellan.LoadConfig(configPath)
err := magellan.LoadConfig(configPath)
if err != nil {
log.Error().Err(err).Msg("failed to load config")
}
}
}

Expand All @@ -102,20 +106,20 @@ func InitializeConfig() {
// TODO: This function should probably be moved to 'internal/config.go'
// instead of in this file.
func SetDefaults() {
currentUser, _ = user.Current()
viper.SetDefault("threads", 1)
viper.SetDefault("timeout", 5)
viper.SetDefault("config", "")
viper.SetDefault("verbose", 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.ports", []int{})
viper.SetDefault("scan.subnets", []string{})
viper.SetDefault("scan.subnet-masks", []net.IP{})
viper.SetDefault("scan.disable-probing", false)
viper.SetDefault("collect.driver", []string{"redfish"})
viper.SetDefault("collect.host", client.Host)
viper.SetDefault("collect.port", client.Port)
viper.SetDefault("collect.user", "")
viper.SetDefault("collect.pass", "")
viper.SetDefault("collect.protocol", "tcp")
Expand Down

0 comments on commit 8a25417

Please sign in to comment.