Skip to content

Commit

Permalink
Format imports and use the auto-format function of my IDE to levelset…
Browse files Browse the repository at this point in the history
… (for now)
  • Loading branch information
rustydb committed Jun 8, 2024
1 parent 0c5ecd9 commit f72af92
Show file tree
Hide file tree
Showing 26 changed files with 489 additions and 153 deletions.
5 changes: 3 additions & 2 deletions cmd/gru/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
46 changes: 34 additions & 12 deletions pkg/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -44,17 +45,29 @@ 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)

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.
Expand All @@ -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,
)
}
}

Expand All @@ -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)
Expand Down
37 changes: 30 additions & 7 deletions pkg/cmd/cli/bios/amd/epyc/rome/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/spf13/viper"
"os"
"path"
"strings"

"github.com/spf13/viper"
)

//go:embed *.json
Expand Down Expand Up @@ -93,19 +94,28 @@ 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
}

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),
),
)
}

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
}
}
32 changes: 23 additions & 9 deletions pkg/cmd/cli/bios/bios.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
}
Expand All @@ -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
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/cli/bios/collections/virtualization.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
65 changes: 51 additions & 14 deletions pkg/cmd/cli/bios/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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,
Expand Down Expand Up @@ -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) {
Expand All @@ -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,
)
}
}

Expand All @@ -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 {

Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit f72af92

Please sign in to comment.