From 94a1ea33fd7542de6d45b86d35c38d1e406aee49 Mon Sep 17 00:00:00 2001 From: "David J. Allen" Date: Wed, 11 Dec 2024 10:14:35 -0700 Subject: [PATCH] cmd: exported scan and collect commands for external use --- cmd/collect.go | 40 ++++++++++++++++++++-------------------- cmd/scan.go | 38 +++++++++++++++++++------------------- 2 files changed, 39 insertions(+), 39 deletions(-) diff --git a/cmd/collect.go b/cmd/collect.go index ed33dd4..53807c3 100644 --- a/cmd/collect.go +++ b/cmd/collect.go @@ -17,7 +17,7 @@ import ( // 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. -var collectCmd = &cobra.Command{ +var CollectCmd = &cobra.Command{ Use: "collect", Short: "Collect system information by interrogating BMC node", Long: "Send request(s) to a collection of hosts running Redfish services found stored from the 'scan' in cache.\n" + @@ -75,28 +75,28 @@ var collectCmd = &cobra.Command{ func init() { currentUser, _ = user.Current() - collectCmd.PersistentFlags().StringVar(&host, "host", "", "Set the URI to the SMD root endpoint") - 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") - 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().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)") + CollectCmd.PersistentFlags().StringVar(&host, "host", "", "Set the URI to the SMD root endpoint") + 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") + 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().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)") // set flags to only be used together - collectCmd.MarkFlagsRequiredTogether("username", "password") + CollectCmd.MarkFlagsRequiredTogether("username", "password") // bind flags to config properties - checkBindFlagError(viper.BindPFlag("collect.host", collectCmd.Flags().Lookup("host"))) - checkBindFlagError(viper.BindPFlag("collect.username", collectCmd.Flags().Lookup("username"))) - checkBindFlagError(viper.BindPFlag("collect.password", collectCmd.Flags().Lookup("password"))) - checkBindFlagError(viper.BindPFlag("collect.scheme", collectCmd.Flags().Lookup("scheme"))) - checkBindFlagError(viper.BindPFlag("collect.protocol", collectCmd.Flags().Lookup("protocol"))) - checkBindFlagError(viper.BindPFlag("collect.output", collectCmd.Flags().Lookup("output"))) - checkBindFlagError(viper.BindPFlag("collect.force-update", collectCmd.Flags().Lookup("force-update"))) - checkBindFlagError(viper.BindPFlag("collect.cacert", collectCmd.Flags().Lookup("cacert"))) - checkBindFlagError(viper.BindPFlags(collectCmd.Flags())) + checkBindFlagError(viper.BindPFlag("collect.host", CollectCmd.Flags().Lookup("host"))) + checkBindFlagError(viper.BindPFlag("collect.username", CollectCmd.Flags().Lookup("username"))) + checkBindFlagError(viper.BindPFlag("collect.password", CollectCmd.Flags().Lookup("password"))) + checkBindFlagError(viper.BindPFlag("collect.scheme", CollectCmd.Flags().Lookup("scheme"))) + checkBindFlagError(viper.BindPFlag("collect.protocol", CollectCmd.Flags().Lookup("protocol"))) + checkBindFlagError(viper.BindPFlag("collect.output", CollectCmd.Flags().Lookup("output"))) + checkBindFlagError(viper.BindPFlag("collect.force-update", CollectCmd.Flags().Lookup("force-update"))) + checkBindFlagError(viper.BindPFlag("collect.cacert", CollectCmd.Flags().Lookup("cacert"))) + checkBindFlagError(viper.BindPFlags(CollectCmd.Flags())) - rootCmd.AddCommand(collectCmd) + rootCmd.AddCommand(CollectCmd) } diff --git a/cmd/scan.go b/cmd/scan.go index 70b636c..d2cbaf8 100644 --- a/cmd/scan.go +++ b/cmd/scan.go @@ -32,7 +32,7 @@ var ( // // See the `ScanForAssets()` function in 'internal/scan.go' for details // related to the implementation. -var scanCmd = &cobra.Command{ +var ScanCmd = &cobra.Command{ Use: "scan urls...", Short: "Scan to discover BMC nodes on a network", Long: "Perform a net scan by attempting to connect to each host and port specified and getting a response.\n" + @@ -174,24 +174,24 @@ var scanCmd = &cobra.Command{ } func init() { - // scanCmd.Flags().StringSliceVar(&hosts, "host", []string{}, "set additional hosts to scan") - scanCmd.Flags().StringSliceVar(&hosts, "host", nil, "Add individual hosts to scan. (example: https://my.bmc.com:5000; same as using positional args)") - scanCmd.Flags().IntSliceVar(&ports, "port", nil, "Adds additional ports to scan for each host with unspecified ports.") - scanCmd.Flags().StringVar(&scheme, "scheme", "https", "Set the default scheme to use if not specified in host URI. (default is 'https')") - scanCmd.Flags().StringVar(&protocol, "protocol", "tcp", "Set the default protocol to use in scan. (default is 'tcp')") - scanCmd.Flags().StringSliceVar(&subnets, "subnet", nil, "Add additional hosts from specified subnets to scan.") - scanCmd.Flags().IPMaskVar(&subnetMask, "subnet-mask", net.IPv4Mask(255, 255, 255, 0), "Set the default subnet mask to use for with all subnets not using CIDR notation.") - 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") + // ScanCmd.Flags().StringSliceVar(&hosts, "host", []string{}, "set additional hosts to scan") + ScanCmd.Flags().StringSliceVar(&hosts, "host", nil, "Add individual hosts to scan. (example: https://my.bmc.com:5000; same as using positional args)") + ScanCmd.Flags().IntSliceVar(&ports, "port", nil, "Adds additional ports to scan for each host with unspecified ports.") + ScanCmd.Flags().StringVar(&scheme, "scheme", "https", "Set the default scheme to use if not specified in host URI. (default is 'https')") + ScanCmd.Flags().StringVar(&protocol, "protocol", "tcp", "Set the default protocol to use in scan. (default is 'tcp')") + ScanCmd.Flags().StringSliceVar(&subnets, "subnet", nil, "Add additional hosts from specified subnets to scan.") + ScanCmd.Flags().IPMaskVar(&subnetMask, "subnet-mask", net.IPv4Mask(255, 255, 255, 0), "Set the default subnet mask to use for with all subnets not using CIDR notation.") + 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") - 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"))) + 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) + rootCmd.AddCommand(ScanCmd) }