Add standalone command for querying a single BMC and outputting inventory json

This commit is contained in:
Alex Lovell-Troy 2024-06-27 08:56:45 +02:00
parent 5072b9bc21
commit a7d5b0ebbb
No known key found for this signature in database
2 changed files with 131 additions and 0 deletions

43
cmd/crawl.go Normal file
View file

@ -0,0 +1,43 @@
package cmd
import (
"encoding/json"
"fmt"
"github.com/OpenCHAMI/magellan/pkg/crawler"
"github.com/spf13/cobra"
)
var crawlCmd = &cobra.Command{
Use: "crawl",
Short: "Crawl a single BMC for inventory information",
Run: func(cmd *cobra.Command, args []string) {
systems, err := crawler.CrawlBMC(crawler.CrawlerConfig{
URI: cmd.Flag("uri").Value.String(),
Username: cmd.Flag("username").Value.String(),
Password: cmd.Flag("password").Value.String(),
Insecure: cmd.Flag("insecure").Value.String() == "true",
})
if err != nil {
panic(err)
}
// Marshal the inventory details to JSON
jsonData, err := json.MarshalIndent(systems, "", " ")
if err != nil {
fmt.Println("Error marshalling to JSON:", err)
return
}
// Print the pretty JSON
fmt.Println(string(jsonData))
},
}
func init() {
crawlCmd.Flags().StringP("uri", "u", "", "URI of the BMC")
crawlCmd.Flags().StringP("username", "n", "", "Username for the BMC")
crawlCmd.Flags().StringP("password", "p", "", "Password for the BMC")
crawlCmd.Flags().BoolP("insecure", "i", false, "Ignore SSL errors")
rootCmd.AddCommand(crawlCmd)
}