Skip to content

Commit

Permalink
Add --yaml output option (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
rustydb authored Mar 8, 2024
2 parents f30e1ae + 2936093 commit 1bb33d3
Show file tree
Hide file tree
Showing 13 changed files with 222 additions and 63 deletions.
9 changes: 2 additions & 7 deletions internal/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@
package query

import (
"os"
"fmt"
"os"
"sync"

"github.com/spf13/viper"
)

// Async runs an async query (fn) against a list of hosts. Returns a map of each host with their
Expand All @@ -43,10 +41,7 @@ func Async(fn func(host string) interface{}, hosts []string) map[string]any {
sliceLength := len(hosts)
wg.Add(sliceLength)

v := viper.GetViper()
if !v.GetBool("json") {
fmt.Fprintf(os.Stderr, "Asynchronously querying [%5d] hosts ... \n", len(hosts))
}
fmt.Fprintf(os.Stderr, "Asynchronously querying [%5d] hosts ... \n", len(hosts))
sm := make(map[string]interface{})

for _, host := range hosts {
Expand Down
18 changes: 4 additions & 14 deletions internal/set/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@
package set

import (
"os"
"fmt"
"github.com/spf13/viper"
"os"
"sync"
)

Expand All @@ -47,10 +46,7 @@ func Async(
sliceLength := len(hosts)
wg.Add(sliceLength)

v := viper.GetViper()
if !v.GetBool("json") {
fmt.Fprintf(os.Stderr, "Asynchronously updating [%5d] hosts ... \n", len(hosts))
}
fmt.Fprintf(os.Stderr, "Asynchronously updating [%5d] hosts ... \n", len(hosts))

sm := make(map[string]interface{})

Expand Down Expand Up @@ -80,10 +76,7 @@ func AsyncMap(
sliceLength := len(hosts)
wg.Add(sliceLength)

v := viper.GetViper()
if !v.GetBool("json") {
fmt.Fprintf(os.Stderr, "Asynchronously updating [%5d] hosts ... \n", len(hosts))
}
fmt.Fprintf(os.Stderr, "Asynchronously updating [%5d] hosts ... \n", len(hosts))

sm := make(map[string]interface{})

Expand Down Expand Up @@ -112,10 +105,7 @@ func AsyncCall(
sliceLength := len(hosts)
wg.Add(sliceLength)

v := viper.GetViper()
if !v.GetBool("json") {
fmt.Fprintf(os.Stderr, "Asynchronously updating [%5d] hosts ... \n", len(hosts))
}
fmt.Fprintf(os.Stderr, "Asynchronously updating [%5d] hosts ... \n", len(hosts))

sm := make(map[string]interface{})

Expand Down
20 changes: 10 additions & 10 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 Expand Up @@ -133,7 +133,7 @@ func (d DecoderMap) Decode(key string) string {
v := viper.GetViper()

if romeAttr, exists := d.Map.Attributes[key]; exists {
if v.GetBool("json") {
if v.GetBool("json") || v.GetBool("yaml") {
key = romeAttr.AttributeName
} else {
key = fmt.Sprintf("%s (%s)", romeAttr.AttributeName, strings.TrimLeft(romeAttr.DisplayName, " "))
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
57 changes: 52 additions & 5 deletions spec/functional/bios_get_attributes_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,16 @@ It "--config ${GRU_CONF} --attributes BootTimeout 127.0.0.1:5000 --json"
The status should equal 0
The stdout should include 'BootTimeout'
The stdout should be_json
The lines of stderr should equal 0
The lines of stderr should equal 1
End

# getting a single key should return only those key in yaml
It "--config ${GRU_CONF} --attributes BootTimeout 127.0.0.1:5000 --yaml"
When call ./gru bios get --config "${GRU_CONF}" --attributes BootTimeout 127.0.0.1:5000 --yaml
The status should equal 0
The stdout should include 'BootTimeout'
The stdout should be_yaml
The lines of stderr should equal 1
End

# getting multiple keys should return only those keys
Expand All @@ -60,7 +69,17 @@ It "--config ${GRU_CONF} --attributes ProcessorHyperThreadingDisable,SRIOVEnable
The stdout should include 'ProcessorHyperThreadingDisable'
The stdout should include 'SRIOVEnable'
The stdout should be_json
The lines of stderr should equal 0
The lines of stderr should equal 1
End

# getting specific keys should return only those keys and should be yaml
It "--config ${GRU_CONF} --attributes ProcessorHyperThreadingDisable,SRIOVEnable 127.0.0.1:5000 --yaml"
When call ./gru bios get --config "${GRU_CONF}" --attributes ProcessorHyperThreadingDisable,SRIOVEnable 127.0.0.1:5000 --yaml
The status should equal 0
The stdout should include 'ProcessorHyperThreadingDisable'
The stdout should include 'SRIOVEnable'
The stdout should be_yaml
The lines of stderr should equal 1
End

# it should error if no matching keys were found
Expand Down Expand Up @@ -103,8 +122,21 @@ It "--config ${GRU_CONF} --virtualization 127.0.0.1:5001 --json"
The stdout should include 'Rome0059' # 'SMT Control'
The stdout should include 'Rome0162' # 'IOMMU'
The stdout should include 'Rome0565' # 'SVM Mode'
The lines of stderr should equal 1
The stdout should be_json
The lines of stderr should equal 0
End

# --virtualization shortcut should return only virtualization attributes in yaml format (Gigabyte)
It "--config ${GRU_CONF} --virtualization 127.0.0.1:5001 --yaml"
When call ./gru bios get --config "${GRU_CONF}" --virtualization 127.0.0.1:5001 --yaml
The status should equal 0
The stdout should include 'PCIS007' # 'SR-IOV Support'
The stdout should include 'Rome0039' # 'Local APIC Mode'
The stdout should include 'Rome0059' # 'SMT Control'
The stdout should include 'Rome0162' # 'IOMMU'
The stdout should include 'Rome0565' # 'SVM Mode'
The lines of stderr should equal 1
The stdout should be_yaml
End

# Gigabyte should return friendly names on non-json output
Expand All @@ -124,15 +156,30 @@ End
It "--config ${GRU_CONF} 127.0.0.1:5001 --json"
When call ./gru bios get --config "${GRU_CONF}" 127.0.0.1:5001 --json
The status should equal 0
# check for some randome keys
# check for some random keys
The stdout should include 'TCG023'
The stdout should include 'Disabled'
The stdout should not include 'Disable Block Sid'
The stdout should include 'Rome0179'
The stdout should include 'Disabled'
The stdout should not include 'Determinism Slider'
The lines of stderr should equal 1
The stdout should be_json
The lines of stderr should equal 0
End

# Gigabyte should not return friendly names on yaml output
It "--config ${GRU_CONF} 127.0.0.1:5001 --yaml"
When call ./gru bios get --config "${GRU_CONF}" 127.0.0.1:5001 --yaml
The status should equal 0
# check for some random keys
The stdout should include 'TCG023'
The stdout should include 'Disabled'
The stdout should not include 'Disable Block Sid'
The stdout should include 'Rome0179'
The stdout should include 'Disabled'
The stdout should not include 'Determinism Slider'
The lines of stderr should equal 1
The stdout should be_yaml
End

End
56 changes: 52 additions & 4 deletions spec/functional/bios_get_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ End
# The lines of stderr should equal 0
#End

# TODO: restore when marshaling YAML errors is fixed.
# getting pending changes should return an error if the Bios/Settings.Attributes does not exist and be valid yaml
#It "--config ${GRU_CONF} --pending 127.0.0.1:5000 --yaml"
# When call ./gru bios get --config "${GRU_CONF}" --pending 127.0.0.1:5000 --yaml
# The status should equal 0
# The stdout should include 'error'
# The stdout should include '\"Attributes\" does not exist or is null, the BIOS/firmware may need to updated for proper Attributes support'
# The stdout should be_yaml
# The lines of stderr should equal 1
#End

# getting keys from a file should return those keys
It "--config ${GRU_CONF} --from-file ${GRU_BIOS_KV} 127.0.0.1:5000"
When call ./gru bios get --config "${GRU_CONF}" --from-file "${GRU_BIOS_KV}" 127.0.0.1:5000
Expand All @@ -84,8 +95,18 @@ It "--config ${GRU_CONF} --from-file ${GRU_BIOS_KV} 127.0.0.1:5000 --json"
The status should equal 0
The stdout should include 'BootTimeout'
The stdout should include 'SRIOVEnable'
The lines of stderr should equal 1
The stdout should be_json
The lines of stderr should equal 0
End

# getting keys from a file should return those keys and be valid yaml
It "--config ${GRU_CONF} --from-file ${GRU_BIOS_KV} 127.0.0.1:5000 --yaml"
When call ./gru bios get --config "${GRU_CONF}" --from-file "${GRU_BIOS_KV}" 127.0.0.1:5000 --yaml
The status should equal 0
The stdout should include 'BootTimeout'
The stdout should include 'SRIOVEnable'
The lines of stderr should equal 1
The stdout should be_yaml
End

# passing a shortcut should return a limited set of pre-defined keys
Expand All @@ -106,11 +127,22 @@ It "--config ${GRU_CONF} --virtualization 127.0.0.1:5003 --json"
The stdout should include 'ProcAmdIOMMU'
The stdout should include 'ProcAmdVirtualization'
The stdout should include 'Sriov'
The lines of stderr should equal 1
The stdout should be_json
The lines of stderr should equal 0
End

# piping in hosts should also work
# passing a shortcut should return a limited set of pre-defined keys and be valid yaml
It "--config ${GRU_CONF} --virtualization 127.0.0.1:5003 --yaml"
When call ./gru bios get --config "${GRU_CONF}" --virtualization 127.0.0.1:5003 --yaml
The status should equal 0
The stdout should include 'ProcAmdIOMMU'
The stdout should include 'ProcAmdVirtualization'
The stdout should include 'Sriov'
The lines of stderr should equal 1
The stdout should be_yaml
End

# piping in hosts should also work (json)
Describe 'validate STDIN works'
Data
#|host1 127.0.0.1:5003
Expand All @@ -121,8 +153,24 @@ Describe 'validate STDIN works'
The stdout should include 'ProcAmdIOMMU'
The stdout should include 'ProcAmdVirtualization'
The stdout should include 'Sriov'
The lines of stderr should equal 1
The stdout should be_json
The lines of stderr should equal 0
End
End

# piping in hosts should also work (yaml)
Describe 'validate STDIN works'
Data
#|host1 127.0.0.1:5003
End
It "echo 127.0.0.1:5003 | --config ${GRU_CONF} --virtualization 127.0.0.1:5003 --yaml"
When call ./gru bios get --config "${GRU_CONF}" --virtualization 127.0.0.1:5003 --yaml
The status should equal 0
The stdout should include 'ProcAmdIOMMU'
The stdout should include 'ProcAmdVirtualization'
The stdout should include 'Sriov'
The stdout should be_yaml
The lines of stderr should equal 1
End
End

Expand Down
Loading

0 comments on commit 1bb33d3

Please sign in to comment.