chore: updated utility functions

This commit is contained in:
David Allen 2025-05-01 23:25:27 -06:00
parent 040e9ac808
commit 5e3332e080
Signed by: towk
GPG key ID: 0430CDBE22619155
5 changed files with 157 additions and 4 deletions

View file

@ -13,7 +13,7 @@ func GetBMCCredentials(store secrets.SecretStore, id string) bmc.BMCCredentials
)
if id == "" {
log.Error().Msg("failed to get BMC credentials: id was empty")
log.Error().Msg("failed to get BMC credentials: ID was empty")
return creds
}
@ -36,11 +36,11 @@ func GetBMCCredentials(store secrets.SecretStore, id string) bmc.BMCCredentials
log.Warn().Str("id", id).Err(err).Msg("no default credentials were set, they will be blank unless overridden by CLI flags")
} else {
// Default credentials found, use them.
log.Info().Str("id", id).Msg("default credentials found, using")
log.Info().Str("id", id).Msg("using default credentials found")
creds = defaultSecret
}
} else {
log.Info().Str("id", id).Msg("specific credentials found, using")
log.Info().Str("id", id).Msg("using specific credentials found")
}
return creds

View file

@ -1,6 +1,8 @@
package util
import "fmt"
import (
"fmt"
)
// FormatErrorList() is a wrapper function that unifies error list formatting
// and makes printing error lists consistent.

25
internal/util/print.go Normal file
View file

@ -0,0 +1,25 @@
package util
import (
"encoding/json"
"fmt"
"github.com/rs/zerolog/log"
"gopkg.in/yaml.v2"
)
func PrintJSON(data any) {
b, err := json.MarshalIndent(data, "", " ")
if err != nil {
log.Error().Err(err).Msgf("failed to marshal scanned results to JSON")
}
fmt.Println(string(b))
}
func PrintYAML(data any) {
b, err := yaml.Marshal(data)
if err != nil {
log.Error().Err(err).Msgf("failed to marshal scanned results to JSON")
}
fmt.Printf("%s\n", string(b))
}

10
internal/util/strings.go Normal file
View file

@ -0,0 +1,10 @@
package util
import "strings"
func TidyJSON(s string) string {
s = strings.ReplaceAll(s, "\n", "")
s = strings.ReplaceAll(s, "\t", "")
s = strings.ReplaceAll(s, " ", "")
return strings.ReplaceAll(s, "\"", "'")
}