Skip to content

Commit

Permalink
Fixes #50 Adds --yaml option
Browse files Browse the repository at this point in the history
Even though the default output looks similar to YAML, it is not
marshalled as YAML and thus shouldn't be parsed by YAML parsers since it
is unsafe.

This adds an official YAML output option, which also updates several
keys to match YAML naming conventions (snake case).
  • Loading branch information
rustydb committed Mar 5, 2024
1 parent 9d8b097 commit 5d0bd1b
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 29 deletions.
18 changes: 9 additions & 9 deletions pkg/cmd/cli/bios/amd/epyc/rome/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,19 @@ type Library struct {

// Attribute is a single bios attribute
type Attribute struct {
AttributeName string `json:"AttributeName"`
DefaultValue interface{} `json:"DefaultValue"` // can be int or string, maybe bool
DisplayName string `json:"DisplayName"`
HelpText string `json:"HelpText"`
ReadOnly bool `json:"ReadOnly"`
Type string `json:"Type"`
Value []Value `json:"Value"`
AttributeName string `json:"AttributeName" yaml:"attribute_name"`
DefaultValue interface{} `json:"DefaultValue" yaml:"default_value"` // can be int or string, maybe bool
DisplayName string `json:"DisplayName" yaml:"display_name"`
HelpText string `json:"HelpText" yaml:"help_text"`
ReadOnly bool `json:"ReadOnly" yaml:"read_only"`
Type string `json:"Type" yaml:"type"`
Value []Value `json:"Value" yaml:"value"`
}

// Value is the display name and a name
type Value struct {
ValueDisplayName string `json:"ValueDisplayName"`
ValueName string `json:"ValueName"`
ValueDisplayName string `json:"ValueDisplayName" yaml:"value_display_name"`
ValueName string `json:"ValueName" yaml:"value_name"`
}

// newEmbeddedLibrary embeds JSON files from: sh control.Rome.BiosParameters.sh <BMC> renew_json
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/cli/bios/bios.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ import (

// Settings is a structure for holding current BIOS attributes, pending attributes, and errors.
type Settings struct {
Attributes map[string]interface{} `json:"attributes,omitempty"`
Pending map[string]interface{} `json:"pending,omitempty"`
Error error `json:"error,omitempty"`
Attributes map[string]interface{} `json:"attributes,omitempty" yaml:"attributes,omitempty"`
Pending map[string]interface{} `json:"pending,omitempty" yaml:"pending,omitempty"`
Error error `json:"error,omitempty" yaml:"error,omitempty"`
}

// Attributes are an array of attribute names (and optionally values).
Expand Down
10 changes: 5 additions & 5 deletions pkg/cmd/cli/chassis/boot/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ import (

// Boot represents boot configuration on the BMC. Only Error is emitted on empty.
type Boot struct {
Order []string `json:"order,omitempty"`
Next string `json:"next,omitempty"`
Error error `json:"error,omitempty"`
Order []string `json:"order,omitempty" yaml:"order,omitempty"`
Next string `json:"next,omitempty" yaml:"next,omitempty"`
Error error `json:"error,omitempty" yaml:"error,omitempty"`
}

// Override represents the result of the boot override.
type Override struct {
Target redfish.BootSourceOverrideTarget `json:"target"`
Error error `json:"error,omitempty"`
Target redfish.BootSourceOverrideTarget `json:"target" yaml:"target"`
Error error `json:"error,omitempty" yaml:"error,omitempty"`
}

// NewCommand creates the `boot` subcommand for `chassis`.
Expand Down
10 changes: 5 additions & 5 deletions pkg/cmd/cli/chassis/power/power.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ func NewCommand() *cobra.Command {

// StateChange represents a change in power states.
type StateChange struct {
PreviousPowerState redfish.PowerState `json:"previousPowerState,omitempty"`
RequestedPowerState redfish.ResetType `json:"requestedPowerState,omitempty"`
Error error `json:"error,omitempty"`
PreviousPowerState redfish.PowerState `json:"previousPowerState,omitempty" yaml:"previous_power_state,omitempty"`
RequestedPowerState redfish.ResetType `json:"requestedPowerState,omitempty" yaml:"requested_power_state,omitempty"`
Error error `json:"error,omitempty" yaml:"error,omitempty"`
}

// State represents a single power state.
type State struct {
PowerState redfish.PowerState `json:"powerState"`
Error error `json:"error,omitempty"`
PowerState redfish.PowerState `json:"powerState" yaml:"power_state"`
Error error `json:"error,omitempty" yaml:"error,omitempty"`
}

// Issue issues an action against a host.
Expand Down
7 changes: 7 additions & 0 deletions pkg/cmd/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"bufio"
"encoding/json"
"fmt"
"gopkg.in/yaml.v3"
"os"
"reflect"
"sort"
Expand Down Expand Up @@ -140,6 +141,12 @@ func MapPrint(content map[string]interface{}) {
panic(fmt.Errorf("could not create valid JSON from %v", content))
}
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))
}
fmt.Printf("%s\n", string(YAML))
} else {
keys := make([]string, 0, len(content))
for k := range content {
Expand Down
12 changes: 6 additions & 6 deletions pkg/cmd/cli/system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ package system

// System represents system meta from the BMC. Only Error is omitted on empty.
type System struct {
BIOSVersion string `json:"biosVersion"`
FirmwareVersion string `json:"firmwareVersion"`
ProcessorModel string `json:"processorModel"`
Manufacturer string `json:"manufacturer"`
Model string `json:"model"`
Error error `json:"error,omitempty"`
BIOSVersion string `json:"biosVersion" yaml:"bios_version"`
FirmwareVersion string `json:"firmwareVersion" yaml:"firmware_version"`
ProcessorModel string `json:"processorModel" yaml:"processor_model"`
Manufacturer string `json:"manufacturer" yaml:"manufacturer"`
Model string `json:"model" yaml:"model"`
Error error `json:"error,omitempty" yaml:"error,omitempty"`
}
8 changes: 7 additions & 1 deletion pkg/cmd/gru/gru.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,13 @@ the YAML file may provide these per host.
"json",
"j",
false,
"Output results in JSON",
"Output in JSON",
)
c.PersistentFlags().BoolP(
"yaml",
"y",
false,
"Output in YAML",
)
c.AddCommand(
bios.NewCommand(),
Expand Down

0 comments on commit 5d0bd1b

Please sign in to comment.