From 81ec43a923cc750a69b2870e0f8ffede9b7db910 Mon Sep 17 00:00:00 2001 From: David Allen Date: Tue, 13 Aug 2024 10:41:06 -0600 Subject: [PATCH] Minor changes to fix lint errors --- cmd/collect.go | 25 ++++++++---------- cmd/crawl.go | 4 +-- cmd/list.go | 4 +-- cmd/login.go | 12 ++++----- cmd/root.go | 51 ++++++++++++++++++++++--------------- cmd/scan.go | 13 ++++++---- cmd/update.go | 34 +++++++++++-------------- internal/util/error.go | 2 +- pkg/client/smd.go | 2 +- tests/api_test.go | 6 ++++- tests/compatibility_test.go | 15 +++++++---- 11 files changed, 90 insertions(+), 78 deletions(-) diff --git a/cmd/collect.go b/cmd/collect.go index f5d83d7..74d3521 100644 --- a/cmd/collect.go +++ b/cmd/collect.go @@ -14,10 +14,6 @@ import ( "github.com/spf13/viper" ) -var ( - forceUpdate bool -) - // The `collect` command fetches data from a collection of BMC nodes. // This command should be ran after the `scan` to find available hosts // on a subnet. @@ -82,7 +78,7 @@ func init() { collectCmd.PersistentFlags().StringVar(&password, "password", "", "Set the BMC password") collectCmd.PersistentFlags().StringVar(&scheme, "scheme", "https", "Set the scheme used to query") collectCmd.PersistentFlags().StringVar(&protocol, "protocol", "tcp", "Set the protocol used to query") - collectCmd.PersistentFlags().StringVarP(&outputPath, "output", "o", fmt.Sprintf("/tmp/%smagellan/inventory/", currentUser.Username+"/"), "set the path to store collection data") + collectCmd.PersistentFlags().StringVarP(&outputPath, "output", "o", fmt.Sprintf("/tmp/%smagellan/inventory/", 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().StringVar(&cacertPath, "cacert", "", "Path to CA cert. (defaults to system CAs)") @@ -90,16 +86,15 @@ func init() { collectCmd.MarkFlagsRequiredTogether("username", "password") // bind flags to config properties - viper.BindPFlag("collect.driver", collectCmd.Flags().Lookup("driver")) - viper.BindPFlag("collect.host", collectCmd.Flags().Lookup("host")) - viper.BindPFlag("collect.port", collectCmd.Flags().Lookup("port")) - viper.BindPFlag("collect.username", collectCmd.Flags().Lookup("username")) - viper.BindPFlag("collect.password", collectCmd.Flags().Lookup("password")) - viper.BindPFlag("collect.protocol", collectCmd.Flags().Lookup("protocol")) - viper.BindPFlag("collect.output", collectCmd.Flags().Lookup("output")) - viper.BindPFlag("collect.force-update", collectCmd.Flags().Lookup("force-update")) - viper.BindPFlag("collect.cacert", collectCmd.Flags().Lookup("secure-tls")) - viper.BindPFlags(collectCmd.Flags()) + checkBindFlagError(viper.BindPFlag("collect.host", collectCmd.Flags().Lookup("collect.host"))) + checkBindFlagError(viper.BindPFlag("collect.username", collectCmd.Flags().Lookup("collect.username"))) + checkBindFlagError(viper.BindPFlag("collect.password", collectCmd.Flags().Lookup("collect.password"))) + checkBindFlagError(viper.BindPFlag("collect.scheme", collectCmd.Flags().Lookup("collect.scheme"))) + checkBindFlagError(viper.BindPFlag("collect.protocol", collectCmd.Flags().Lookup("collect.protocol"))) + checkBindFlagError(viper.BindPFlag("collect.output", collectCmd.Flags().Lookup("collect.output"))) + checkBindFlagError(viper.BindPFlag("collect.force-update", collectCmd.Flags().Lookup("collect.force-update"))) + checkBindFlagError(viper.BindPFlag("collect.cacert", collectCmd.Flags().Lookup("collect.cacert"))) + checkBindFlagError(viper.BindPFlags(collectCmd.Flags())) rootCmd.AddCommand(collectCmd) } diff --git a/cmd/crawl.go b/cmd/crawl.go index 1eff5ad..4dab37d 100644 --- a/cmd/crawl.go +++ b/cmd/crawl.go @@ -62,8 +62,8 @@ var crawlCmd = &cobra.Command{ } func init() { - crawlCmd.Flags().StringP("username", "u", "", "Username for the BMC") - crawlCmd.Flags().StringP("password", "p", "", "Password for the BMC") + crawlCmd.Flags().StringP("username", "u", "", "Set the username for the BMC") + crawlCmd.Flags().StringP("password", "p", "", "Set the password for the BMC") crawlCmd.Flags().BoolP("insecure", "i", false, "Ignore SSL errors") rootCmd.AddCommand(crawlCmd) diff --git a/cmd/list.go b/cmd/list.go index 826f1e3..e09299f 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -55,7 +55,7 @@ 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") + 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/login.go b/cmd/login.go index 79a5243..47aec06 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -77,11 +77,11 @@ var loginCmd = &cobra.Command{ } func init() { - loginCmd.Flags().StringVar(&loginUrl, "url", "http://127.0.0.1:3333/login", "set the login URL") - loginCmd.Flags().StringVar(&targetHost, "target-host", "127.0.0.1", "set the target host to return the access code") - loginCmd.Flags().IntVar(&targetPort, "target-port", 5000, "set the target host to return the access code") - loginCmd.Flags().BoolVarP(&forceLogin, "force", "f", false, "start the login process even with a valid token") - loginCmd.Flags().StringVar(&tokenPath, "token-path", ".ochami-token", "set the path the load/save the access token") - loginCmd.Flags().BoolVar(&noBrowser, "no-browser", false, "prevent the default browser from being opened automatically") + loginCmd.Flags().StringVar(&loginUrl, "url", "http://127.0.0.1:3333/login", "Set the login URL") + loginCmd.Flags().StringVar(&targetHost, "target-host", "127.0.0.1", "Set the target host to return the access code") + loginCmd.Flags().IntVar(&targetPort, "target-port", 5000, "Set the target host to return the access code") + loginCmd.Flags().BoolVarP(&forceLogin, "force", "f", false, "Start the login process even with a valid token") + loginCmd.Flags().StringVar(&tokenPath, "token-path", ".ochami-token", "Set the path to load/save the access token") + loginCmd.Flags().BoolVar(&noBrowser, "no-browser", false, "Prevent the default browser from being opened automatically") rootCmd.AddCommand(loginCmd) } diff --git a/cmd/root.go b/cmd/root.go index a761f7d..e9a497f 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -43,6 +43,7 @@ var ( configPath string verbose bool debug bool + forceUpdate bool ) // The `root` command doesn't do anything on it's own except display @@ -53,7 +54,10 @@ var rootCmd = &cobra.Command{ Long: "", Run: func(cmd *cobra.Command, args []string) { if len(args) == 0 { - cmd.Help() + err := cmd.Help() + if err != nil { + log.Error().Err(err).Msg("failed to print help") + } os.Exit(0) } }, @@ -79,11 +83,19 @@ func init() { 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")) - viper.BindPFlag("timeout", rootCmd.Flags().Lookup("timeout")) - viper.BindPFlag("verbose", rootCmd.Flags().Lookup("verbose")) - viper.BindPFlag("cache", rootCmd.Flags().Lookup("cache")) - viper.BindPFlags(rootCmd.Flags()) + checkBindFlagError(viper.BindPFlag("concurrency", rootCmd.Flags().Lookup("concurrency"))) + checkBindFlagError(viper.BindPFlag("timeout", rootCmd.Flags().Lookup("timeout"))) + checkBindFlagError(viper.BindPFlag("verbose", rootCmd.Flags().Lookup("verbose"))) + checkBindFlagError(viper.BindPFlag("debug", rootCmd.Flags().Lookup("debug"))) + checkBindFlagError(viper.BindPFlag("access-token", rootCmd.Flags().Lookup("verbose"))) + checkBindFlagError(viper.BindPFlag("cache", rootCmd.Flags().Lookup("cache"))) + checkBindFlagError(viper.BindPFlags(rootCmd.Flags())) +} + +func checkBindFlagError(err error) { + if err != nil { + log.Error().Err(err).Msg("failed to bind flag") + } } // InitializeConfig() initializes a new config object by loading it @@ -117,24 +129,21 @@ func SetDefaults() { 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("scan.disable-cache", false) viper.SetDefault("collect.host", host) - viper.SetDefault("collect.user", "") - viper.SetDefault("collect.pass", "") + viper.SetDefault("collect.username", "") + viper.SetDefault("collect.password", "") viper.SetDefault("collect.protocol", "tcp") viper.SetDefault("collect.output", "/tmp/magellan/data/") viper.SetDefault("collect.force-update", false) - viper.SetDefault("collect.ca-cert", "") - viper.SetDefault("bmc-host", "") - viper.SetDefault("bmc-port", 443) - viper.SetDefault("user", "") - viper.SetDefault("pass", "") - viper.SetDefault("transfer-protocol", "HTTP") - viper.SetDefault("protocol", "tcp") - viper.SetDefault("firmware-url", "") - viper.SetDefault("firmware-version", "") - viper.SetDefault("component", "") - viper.SetDefault("secure-tls", false) - viper.SetDefault("status", false) + viper.SetDefault("collect.cacert", "") + viper.SetDefault("update.username", "") + viper.SetDefault("update.password", "") + viper.SetDefault("update.transfer-protocol", "https") + viper.SetDefault("update.protocol", "tcp") + viper.SetDefault("update.firmware.url", "") + viper.SetDefault("update.firmware.version", "") + viper.SetDefault("update.component", "") + viper.SetDefault("update.status", false) } diff --git a/cmd/scan.go b/cmd/scan.go index f8b4763..8aab3e8 100644 --- a/cmd/scan.go +++ b/cmd/scan.go @@ -184,11 +184,14 @@ func init() { scanCmd.Flags().BoolVar(&disableProbing, "disable-probing", false, "Disable probing found assets for Redfish service(s) running on BMC nodes") scanCmd.Flags().BoolVar(&disableCache, "disable-cache", false, "Disable saving found assets to a cache database specified with 'cache' flag") - viper.BindPFlag("scan.hosts", scanCmd.Flags().Lookup("host")) - viper.BindPFlag("scan.ports", scanCmd.Flags().Lookup("port")) - viper.BindPFlag("scan.subnets", scanCmd.Flags().Lookup("subnet")) - viper.BindPFlag("scan.subnet-masks", scanCmd.Flags().Lookup("subnet-mask")) - viper.BindPFlag("scan.disable-probing", scanCmd.Flags().Lookup("disable-probing")) + checkBindFlagError(viper.BindPFlag("scan.hosts", scanCmd.Flags().Lookup("host"))) + checkBindFlagError(viper.BindPFlag("scan.ports", scanCmd.Flags().Lookup("port"))) + checkBindFlagError(viper.BindPFlag("scan.scheme", scanCmd.Flags().Lookup("scheme"))) + checkBindFlagError(viper.BindPFlag("scan.protocol", scanCmd.Flags().Lookup("protocol"))) + checkBindFlagError(viper.BindPFlag("scan.subnets", scanCmd.Flags().Lookup("subnet"))) + checkBindFlagError(viper.BindPFlag("scan.subnet-masks", scanCmd.Flags().Lookup("subnet-mask"))) + checkBindFlagError(viper.BindPFlag("scan.disable-probing", scanCmd.Flags().Lookup("disable-probing"))) + checkBindFlagError(viper.BindPFlag("scan.disable-cache", scanCmd.Flags().Lookup("disable-cache"))) rootCmd.AddCommand(scanCmd) } diff --git a/cmd/update.go b/cmd/update.go index 66dcdc3..2c99de9 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -12,7 +12,6 @@ import ( var ( host string - port int firmwareUrl string firmwareVersion string component string @@ -79,25 +78,22 @@ var updateCmd = &cobra.Command{ } func init() { - updateCmd.Flags().StringVar(&username, "username", "", "set the BMC user") - updateCmd.Flags().StringVar(&password, "password", "", "set the BMC password") - updateCmd.Flags().StringVar(&transferProtocol, "transfer-protocol", "HTTP", "set the transfer protocol") - updateCmd.Flags().StringVar(&firmwareUrl, "firmware.url", "", "set the path to the firmware") - updateCmd.Flags().StringVar(&firmwareVersion, "firmware.version", "", "set the version of firmware to be installed") - updateCmd.Flags().StringVar(&component, "component", "", "set the component to upgrade") - updateCmd.Flags().BoolVar(&showStatus, "status", false, "get the status of the update") + updateCmd.Flags().StringVar(&username, "username", "", "Set the BMC user") + updateCmd.Flags().StringVar(&password, "password", "", "Set the BMC password") + updateCmd.Flags().StringVar(&transferProtocol, "transfer-protocol", "HTTP", "Set the transfer protocol") + updateCmd.Flags().StringVar(&firmwareUrl, "firmware.url", "", "Set the path to the firmware") + updateCmd.Flags().StringVar(&firmwareVersion, "firmware.version", "", "Set the version of firmware to be installed") + updateCmd.Flags().StringVar(&component, "component", "", "Set the component to upgrade (BMC|BIOS)") + updateCmd.Flags().BoolVar(&showStatus, "status", false, "Get the status of the update") - viper.BindPFlag("update.bmc.host", updateCmd.Flags().Lookup("bmc.host")) - viper.BindPFlag("update.bmc.port", updateCmd.Flags().Lookup("bmc.port")) - viper.BindPFlag("update.username", updateCmd.Flags().Lookup("username")) - viper.BindPFlag("update.password", updateCmd.Flags().Lookup("password")) - viper.BindPFlag("update.transfer-protocol", updateCmd.Flags().Lookup("transfer-protocol")) - viper.BindPFlag("update.protocol", updateCmd.Flags().Lookup("protocol")) - viper.BindPFlag("update.firmware.url", updateCmd.Flags().Lookup("firmware.url")) - viper.BindPFlag("update.firmware.version", updateCmd.Flags().Lookup("firmware.version")) - viper.BindPFlag("update.component", updateCmd.Flags().Lookup("component")) - viper.BindPFlag("update.secure-tls", updateCmd.Flags().Lookup("secure-tls")) - viper.BindPFlag("update.status", updateCmd.Flags().Lookup("status")) + checkBindFlagError(viper.BindPFlag("update.username", updateCmd.Flags().Lookup("username"))) + checkBindFlagError(viper.BindPFlag("update.password", updateCmd.Flags().Lookup("password"))) + checkBindFlagError(viper.BindPFlag("update.transfer-protocol", updateCmd.Flags().Lookup("transfer-protocol"))) + checkBindFlagError(viper.BindPFlag("update.protocol", updateCmd.Flags().Lookup("protocol"))) + checkBindFlagError(viper.BindPFlag("update.firmware.url", updateCmd.Flags().Lookup("firmware.url"))) + checkBindFlagError(viper.BindPFlag("update.firmware.version", updateCmd.Flags().Lookup("firmware.version"))) + checkBindFlagError(viper.BindPFlag("update.component", updateCmd.Flags().Lookup("component"))) + checkBindFlagError(viper.BindPFlag("update.status", updateCmd.Flags().Lookup("status"))) rootCmd.AddCommand(updateCmd) } diff --git a/internal/util/error.go b/internal/util/error.go index addca80..0141ea7 100644 --- a/internal/util/error.go +++ b/internal/util/error.go @@ -11,8 +11,8 @@ import "fmt" func FormatErrorList(errList []error) error { var err error for i, e := range errList { + // NOTE: for multi-error formating, we want to include \n here err = fmt.Errorf("\t[%d] %v\n", i, e) - i += 1 } return err } diff --git a/pkg/client/smd.go b/pkg/client/smd.go index f39d3ce..69fbf17 100644 --- a/pkg/client/smd.go +++ b/pkg/client/smd.go @@ -54,12 +54,12 @@ func (c SmdClient) Update(data HTTPBody, headers HTTPHeader) error { // Update redfish endpoint via PUT `/hsm/v2/Inventory/RedfishEndpoints` endpoint url := c.RootEndpoint("/Inventory/RedfishEndpoints/" + c.Xname) res, body, err := MakeRequest(c.Client, url, http.MethodPut, data, headers) - fmt.Printf("%v (%v)\n%s\n", url, res.Status, string(body)) if res != nil { statusOk := res.StatusCode >= 200 && res.StatusCode < 300 if !statusOk { return fmt.Errorf("failed to update redfish endpoint (returned %s)", res.Status) } + fmt.Printf("%v (%v)\n%s\n", url, res.Status, string(body)) } return err } diff --git a/tests/api_test.go b/tests/api_test.go index 4b8ddf3..c213451 100644 --- a/tests/api_test.go +++ b/tests/api_test.go @@ -11,6 +11,7 @@ import ( "testing" magellan "github.com/OpenCHAMI/magellan/internal" + "github.com/rs/zerolog/log" ) var ( @@ -43,7 +44,10 @@ func TestScanAndCollect(t *testing.T) { } // do a collect on the emulator cluster to collect Redfish info - magellan.CollectInventory(&results, &magellan.CollectParams{}) + err := magellan.CollectInventory(&results, &magellan.CollectParams{}) + if err != nil { + log.Error().Err(err).Msg("failed to collect inventory") + } } func TestCrawlCommand(t *testing.T) { diff --git a/tests/compatibility_test.go b/tests/compatibility_test.go index b879536..86e3a4f 100644 --- a/tests/compatibility_test.go +++ b/tests/compatibility_test.go @@ -15,6 +15,7 @@ import ( "github.com/OpenCHAMI/magellan/pkg/client" "github.com/OpenCHAMI/magellan/pkg/crawler" + "github.com/rs/zerolog/log" ) var ( @@ -26,7 +27,7 @@ var ( // Simple test to fetch the base Redfish URL and assert a 200 OK response. func TestRedfishV1Availability(t *testing.T) { var ( - url = fmt.Sprintf("%s/redfish/v1", host) + url = fmt.Sprintf("%s/redfish/v1", *host) body = []byte{} headers = map[string]string{} ) @@ -55,12 +56,16 @@ func TestRedfishV1Availability(t *testing.T) { // Simple test to ensure an expected Redfish version minimum requirement. func TestRedfishVersion(t *testing.T) { var ( - url = fmt.Sprintf("%s/redfish/v1", host) - body = []byte{} - headers = map[string]string{} + url string = fmt.Sprintf("%s/redfish/v1", *host) + body client.HTTPBody = []byte{} + headers client.HTTPHeader = map[string]string{} + err error ) - client.MakeRequest(nil, url, http.MethodGet, body, headers) + _, _, err = client.MakeRequest(nil, url, http.MethodGet, body, headers) + if err != nil { + log.Error().Err(err).Msg("failed to make request") + } } // Crawls a BMC node and checks that we're able to query certain properties