diff --git a/cmd/collect.go b/cmd/collect.go index b2cd151..af4df5e 100644 --- a/cmd/collect.go +++ b/cmd/collect.go @@ -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") } } @@ -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") diff --git a/cmd/list.go b/cmd/list.go index 26cc1fc..826f1e3 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -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) } diff --git a/cmd/root.go b/cmd/root.go index 09a38c1..55365cd 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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" ) @@ -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")) @@ -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") + } } } @@ -102,12 +106,13 @@ 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{}) @@ -115,7 +120,6 @@ func SetDefaults() { 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")