diff --git a/cmd/gru/main.go b/cmd/gru/main.go index 5aff8e2..afef934 100644 --- a/cmd/gru/main.go +++ b/cmd/gru/main.go @@ -27,10 +27,11 @@ package main import ( - "github.com/Cray-HPE/gru/pkg/cmd" - "github.com/Cray-HPE/gru/pkg/cmd/gru" "os" "path/filepath" + + "github.com/Cray-HPE/gru/pkg/cmd" + "github.com/Cray-HPE/gru/pkg/cmd/gru" ) func main() { diff --git a/pkg/auth/auth.go b/pkg/auth/auth.go index 2bde69d..62f8422 100644 --- a/pkg/auth/auth.go +++ b/pkg/auth/auth.go @@ -29,9 +29,10 @@ package auth import ( "fmt" - "github.com/Cray-HPE/gru/pkg/cmd" "github.com/spf13/viper" "github.com/stmcginnis/gofish" + + "github.com/Cray-HPE/gru/pkg/cmd" ) type configuration struct { @@ -44,9 +45,18 @@ var config configuration // LoadConfig loads the applications configuration file and merges it with the environment. func LoadConfig(path string) { - viper.SetDefault("username", "") - viper.SetDefault("password", "") - if viper.BindEnv("password", "IPMI_PASSWORD") != nil { + viper.SetDefault( + "username", + "", + ) + viper.SetDefault( + "password", + "", + ) + if viper.BindEnv( + "password", + "IPMI_PASSWORD", + ) != nil { cmd.CheckError(fmt.Errorf("failed to bind ipmi_password environment variable")) } viper.SetConfigFile(path) @@ -54,7 +64,10 @@ func LoadConfig(path string) { if err := viper.ReadInConfig(); err != nil { if _, ok := err.(viper.ConfigFileNotFoundError); ok { // Config file not found; ignore error if desired - fmt.Printf("Loading config file %s", path) + fmt.Printf( + "Loading config file %s", + path, + ) } else { // Config file was found but another error was produced // TODO: Handle all errors except if the file is missing. @@ -64,7 +77,10 @@ func LoadConfig(path string) { viper.AutomaticEnv() err := viper.Unmarshal(&config) if err != nil { - fmt.Printf("Unable to decode into struct, %v", err) + fmt.Printf( + "Unable to decode into struct, %v", + err, + ) } } @@ -78,14 +94,20 @@ func Connection(host string) (*gofish.APIClient, error) { password := viper.GetString("password") if val, ok := hosts[host]; ok { hostConfig := val.(map[string]interface{}) - username = fmt.Sprintf("%v", hostConfig["username"]) - password = fmt.Sprintf("%v", hostConfig["password"]) + username = fmt.Sprintf( + "%v", + hostConfig["username"], + ) + password = fmt.Sprintf( + "%v", + hostConfig["password"], + ) } config := gofish.ClientConfig{ - Endpoint: "https://" + host, - Username: username, - Password: password, - Insecure: viper.GetBool("insecure"), + Endpoint: "https://" + host, + Username: username, + Password: password, + Insecure: viper.GetBool("insecure"), BasicAuth: true, } c, err := gofish.Connect(config) diff --git a/pkg/cmd/cli/bios/amd/epyc/rome/decoder.go b/pkg/cmd/cli/bios/amd/epyc/rome/decoder.go index bf61436..b868705 100644 --- a/pkg/cmd/cli/bios/amd/epyc/rome/decoder.go +++ b/pkg/cmd/cli/bios/amd/epyc/rome/decoder.go @@ -31,10 +31,11 @@ import ( "encoding/json" "errors" "fmt" - "github.com/spf13/viper" "os" "path" "strings" + + "github.com/spf13/viper" ) //go:embed *.json @@ -93,7 +94,10 @@ func newEmbeddedLibrary(customDir string) (*Library, error) { } for _, file := range builtin { - filePath := path.Join(basePath, file.Name()) + filePath := path.Join( + basePath, + file.Name(), + ) data, err := fs.ReadFile(filePath) if err != nil { return nil, err @@ -101,11 +105,17 @@ func newEmbeddedLibrary(customDir string) (*Library, error) { attribute := Attribute{} - err = json.Unmarshal(data, &attribute) + err = json.Unmarshal( + data, + &attribute, + ) if err != nil { return nil, errors.Join( err, - fmt.Errorf("%+v", string(data)), + fmt.Errorf( + "%+v", + string(data), + ), ) } @@ -121,7 +131,10 @@ func newEmbeddedLibrary(customDir string) (*Library, error) { // RegisterAttribute adds an attribute to the library func (l *Library) RegisterAttribute(attribute Attribute) error { if _, exists := l.Attributes[attribute.AttributeName]; exists { - return fmt.Errorf("%s already exists", attribute.AttributeName) + return fmt.Errorf( + "%s already exists", + attribute.AttributeName, + ) } l.Attributes[attribute.AttributeName] = attribute @@ -136,7 +149,14 @@ func (d DecoderMap) Decode(key string) string { if v.GetBool("json") || v.GetBool("yaml") { key = romeAttr.AttributeName } else { - key = fmt.Sprintf("%s (%s)", romeAttr.AttributeName, strings.TrimLeft(romeAttr.DisplayName, " ")) + key = fmt.Sprintf( + "%s (%s)", + romeAttr.AttributeName, + strings.TrimLeft( + romeAttr.DisplayName, + " ", + ), + ) } } return key @@ -146,7 +166,10 @@ func init() { var err error Map, err = newEmbeddedLibrary("") if err != nil { - fmt.Printf("failed to decode rome attributes:\n%v\n", err) + fmt.Printf( + "failed to decode rome attributes:\n%v\n", + err, + ) os.Exit(1) } } diff --git a/pkg/cmd/cli/bios/bios.go b/pkg/cmd/cli/bios/bios.go index a12e2d7..d38b2fa 100644 --- a/pkg/cmd/cli/bios/bios.go +++ b/pkg/cmd/cli/bios/bios.go @@ -28,15 +28,17 @@ package bios import ( "fmt" - "github.com/Cray-HPE/gru/pkg/auth" - "github.com/Cray-HPE/gru/pkg/cmd/cli/bios/collections" - "github.com/spf13/cobra" - "github.com/stmcginnis/gofish/redfish" - "gopkg.in/yaml.v3" "os" "path/filepath" "regexp" "strings" + + "github.com/spf13/cobra" + "github.com/stmcginnis/gofish/redfish" + "gopkg.in/yaml.v3" + + "github.com/Cray-HPE/gru/pkg/auth" + "github.com/Cray-HPE/gru/pkg/cmd/cli/bios/collections" ) // Settings is a structure for holding current BIOS attributes, pending attributes, and errors. @@ -105,7 +107,10 @@ func makeAttributes(args []string) Settings { var a interface{} for _, attribute := range args { - if key, value, ok := strings.Cut(attribute, "="); ok { + if key, value, ok := strings.Cut( + attribute, + "=", + ); ok { attributes.Attributes[key] = value } } @@ -116,7 +121,10 @@ func makeAttributes(args []string) Settings { return attributes } - err = yaml.Unmarshal(b, &a) + err = yaml.Unmarshal( + b, + &a, + ) if err != nil { attributes.Error = err return attributes @@ -137,9 +145,15 @@ func unmarshalBiosKeyValFile(file string) (settings map[string]interface{}, err fileExtension := filepath.Ext(file) re := regexp.MustCompile(`ya?ml`) if re.Match([]byte(fileExtension)) { - err = yaml.Unmarshal(biosKv, settings) + err = yaml.Unmarshal( + biosKv, + settings, + ) } else { - return settings, fmt.Errorf("invalid filetype: %s", fileExtension) + return settings, fmt.Errorf( + "invalid filetype: %s", + fileExtension, + ) } if err != nil { diff --git a/pkg/cmd/cli/bios/collections/virtualization.go b/pkg/cmd/cli/bios/collections/virtualization.go index c4fe175..0a8df16 100644 --- a/pkg/cmd/cli/bios/collections/virtualization.go +++ b/pkg/cmd/cli/bios/collections/virtualization.go @@ -28,8 +28,9 @@ package collections import ( "fmt" - "github.com/stmcginnis/gofish/redfish" "strings" + + "github.com/stmcginnis/gofish/redfish" ) // Virtualization denotes whether to use this collection. diff --git a/pkg/cmd/cli/bios/get.go b/pkg/cmd/cli/bios/get.go index 40ad3f2..edc0625 100644 --- a/pkg/cmd/cli/bios/get.go +++ b/pkg/cmd/cli/bios/get.go @@ -29,16 +29,18 @@ package bios import ( "encoding/json" "fmt" + "log" + "regexp" + "strings" + + "github.com/spf13/cobra" + "github.com/stmcginnis/gofish/redfish" + "github.com/Cray-HPE/gru/internal/query" "github.com/Cray-HPE/gru/pkg/cmd/cli" "github.com/Cray-HPE/gru/pkg/cmd/cli/bios/collections" - "github.com/spf13/cobra" - "github.com/stmcginnis/gofish/redfish" - "log" - "regexp" "github.com/spf13/viper" - "strings" ) // NewBiosGetCommand creates a `get` subcommand for `bios`. @@ -49,7 +51,10 @@ func NewBiosGetCommand() *cobra.Command { Long: `Gets BIOS attributes`, Run: func(c *cobra.Command, args []string) { hosts := cli.ParseHosts(args) - content := query.Async(getBiosAttributes, hosts) + content := query.Async( + getBiosAttributes, + hosts, + ) cli.PrettyPrint(content) }, Hidden: false, @@ -87,7 +92,10 @@ func getBiosAttributes(host string) interface{} { for decoder := range AttributeDecoderMaps { regex, err := regexp.Compile(AttributeDecoderMaps[decoder].Token) if err != nil { - fmt.Printf("Failed to create decoder regex for: %s", AttributeDecoderMaps[decoder].Token) + fmt.Printf( + "Failed to create decoder regex for: %s", + AttributeDecoderMaps[decoder].Token, + ) continue } if regex.MatchString(systems[0].ProcessorSummary.Model) { @@ -109,21 +117,30 @@ func getBiosAttributes(host string) interface{} { log.Fatal(err) } for k := range attrsFromFile { - requestedAttributes = append(requestedAttributes, k) + requestedAttributes = append( + requestedAttributes, + k, + ) } } else { requestedAttributes = viper.GetStringSlice("attributes") } if v.GetBool("virtualization") { - virtualizationAttributes, err := collections.VirtualizationAttributes(true, systems[0].Manufacturer) + virtualizationAttributes, err := collections.VirtualizationAttributes( + true, + systems[0].Manufacturer, + ) if err != nil { attributes.Error = err return attributes } for key := range virtualizationAttributes { - requestedAttributes = append(requestedAttributes, key) + requestedAttributes = append( + requestedAttributes, + key, + ) } } @@ -140,13 +157,21 @@ func getBiosAttributes(host string) interface{} { decodedAttribute = biosDecoder.Decode(attribute) } if v, exists := bios.Attributes[attribute]; exists { - attributes = updateAttributeMap(attributes, attribute, v, decodedAttribute) + attributes = updateAttributeMap( + attributes, + attribute, + v, + decodedAttribute, + ) } else { attributes.Attributes[attribute] = nil } } if len(attributes.Attributes) == 0 { - attributes.Error = fmt.Errorf("no matching keys found in: %v", requestedAttributes) + attributes.Error = fmt.Errorf( + "no matching keys found in: %v", + requestedAttributes, + ) } } else { @@ -156,7 +181,12 @@ func getBiosAttributes(host string) interface{} { decodedAttribute = biosDecoder.Decode(k) } - attributes = updateAttributeMap(attributes, k, v, decodedAttribute) + attributes = updateAttributeMap( + attributes, + k, + v, + decodedAttribute, + ) } } return attributes @@ -187,7 +217,14 @@ func getPendingBiosAttributes(host string) Settings { Some combos of redfish/bios versions do not actually have this endpoint The library should actually check for this, but this works for now */ - staging := fmt.Sprintf("%s/%s", strings.TrimRight(bios.ODataID, "/"), "Settings") + staging := fmt.Sprintf( + "%s/%s", + strings.TrimRight( + bios.ODataID, + "/", + ), + "Settings", + ) client := bios.GetClient() resp, err := client.Get(staging) if err != nil { diff --git a/pkg/cmd/cli/bios/set.go b/pkg/cmd/cli/bios/set.go index d5d84cd..a85ce3c 100644 --- a/pkg/cmd/cli/bios/set.go +++ b/pkg/cmd/cli/bios/set.go @@ -26,13 +26,15 @@ package bios import ( "fmt" - "github.com/Cray-HPE/gru/internal/set" - "github.com/Cray-HPE/gru/pkg/cmd/cli" - "github.com/Cray-HPE/gru/pkg/cmd/cli/bios/collections" + "os" + "github.com/spf13/cobra" "github.com/spf13/viper" "github.com/stmcginnis/gofish/redfish" - "os" + + "github.com/Cray-HPE/gru/internal/set" + "github.com/Cray-HPE/gru/pkg/cmd/cli" + "github.com/Cray-HPE/gru/pkg/cmd/cli/bios/collections" ) // ClearCmos determines whether to clear the CMOS values. @@ -75,9 +77,16 @@ func NewBiosSetCommand() *cobra.Command { var content map[string]interface{} if v.GetBool("clear-cmos") { - content = set.AsyncCall(resetBios, hosts) + content = set.AsyncCall( + resetBios, + hosts, + ) } else { - content = set.AsyncMap(setBios, hosts, attributes.Attributes) + content = set.AsyncMap( + setBios, + hosts, + attributes.Attributes, + ) } cli.PrettyPrint(content) @@ -108,7 +117,10 @@ func setBios(host string, requestedAttributes map[string]interface{}) interface{ attributes.Attributes = redfish.SettingsAttributes{} if v.GetBool("virtualization") { - attributes.Attributes, err = collections.VirtualizationAttributes(true, systems[0].Manufacturer) + attributes.Attributes, err = collections.VirtualizationAttributes( + true, + systems[0].Manufacturer, + ) if err != nil { attributes.Error = err return attributes diff --git a/pkg/cmd/cli/chassis/boot/boot.go b/pkg/cmd/cli/chassis/boot/boot.go index 388fbc5..44af8f5 100644 --- a/pkg/cmd/cli/chassis/boot/boot.go +++ b/pkg/cmd/cli/chassis/boot/boot.go @@ -28,10 +28,12 @@ package boot import ( "fmt" - "github.com/Cray-HPE/gru/pkg/auth" + "github.com/spf13/cobra" "github.com/spf13/viper" "github.com/stmcginnis/gofish/redfish" + + "github.com/Cray-HPE/gru/pkg/auth" ) // Boot represents boot configuration on the BMC. Only Error is emitted on empty. diff --git a/pkg/cmd/cli/chassis/boot/override.go b/pkg/cmd/cli/chassis/boot/override.go index 96cf7a8..216f8fa 100644 --- a/pkg/cmd/cli/chassis/boot/override.go +++ b/pkg/cmd/cli/chassis/boot/override.go @@ -27,13 +27,14 @@ package boot import ( + "github.com/spf13/cobra" + "github.com/spf13/viper" + "github.com/stmcginnis/gofish/redfish" + "github.com/Cray-HPE/gru/internal/set" "github.com/Cray-HPE/gru/pkg/cmd" "github.com/Cray-HPE/gru/pkg/cmd/cli" "github.com/Cray-HPE/gru/pkg/cmd/cli/chassis/power" - "github.com/spf13/cobra" - "github.com/spf13/viper" - "github.com/stmcginnis/gofish/redfish" ) // NewBiosOverrideCommand creates the `bios` subcommand for `boot`. @@ -49,10 +50,18 @@ func NewBiosOverrideCommand() *cobra.Command { bindErr := v.BindPFlags(c.Flags()) cmd.CheckError(bindErr) - content := set.Async(issueOverride, hosts, redfish.BiosSetupBootSourceOverrideTarget) + content := set.Async( + issueOverride, + hosts, + redfish.BiosSetupBootSourceOverrideTarget, + ) cli.PrettyPrint(content) if v.GetBool("now") { - content = set.Async(power.Issue, hosts, redfish.ForceRestartResetType) + content = set.Async( + power.Issue, + hosts, + redfish.ForceRestartResetType, + ) cli.PrettyPrint(content) } }, @@ -68,7 +77,11 @@ func NewPxeOverrideCommand() *cobra.Command { Long: `Override the next boot with the PXE option`, Run: func(c *cobra.Command, args []string) { hosts := cli.ParseHosts(args) - content := set.Async(issueOverride, hosts, redfish.PxeBootSourceOverrideTarget) + content := set.Async( + issueOverride, + hosts, + redfish.PxeBootSourceOverrideTarget, + ) cli.PrettyPrint(content) }, } @@ -83,7 +96,11 @@ func NewHddOverrideCommand() *cobra.Command { Long: `Override the next boot with the HDD option`, Run: func(c *cobra.Command, args []string) { hosts := cli.ParseHosts(args) - content := set.Async(issueOverride, hosts, redfish.HddBootSourceOverrideTarget) + content := set.Async( + issueOverride, + hosts, + redfish.HddBootSourceOverrideTarget, + ) cli.PrettyPrint(content) }, } @@ -98,7 +115,11 @@ func NewUEFIHttpOverrideCommand() *cobra.Command { Long: `Override the next boot with the HTTP option`, Run: func(c *cobra.Command, args []string) { hosts := cli.ParseHosts(args) - content := set.Async(issueOverride, hosts, redfish.UefiHTTPBootSourceOverrideTarget) + content := set.Async( + issueOverride, + hosts, + redfish.UefiHTTPBootSourceOverrideTarget, + ) cli.PrettyPrint(content) }, } @@ -113,7 +134,11 @@ func NewNoneOverrideCommand() *cobra.Command { Long: `Clears a boot override`, Run: func(c *cobra.Command, args []string) { hosts := cli.ParseHosts(args) - content := set.Async(issueOverride, hosts, redfish.NoneBootSourceOverrideTarget) + content := set.Async( + issueOverride, + hosts, + redfish.NoneBootSourceOverrideTarget, + ) cli.PrettyPrint(content) }, } diff --git a/pkg/cmd/cli/chassis/boot/show.go b/pkg/cmd/cli/chassis/boot/show.go index 3387da2..33f4b00 100644 --- a/pkg/cmd/cli/chassis/boot/show.go +++ b/pkg/cmd/cli/chassis/boot/show.go @@ -29,14 +29,17 @@ package boot import ( "encoding/json" "fmt" - "github.com/Cray-HPE/gru/internal/query" - "github.com/stmcginnis/gofish/redfish" "io" "strings" + "github.com/stmcginnis/gofish/redfish" + + "github.com/Cray-HPE/gru/internal/query" + + "github.com/spf13/cobra" + "github.com/Cray-HPE/gru/pkg/auth" "github.com/Cray-HPE/gru/pkg/cmd/cli" - "github.com/spf13/cobra" ) // NewShowCommand creates the `boot` subcommand for `show`. @@ -47,7 +50,10 @@ func NewShowCommand() *cobra.Command { Long: `Show the current BootOrder; BootNext, networkRetry, and more`, Run: func(c *cobra.Command, args []string) { hosts := cli.ParseHosts(args) - content := query.Async(getBootInformation, hosts) + content := query.Async( + getBootInformation, + hosts, + ) cli.PrettyPrint(content) }, } @@ -72,7 +78,14 @@ func getBootInformation(host string) interface{} { return boot } - bo := fmt.Sprintf("%s/%s", strings.TrimRight(systems[0].ODataID, "/"), "BootOptions") + bo := fmt.Sprintf( + "%s/%s", + strings.TrimRight( + systems[0].ODataID, + "/", + ), + "BootOptions", + ) client := systems[0].GetClient() resp, err := client.Get(bo) @@ -87,7 +100,14 @@ func getBootInformation(host string) interface{} { } for _, b := range systems[0].Boot.BootOrder { - ep := fmt.Sprintf("%s/%s", bo, strings.TrimPrefix(b, "Boot")) + ep := fmt.Sprintf( + "%s/%s", + bo, + strings.TrimPrefix( + b, + "Boot", + ), + ) resp, err := client.Get(ep) if err != nil { boot.Error = err @@ -100,11 +120,17 @@ func getBootInformation(host string) interface{} { return boot } - boot.Order = append(boot.Order, strings.TrimSpace(names["Description"].(string))) + boot.Order = append( + boot.Order, + strings.TrimSpace(names["Description"].(string)), + ) } } else { - bo = strings.TrimRight(systems[0].ODataID, "/") + bo = strings.TrimRight( + systems[0].ODataID, + "/", + ) response, err := client.Get(bo) if err != nil { boot.Error = err @@ -113,7 +139,10 @@ func getBootInformation(host string) interface{} { names := make(map[string]interface{}) body, err := io.ReadAll(response.Body) - err = json.Unmarshal(body, &names) + err = json.Unmarshal( + body, + &names, + ) if err != nil { boot.Error = err return boot @@ -133,7 +162,10 @@ func getBootInformation(host string) interface{} { } for _, v := range bootMap.BootOrder { - boot.Order = append(boot.Order, strings.TrimSpace(v)) + boot.Order = append( + boot.Order, + strings.TrimSpace(v), + ) } } diff --git a/pkg/cmd/cli/chassis/chassis.go b/pkg/cmd/cli/chassis/chassis.go index ec0abad..b07775b 100644 --- a/pkg/cmd/cli/chassis/chassis.go +++ b/pkg/cmd/cli/chassis/chassis.go @@ -27,9 +27,10 @@ package chassis import ( + "github.com/spf13/cobra" + "github.com/Cray-HPE/gru/pkg/cmd/cli/chassis/boot" "github.com/Cray-HPE/gru/pkg/cmd/cli/chassis/power" - "github.com/spf13/cobra" ) // NewCommand creates the `chassis` subcommand. diff --git a/pkg/cmd/cli/chassis/power/cycle.go b/pkg/cmd/cli/chassis/power/cycle.go index 43b1162..d736916 100644 --- a/pkg/cmd/cli/chassis/power/cycle.go +++ b/pkg/cmd/cli/chassis/power/cycle.go @@ -28,18 +28,20 @@ package power import ( "fmt" - "github.com/Cray-HPE/gru/internal/set" - "github.com/Cray-HPE/gru/pkg/cmd" - "github.com/Cray-HPE/gru/pkg/cmd/cli" + "github.com/spf13/cobra" "github.com/spf13/viper" "github.com/stmcginnis/gofish/redfish" + + "github.com/Cray-HPE/gru/internal/set" + "github.com/Cray-HPE/gru/pkg/cmd" + "github.com/Cray-HPE/gru/pkg/cmd/cli" ) // NewPowerCycleCommand creates the `cycle` subcommand for `power`. func NewPowerCycleCommand() *cobra.Command { c := &cobra.Command{ - Use: "cycle host [...host]", + Use: "cycle host [...host]", Short: "Power cycle the target machine(s)", Long: `Performs an ACPI shutdown and startup to power cycle the target machine(s). Also allows bypassing the OS shutdown, forcing a warm boot.`, @@ -57,7 +59,11 @@ Also allows bypassing the OS shutdown, forcing a warm boot.`, resetType = redfish.ForceRestartResetType } - content := set.Async(Issue, hosts, resetType) + content := set.Async( + Issue, + hosts, + resetType, + ) cli.PrettyPrint(content) }, Hidden: false, diff --git a/pkg/cmd/cli/chassis/power/nmi.go b/pkg/cmd/cli/chassis/power/nmi.go index 9249ad1..4b30b97 100644 --- a/pkg/cmd/cli/chassis/power/nmi.go +++ b/pkg/cmd/cli/chassis/power/nmi.go @@ -27,21 +27,26 @@ package power import ( - "github.com/Cray-HPE/gru/internal/set" - "github.com/Cray-HPE/gru/pkg/cmd/cli" "github.com/spf13/cobra" "github.com/stmcginnis/gofish/redfish" + + "github.com/Cray-HPE/gru/internal/set" + "github.com/Cray-HPE/gru/pkg/cmd/cli" ) // NewPowerNMICommand creates the `nmi` subcommand for `power`. func NewPowerNMICommand() *cobra.Command { c := &cobra.Command{ - Use: "nmi host [...host]", + Use: "nmi host [...host]", Short: "Issue an NMI to the target machine(s)", - Long: `Issue a non-maskable interrupt, triggering a crash/core dump`, + Long: `Issue a non-maskable interrupt, triggering a crash/core dump`, Run: func(c *cobra.Command, args []string) { hosts := cli.ParseHosts(args) - content := set.Async(Issue, hosts, redfish.NmiResetType) + content := set.Async( + Issue, + hosts, + redfish.NmiResetType, + ) cli.PrettyPrint(content) }, Hidden: false, diff --git a/pkg/cmd/cli/chassis/power/off.go b/pkg/cmd/cli/chassis/power/off.go index fa6161f..cb6acc8 100644 --- a/pkg/cmd/cli/chassis/power/off.go +++ b/pkg/cmd/cli/chassis/power/off.go @@ -28,12 +28,14 @@ package power import ( "fmt" - "github.com/Cray-HPE/gru/internal/set" - "github.com/Cray-HPE/gru/pkg/cmd" - "github.com/Cray-HPE/gru/pkg/cmd/cli" + "github.com/spf13/cobra" "github.com/spf13/viper" "github.com/stmcginnis/gofish/redfish" + + "github.com/Cray-HPE/gru/internal/set" + "github.com/Cray-HPE/gru/pkg/cmd" + "github.com/Cray-HPE/gru/pkg/cmd/cli" ) // NewPowerOffCommand creates the `off` subcommand for `power`. @@ -61,7 +63,11 @@ as well as a power-button emulated shutdown.`, resetType = redfish.PushPowerButtonResetType } - content := set.Async(Issue, hosts, resetType) + content := set.Async( + Issue, + hosts, + resetType, + ) cli.PrettyPrint(content) }, } @@ -79,6 +85,9 @@ as well as a power-button emulated shutdown.`, false, "Emulate a power-button press", ) - c.MarkFlagsMutuallyExclusive("button", "force") + c.MarkFlagsMutuallyExclusive( + "button", + "force", + ) return c } diff --git a/pkg/cmd/cli/chassis/power/on.go b/pkg/cmd/cli/chassis/power/on.go index 4d0e6d4..25bbe66 100644 --- a/pkg/cmd/cli/chassis/power/on.go +++ b/pkg/cmd/cli/chassis/power/on.go @@ -27,21 +27,26 @@ package power import ( - "github.com/Cray-HPE/gru/internal/set" - "github.com/Cray-HPE/gru/pkg/cmd/cli" "github.com/spf13/cobra" "github.com/stmcginnis/gofish/redfish" + + "github.com/Cray-HPE/gru/internal/set" + "github.com/Cray-HPE/gru/pkg/cmd/cli" ) // NewPowerOnCommand creates the `on` subcommand for `power`. func NewPowerOnCommand() *cobra.Command { c := &cobra.Command{ - Use: "on host [...host]", + Use: "on host [...host]", Short: "Power on the target machine(s)", - Long: `Powers on the target machines (cold boot)`, + Long: `Powers on the target machines (cold boot)`, Run: func(c *cobra.Command, args []string) { hosts := cli.ParseHosts(args) - content := set.Async(Issue, hosts, redfish.OnResetType) + content := set.Async( + Issue, + hosts, + redfish.OnResetType, + ) cli.PrettyPrint(content) }, } diff --git a/pkg/cmd/cli/chassis/power/power.go b/pkg/cmd/cli/chassis/power/power.go index 9d3ab97..dde20a2 100644 --- a/pkg/cmd/cli/chassis/power/power.go +++ b/pkg/cmd/cli/chassis/power/power.go @@ -27,9 +27,10 @@ package power import ( - "github.com/Cray-HPE/gru/pkg/auth" "github.com/spf13/cobra" "github.com/stmcginnis/gofish/redfish" + + "github.com/Cray-HPE/gru/pkg/auth" ) // NewCommand creates the `power` subcommand for `chassis`. diff --git a/pkg/cmd/cli/chassis/power/reset.go b/pkg/cmd/cli/chassis/power/reset.go index 1204fcf..f362340 100644 --- a/pkg/cmd/cli/chassis/power/reset.go +++ b/pkg/cmd/cli/chassis/power/reset.go @@ -27,21 +27,26 @@ package power import ( - "github.com/Cray-HPE/gru/internal/set" - "github.com/Cray-HPE/gru/pkg/cmd/cli" "github.com/spf13/cobra" "github.com/stmcginnis/gofish/redfish" + + "github.com/Cray-HPE/gru/internal/set" + "github.com/Cray-HPE/gru/pkg/cmd/cli" ) // NewPowerResetCommand creates the `reset` subcommand for `power`. func NewPowerResetCommand() *cobra.Command { c := &cobra.Command{ - Use: "reset host [...host]", + Use: "reset host [...host]", Short: "Power reset the target machine(s)", - Long: `Forcefully restart the target machine(s) without a graceful shutdown`, + Long: `Forcefully restart the target machine(s) without a graceful shutdown`, Run: func(c *cobra.Command, args []string) { hosts := cli.ParseHosts(args) - content := set.Async(Issue, hosts, redfish.ForceRestartResetType) + content := set.Async( + Issue, + hosts, + redfish.ForceRestartResetType, + ) cli.PrettyPrint(content) }, Hidden: false, diff --git a/pkg/cmd/cli/chassis/power/status.go b/pkg/cmd/cli/chassis/power/status.go index 546144e..b0ea03d 100644 --- a/pkg/cmd/cli/chassis/power/status.go +++ b/pkg/cmd/cli/chassis/power/status.go @@ -27,21 +27,25 @@ package power import ( + "github.com/spf13/cobra" + "github.com/Cray-HPE/gru/internal/query" "github.com/Cray-HPE/gru/pkg/auth" "github.com/Cray-HPE/gru/pkg/cmd/cli" - "github.com/spf13/cobra" ) // NewPowerStatusCommand creates the `status` subcommand for `power`. func NewPowerStatusCommand() *cobra.Command { c := &cobra.Command{ - Use: "status host [...host]", + Use: "status host [...host]", Short: "Power status for the target machine(s)", - Long: `Prints the current power status reported by the blade management controller for the target machine(s)`, + Long: `Prints the current power status reported by the blade management controller for the target machine(s)`, Run: func(c *cobra.Command, args []string) { hosts := cli.ParseHosts(args) - content := query.Async(status, hosts) + content := query.Async( + status, + hosts, + ) cli.PrettyPrint(content) }, Hidden: false, diff --git a/pkg/cmd/cli/cli.go b/pkg/cmd/cli/cli.go index bf53f48..f916e63 100644 --- a/pkg/cmd/cli/cli.go +++ b/pkg/cmd/cli/cli.go @@ -108,17 +108,27 @@ func ParseHosts(args []string) []string { // Count the words. count := 0 for scanner.Scan() { - newArgs = append(newArgs, scanner.Text()) + newArgs = append( + newArgs, + scanner.Text(), + ) count++ } if err := scanner.Err(); err != nil { - fmt.Fprintln(os.Stderr, "reading input:", err) + fmt.Fprintln( + os.Stderr, + "reading input:", + err, + ) } return newArgs } if len(args) < 1 { err := fmt.Errorf("no hosts given") - if _, exc := fmt.Fprintln(os.Stderr, err); exc != nil { + if _, exc := fmt.Fprintln( + os.Stderr, + err, + ); exc != nil { panic(exc) } diff --git a/pkg/cmd/cli/print.go b/pkg/cmd/cli/print.go index 0de7130..59f55bb 100644 --- a/pkg/cmd/cli/print.go +++ b/pkg/cmd/cli/print.go @@ -29,25 +29,39 @@ package cli import ( "encoding/json" "fmt" - "github.com/spf13/viper" - "gopkg.in/yaml.v3" "reflect" "sort" + + "github.com/spf13/viper" + "gopkg.in/yaml.v3" ) // keyValuePrint is a print helper for formatting key-value pairs. func keyValuePrint(key string, value any, indent string) { - fmt.Printf("%s%-60s: %-60v\n", indent, key, value) + fmt.Printf( + "%s%-60s: %-60v\n", + indent, + key, + value, + ) } // keyPrint is a print helper for printing just a key in anticipation of printing a data structure as a value. func keyPrint(key string, indent string) { - fmt.Printf("%s%s:\n", indent, key) + fmt.Printf( + "%s%s:\n", + indent, + key, + ) } // sliceElementPrint is a print helper for printing values belonging to an array in a marked up format. func sliceElementPrint(value any, indent string) { - fmt.Printf("%s%-60v\n", indent, value) + fmt.Printf( + "%s%-60v\n", + indent, + value, + ) } // slicePrint is a print helper for printing a slice of interfaces. @@ -56,7 +70,13 @@ func slicePrint(value reflect.Value) { for i := 0; i < value.Len(); i++ { sv := reflect.ValueOf(value.Index(i).Interface()) - keyPrint(fmt.Sprintf("%d", i), "\t") + keyPrint( + fmt.Sprintf( + "%d", + i, + ), + "\t", + ) for k := 0; k < sv.NumField(); k++ { @@ -72,13 +92,20 @@ func slicePrint(value reflect.Value) { if kind == reflect.Slice { - keyPrint(typeOfS.Field(k).Name, "\t") + keyPrint( + typeOfS.Field(k).Name, + "\t", + ) // FIXME: This does not increase indent level. slicePrint(sv.Field(k)) } else { - keyValuePrint(typeOfS.Field(k).Name, sv.Field(k), "\t\t") + keyValuePrint( + typeOfS.Field(k).Name, + sv.Field(k), + "\t\t", + ) } } } @@ -101,7 +128,10 @@ func structPrint(value reflect.Value) { if len(keys) != 0 { - keyPrint(typeOfS.Field(i).Name, "\t") + keyPrint( + typeOfS.Field(i).Name, + "\t", + ) } else { @@ -109,11 +139,18 @@ func structPrint(value reflect.Value) { } - sortedKeys := make([]string, 0, len(keys)) + sortedKeys := make( + []string, + 0, + len(keys), + ) for key := range keys { - sortedKeys = append(sortedKeys, keys[key].String()) + sortedKeys = append( + sortedKeys, + keys[key].String(), + ) } @@ -121,17 +158,27 @@ func structPrint(value reflect.Value) { for key := range sortedKeys { - keyValuePrint(sortedKeys[key], value.Field(i).MapIndex(reflect.ValueOf(sortedKeys[key])), "\t\t") + keyValuePrint( + sortedKeys[key], + value.Field(i).MapIndex(reflect.ValueOf(sortedKeys[key])), + "\t\t", + ) } } else if _, ok := value.Field(i).Interface().([]string); ok { - keyPrint(typeOfS.Field(i).Name, "\t") + keyPrint( + typeOfS.Field(i).Name, + "\t", + ) for _, v := range value.Field(i).Interface().([]string) { - sliceElementPrint(v, "\t\t") + sliceElementPrint( + v, + "\t\t", + ) } @@ -147,7 +194,11 @@ func structPrint(value reflect.Value) { } - keyValuePrint(typeOfS.Field(i).Name, value.Field(i).Interface(), "\t") + keyValuePrint( + typeOfS.Field(i).Name, + value.Field(i).Interface(), + "\t", + ) } } @@ -157,27 +208,54 @@ func structPrint(value reflect.Value) { func PrettyPrint(content map[string]interface{}) { if viper.GetBool("json") { - JSON, err := json.MarshalIndent(content, "", " ") + JSON, err := json.MarshalIndent( + content, + "", + " ", + ) if err != nil { - panic(fmt.Errorf("could not create valid JSON from %v", content)) + panic( + fmt.Errorf( + "could not create valid JSON from %v", + content, + ), + ) } - fmt.Printf("%s\n", string(JSON)) + fmt.Printf( + "%s\n", + string(JSON), + ) } else if viper.GetBool("yaml") { YAML, err := yaml.Marshal(content) if err != nil { - panic(fmt.Errorf("could not create valid YAML from %v", content)) + panic( + fmt.Errorf( + "could not create valid YAML from %v", + content, + ), + ) } - fmt.Printf("%s\n", string(YAML)) + fmt.Printf( + "%s\n", + string(YAML), + ) } else { - keys := make([]string, 0, len(content)) + keys := make( + []string, + 0, + len(content), + ) for k := range content { - keys = append(keys, k) + keys = append( + keys, + k, + ) } @@ -185,7 +263,10 @@ func PrettyPrint(content map[string]interface{}) { for _, k := range keys { - fmt.Printf("%s:\n", k) + fmt.Printf( + "%s:\n", + k, + ) // Warning; the struct fields must be exported! s := content[k] diff --git a/pkg/cmd/cli/proc/show.go b/pkg/cmd/cli/proc/show.go index 91c7453..791330b 100644 --- a/pkg/cmd/cli/proc/show.go +++ b/pkg/cmd/cli/proc/show.go @@ -28,22 +28,27 @@ package proc import ( "fmt" + "strings" + + "github.com/spf13/cobra" + "github.com/Cray-HPE/gru/internal/query" "github.com/Cray-HPE/gru/pkg/auth" "github.com/Cray-HPE/gru/pkg/cmd/cli" - "github.com/spf13/cobra" - "strings" ) // NewShowCommand creates the `system` subcommand for `show`. func NewShowCommand() *cobra.Command { c := &cobra.Command{ - Use: "proc host [...host]", + Use: "proc host [...host]", Short: "Processor information", - Long: `Show the Server's processors, a full list with their core count, model, architecture, and serial numbers.`, + Long: `Show the Server's processors, a full list with their core count, model, architecture, and serial numbers.`, Run: func(c *cobra.Command, args []string) { hosts := cli.ParseHosts(args) - content := query.Async(getProcessors, hosts) + content := query.Async( + getProcessors, + hosts, + ) cli.PrettyPrint(content) }, } @@ -55,7 +60,8 @@ func getProcessors(host string) interface{} { c, err := auth.Connection(host) if err != nil { foundProcessors = append( - foundProcessors, Processor{ + foundProcessors, + Processor{ Error: err, }, ) @@ -67,7 +73,8 @@ func getProcessors(host string) interface{} { managers, err := service.Managers() if err != nil || len(managers) < 1 { foundProcessors = append( - foundProcessors, Processor{ + foundProcessors, + Processor{ Error: err, }, ) @@ -77,7 +84,8 @@ func getProcessors(host string) interface{} { systems, err := service.Systems() if err != nil || len(systems) < 1 { foundProcessors = append( - foundProcessors, Processor{ + foundProcessors, + Processor{ Error: err, }, ) @@ -86,21 +94,30 @@ func getProcessors(host string) interface{} { systemProcessors, err := systems[0].Processors() if err != nil { foundProcessors = append( - foundProcessors, Processor{ + foundProcessors, + Processor{ Error: err, }, ) } else { for i := range systemProcessors { processor := Processor{ - Architecture: strings.TrimSpace(fmt.Sprintf("%v", systemProcessors[i].ProcessorArchitecture)), + Architecture: strings.TrimSpace( + fmt.Sprintf( + "%v", + systemProcessors[i].ProcessorArchitecture, + ), + ), TotalCores: systemProcessors[i].TotalCores, Model: strings.TrimSpace(systemProcessors[i].Model), Socket: strings.TrimSpace(systemProcessors[i].Socket), Threads: systemProcessors[i].TotalThreads, VendorID: strings.TrimSpace(systemProcessors[i].ProcessorID.VendorID), } - foundProcessors = append(foundProcessors, processor) + foundProcessors = append( + foundProcessors, + processor, + ) } } return foundProcessors diff --git a/pkg/cmd/cli/show/show.go b/pkg/cmd/cli/show/show.go index 3ade3e4..a4a611b 100644 --- a/pkg/cmd/cli/show/show.go +++ b/pkg/cmd/cli/show/show.go @@ -27,10 +27,11 @@ package show import ( + "github.com/spf13/cobra" + "github.com/Cray-HPE/gru/pkg/cmd/cli/chassis/boot" "github.com/Cray-HPE/gru/pkg/cmd/cli/proc" "github.com/Cray-HPE/gru/pkg/cmd/cli/system" - "github.com/spf13/cobra" ) // NewCommand creates the `show` subcommand. diff --git a/pkg/cmd/cli/system/show.go b/pkg/cmd/cli/system/show.go index 7bc8ed4..0af733e 100644 --- a/pkg/cmd/cli/system/show.go +++ b/pkg/cmd/cli/system/show.go @@ -27,22 +27,27 @@ package system import ( + "strings" + + "github.com/spf13/cobra" + "github.com/Cray-HPE/gru/internal/query" "github.com/Cray-HPE/gru/pkg/auth" "github.com/Cray-HPE/gru/pkg/cmd/cli" - "github.com/spf13/cobra" - "strings" ) // NewShowCommand creates the `system` subcommand for `show`. func NewShowCommand() *cobra.Command { c := &cobra.Command{ - Use: "system host [...host]", + Use: "system host [...host]", Short: "System information", - Long: `Show the Server Manufacturer, Server Model, System Version, and Firmware Version for the given server(s)`, + Long: `Show the Server Manufacturer, Server Model, System Version, and Firmware Version for the given server(s)`, Run: func(c *cobra.Command, args []string) { hosts := cli.ParseHosts(args) - content := query.Async(getSystemInformation, hosts) + content := query.Async( + getSystemInformation, + hosts, + ) cli.PrettyPrint(content) }, } diff --git a/pkg/cmd/errors.go b/pkg/cmd/errors.go index f9c65fa..2068d5d 100644 --- a/pkg/cmd/errors.go +++ b/pkg/cmd/errors.go @@ -37,7 +37,11 @@ import ( func CheckError(err error) { if err != nil { if err != context.Canceled { - fmt.Fprintf(os.Stderr, "An error occurred: %v\n", err) + fmt.Fprintf( + os.Stderr, + "An error occurred: %v\n", + err, + ) } os.Exit(1) } diff --git a/pkg/cmd/gru/gru.go b/pkg/cmd/gru/gru.go index e415bf4..faab8fc 100644 --- a/pkg/cmd/gru/gru.go +++ b/pkg/cmd/gru/gru.go @@ -28,14 +28,16 @@ package gru import ( "fmt" + + "github.com/spf13/cobra" + "github.com/spf13/viper" + "github.com/Cray-HPE/gru/pkg/auth" "github.com/Cray-HPE/gru/pkg/cmd" "github.com/Cray-HPE/gru/pkg/cmd/cli/bios" "github.com/Cray-HPE/gru/pkg/cmd/cli/chassis" "github.com/Cray-HPE/gru/pkg/cmd/cli/show" "github.com/Cray-HPE/gru/pkg/version" - "github.com/spf13/cobra" - "github.com/spf13/viper" ) // NewCommand creates the main command for `gru`. @@ -43,7 +45,10 @@ func NewCommand(name string) *cobra.Command { c := &cobra.Command{ Use: name, TraverseChildren: true, - Short: fmt.Sprintf("Go Redfish Utility (%s)", name), + Short: fmt.Sprintf( + "Go Redfish Utility (%s)", + name, + ), Long: fmt.Sprintf( ` %[1]s is a tool for interacting with Redfish devices. %[1]s provides a @@ -57,7 +62,8 @@ authenticating with the target RedFish endpoints. Set USERNAME and PASSWORD (or IPMI_PASSWORD) with the target's credentials, or provide these via a YAML file. Optionally, if the target hosts have different credentials the YAML file may provide these per host. -`, name, +`, + name, ), Version: version.Version(), PersistentPreRun: func(c *cobra.Command, args []string) { @@ -71,7 +77,10 @@ the YAML file may provide these per host. c.PersistentFlags().StringP( "config", "c", - fmt.Sprintf("./%s.yml", name), + fmt.Sprintf( + "./%s.yml", + name, + ), fmt.Sprintln( "Configuration file containing BMC credentials, necessary if USERNAME and PASSWORD are not defined in the environment", name, diff --git a/pkg/version/version.go b/pkg/version/version.go index b3aa528..c289ebe 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -52,7 +52,11 @@ var ( // Version returns the one-line version of this application. func Version() string { if GitTreeState != "clean" { - return fmt.Sprintf("%s-%s", GitTag, GitTreeState) + return fmt.Sprintf( + "%s-%s", + GitTag, + GitTreeState, + ) } return GitTag }