Removed and updated flags

This commit is contained in:
David J. Allen 2024-05-10 10:09:37 -06:00
parent ebee86ccbe
commit 9663b677ba
No known key found for this signature in database
GPG key ID: 717C593FF60A2ACC
3 changed files with 34 additions and 43 deletions

View file

@ -2,6 +2,7 @@ package cmd
import ( import (
"fmt" "fmt"
"os/user"
magellan "github.com/OpenCHAMI/magellan/internal" magellan "github.com/OpenCHAMI/magellan/internal"
"github.com/OpenCHAMI/magellan/internal/api/smd" "github.com/OpenCHAMI/magellan/internal/api/smd"
@ -44,17 +45,15 @@ var collectCmd = &cobra.Command{
} }
// //
if threads <= 0 { if concurrency <= 0 {
threads = mathutil.Clamp(len(probeStates), 1, 255) concurrency = mathutil.Clamp(len(probeStates), 1, 255)
} }
q := &magellan.QueryParams{ q := &magellan.QueryParams{
User: username, User: username,
Pass: password, Pass: password,
Protocol: protocol, Protocol: protocol,
Drivers: drivers,
Preferred: preferredDriver,
Timeout: timeout, Timeout: timeout,
Threads: threads, Concurrency: concurrency,
Verbose: verbose, Verbose: verbose,
CaCertPath: cacertPath, CaCertPath: cacertPath,
OutputPath: outputPath, OutputPath: outputPath,
@ -72,16 +71,14 @@ var collectCmd = &cobra.Command{
} }
func init() { func init() {
collectCmd.PersistentFlags().StringSliceVar(&drivers, "driver", []string{"redfish"}, "set the driver(s) and fallback drivers to use") currentUser, _ = user.Current()
collectCmd.PersistentFlags().StringVar(&smd.Host, "host", smd.Host, "set the host to the smd API") collectCmd.PersistentFlags().StringVar(&smd.Host, "host", smd.Host, "set the host to the SMD API")
collectCmd.PersistentFlags().IntVarP(&smd.Port, "port", "p", smd.Port, "set the port to the smd API") collectCmd.PersistentFlags().IntVarP(&smd.Port, "port", "p", smd.Port, "set the port to the SMD API")
collectCmd.PersistentFlags().StringVar(&username, "user", "", "set the BMC user") collectCmd.PersistentFlags().StringVar(&username, "user", "", "set the BMC user")
collectCmd.PersistentFlags().StringVar(&password, "pass", "", "set the BMC password") collectCmd.PersistentFlags().StringVar(&password, "pass", "", "set the BMC password")
collectCmd.PersistentFlags().StringVar(&protocol, "protocol", "https", "set the protocol used to query") collectCmd.PersistentFlags().StringVar(&protocol, "protocol", "https", "set the protocol used to query")
collectCmd.PersistentFlags().StringVarP(&outputPath, "output", "o", "/tmp/magellan/data/", "set the path to store collection data") collectCmd.PersistentFlags().StringVarP(&outputPath, "output", "o", fmt.Sprintf("/tmp/%smagellan/data/", currentUser.Username), "set the path to store collection data")
collectCmd.PersistentFlags().BoolVar(&forceUpdate, "force-update", false, "set flag to force update data sent to SMD ") collectCmd.PersistentFlags().BoolVar(&forceUpdate, "force-update", false, "set flag to force update data sent to SMD")
collectCmd.PersistentFlags().StringVar(&preferredDriver, "preferred-driver", "ipmi", "set the preferred driver to use")
collectCmd.PersistentFlags().StringVar(&ipmitoolPath, "ipmitool.path", "/usr/bin/ipmitool", "set the path for ipmitool")
collectCmd.PersistentFlags().StringVar(&cacertPath, "ca-cert", "", "path to CA cert. (defaults to system CAs)") collectCmd.PersistentFlags().StringVar(&cacertPath, "ca-cert", "", "path to CA cert. (defaults to system CAs)")
viper.BindPFlag("collect.driver", collectCmd.Flags().Lookup("driver")) viper.BindPFlag("collect.driver", collectCmd.Flags().Lookup("driver"))
@ -92,10 +89,8 @@ func init() {
viper.BindPFlag("collect.protocol", collectCmd.Flags().Lookup("protocol")) viper.BindPFlag("collect.protocol", collectCmd.Flags().Lookup("protocol"))
viper.BindPFlag("collect.output", collectCmd.Flags().Lookup("output")) viper.BindPFlag("collect.output", collectCmd.Flags().Lookup("output"))
viper.BindPFlag("collect.force-update", collectCmd.Flags().Lookup("force-update")) viper.BindPFlag("collect.force-update", collectCmd.Flags().Lookup("force-update"))
viper.BindPFlag("collect.preferred-driver", collectCmd.Flags().Lookup("preferred-driver")) viper.BindPFlag("collect.ca-cert", collectCmd.Flags().Lookup("secure-tls"))
viper.BindPFlag("collect.ipmitool.path", collectCmd.Flags().Lookup("ipmitool.path")) viper.BindPFlags(collectCmd.Flags())
viper.BindPFlag("collect.secure-tls", collectCmd.Flags().Lookup("secure-tls"))
viper.BindPFlag("collect.cert-pool", collectCmd.Flags().Lookup("cert-pool"))
rootCmd.AddCommand(collectCmd) rootCmd.AddCommand(collectCmd)
} }

View file

@ -13,9 +13,10 @@ import (
) )
var ( var (
currentUser *user.User
accessToken string accessToken string
timeout int timeout int
threads int concurrency int
ports []int ports []int
hosts []string hosts []string
protocol string protocol string
@ -23,9 +24,6 @@ var (
username string username string
password string password string
dbpath string dbpath string
drivers []string
preferredDriver string
ipmitoolPath string
outputPath string outputPath string
configPath string configPath string
verbose bool verbose bool
@ -77,28 +75,26 @@ func LoadAccessToken() (string, error) {
} }
func init() { func init() {
// get the current user currentUser, _ = user.Current()
currentUser, _ := user.Current()
cobra.OnInitialize(InitializeConfig) cobra.OnInitialize(InitializeConfig)
rootCmd.PersistentFlags().IntVar(&threads, "threads", -1, "set the number of threads") rootCmd.PersistentFlags().IntVar(&concurrency, "concurrency", -1, "set the number of concurrent processes")
rootCmd.PersistentFlags().IntVar(&timeout, "timeout", 30, "set the timeout") rootCmd.PersistentFlags().IntVar(&timeout, "timeout", 30, "set the timeout")
rootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "", "set the config file path") rootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "", "set the config file path")
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "set verbose flag") rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "set output verbosity")
rootCmd.PersistentFlags().StringVar(&accessToken, "access-token", "", "set the access token") rootCmd.PersistentFlags().StringVar(&accessToken, "access-token", "", "set the access token")
rootCmd.PersistentFlags().StringVar(&dbpath, "db.path", fmt.Sprintf("/tmp/%smagellan/magellan.db", currentUser.Name+"/"), "set the probe storage path") rootCmd.PersistentFlags().StringVar(&dbpath, "db.path", fmt.Sprintf("/tmp/%smagellan/magellan.db", currentUser.Username+"/"), "set the probe storage path")
// bind viper config flags with cobra // bind viper config flags with cobra
viper.BindPFlag("threads", rootCmd.Flags().Lookup("threads")) viper.BindPFlag("concurrency", rootCmd.Flags().Lookup("concurrency"))
viper.BindPFlag("timeout", rootCmd.Flags().Lookup("timeout")) viper.BindPFlag("timeout", rootCmd.Flags().Lookup("timeout"))
viper.BindPFlag("verbose", rootCmd.Flags().Lookup("verbose")) viper.BindPFlag("verbose", rootCmd.Flags().Lookup("verbose"))
viper.BindPFlag("db.path", rootCmd.Flags().Lookup("db.path")) viper.BindPFlag("db.path", rootCmd.Flags().Lookup("db.path"))
// viper.BindPFlags(rootCmd.Flags()) viper.BindPFlags(rootCmd.Flags())
} }
func InitializeConfig() { func InitializeConfig() {
if configPath != "" { if configPath != "" {
magellan.LoadConfig(configPath) magellan.LoadConfig(configPath)
fmt.Printf("subnets: %v\n", viper.Get("scan.subnets"))
} }
} }

View file

@ -54,10 +54,10 @@ var scanCmd = &cobra.Command{
} }
// scan and store probe data in dbPath // scan and store probe data in dbPath
if threads <= 0 { if concurrency <= 0 {
threads = mathutil.Clamp(len(hostsToScan), 1, 255) concurrency = mathutil.Clamp(len(hostsToScan), 1, 255)
} }
probeStates := magellan.ScanForAssets(hostsToScan, portsToScan, threads, timeout, disableProbing, verbose) probeStates := magellan.ScanForAssets(hostsToScan, portsToScan, concurrency, timeout, disableProbing, verbose)
if verbose { if verbose {
for _, r := range probeStates { for _, r := range probeStates {
fmt.Printf("%s:%d (%s)\n", r.Host, r.Port, r.Protocol) fmt.Printf("%s:%d (%s)\n", r.Host, r.Port, r.Protocol)