Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/ghwc/commands/accelerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ func showAccelerator(cmd *cobra.Command, args []string) error {
fmt.Printf(" %v\n", card)
}
case outputFormatJSON:
fmt.Printf("%s\n", accel.JSONString(pretty))
fmt.Printf("%s\n", JSONString(accel, pretty))
case outputFormatYAML:
fmt.Printf("%s", accel.YAMLString())
fmt.Printf("%s", YAMLString(accel))
}
return nil
}
Expand Down
9 changes: 1 addition & 8 deletions cmd/ghwc/commands/baseboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,7 @@ func showBaseboard(cmd *cobra.Command, args []string) error {
return fmt.Errorf("error getting baseboard info: %w", err)
}

switch outputFormat {
case outputFormatHuman:
fmt.Printf("%v\n", baseboard)
case outputFormatJSON:
fmt.Printf("%s\n", baseboard.JSONString(pretty))
case outputFormatYAML:
fmt.Printf("%s", baseboard.YAMLString())
}
printInfo(baseboard)
return nil
}

Expand Down
9 changes: 1 addition & 8 deletions cmd/ghwc/commands/bios.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,7 @@ func showBIOS(cmd *cobra.Command, args []string) error {
return fmt.Errorf("error getting BIOS info: %w", err)
}

switch outputFormat {
case outputFormatHuman:
fmt.Printf("%v\n", bios)
case outputFormatJSON:
fmt.Printf("%s\n", bios.JSONString(pretty))
case outputFormatYAML:
fmt.Printf("%s", bios.YAMLString())
}
printInfo(bios)
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/ghwc/commands/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ func showBlock(cmd *cobra.Command, args []string) error {
}
}
case outputFormatJSON:
fmt.Printf("%s\n", block.JSONString(pretty))
fmt.Printf("%s\n", JSONString(block, pretty))
case outputFormatYAML:
fmt.Printf("%s", block.YAMLString())
fmt.Printf("%s", YAMLString(block))
}
return nil
}
Expand Down
9 changes: 1 addition & 8 deletions cmd/ghwc/commands/chassis.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,7 @@ func showChassis(cmd *cobra.Command, args []string) error {
return fmt.Errorf("error getting chassis info: %w", err)
}

switch outputFormat {
case outputFormatHuman:
fmt.Printf("%v\n", chassis)
case outputFormatJSON:
fmt.Printf("%s\n", chassis.JSONString(pretty))
case outputFormatYAML:
fmt.Printf("%s", chassis.YAMLString())
}
printInfo(chassis)
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/ghwc/commands/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ func showCPU(cmd *cobra.Command, args []string) error {
}
}
case outputFormatJSON:
fmt.Printf("%s\n", cpu.JSONString(pretty))
fmt.Printf("%s\n", JSONString(cpu, pretty))
case outputFormatYAML:
fmt.Printf("%s", cpu.YAMLString())
fmt.Printf("%s", YAMLString(cpu))
}
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/ghwc/commands/gpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ func showGPU(cmd *cobra.Command, args []string) error {
fmt.Printf(" %v\n", card)
}
case outputFormatJSON:
fmt.Printf("%s\n", gpu.JSONString(pretty))
fmt.Printf("%s\n", JSONString(gpu, pretty))
case outputFormatYAML:
fmt.Printf("%s", gpu.YAMLString())
fmt.Printf("%s", YAMLString(gpu))
}
return nil
}
Expand Down
50 changes: 50 additions & 0 deletions cmd/ghwc/commands/marshal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// Use and distribution licensed under the Apache license version 2.
//
// See the COPYING file in the root project directory for full text.
//

package commands

import (
"encoding/json"

yaml "gopkg.in/yaml.v3"
)

// YAMLString returns a string after marshalling the supplied parameter into YAML.
func YAMLString(p interface{}) string {
b, err := json.Marshal(p)
if err != nil {
return ""
}

var jsonObj interface{}
if err := yaml.Unmarshal(b, &jsonObj); err != nil {
return ""
}

yb, err := yaml.Marshal(jsonObj)
if err != nil {
return ""
}

return string(yb)
}

// JSONString returns a string after marshalling the supplied parameter into
// JSON. Accepts an optional argument to trigger pretty/indented formatting of
// the JSON string.
func JSONString(p interface{}, indent bool) string {
var b []byte
var err error
if !indent {
b, err = json.Marshal(p)
} else {
b, err = json.MarshalIndent(&p, "", " ")
}
if err != nil {
return ""
}
return string(b)
}
4 changes: 2 additions & 2 deletions cmd/ghwc/commands/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ func showNetwork(cmd *cobra.Command, args []string) error {
}
}
case outputFormatJSON:
fmt.Printf("%s\n", net.JSONString(pretty))
fmt.Printf("%s\n", JSONString(net, pretty))
case outputFormatYAML:
fmt.Printf("%s", net.YAMLString())
fmt.Printf("%s", YAMLString(net))
}
return nil
}
Expand Down
19 changes: 7 additions & 12 deletions cmd/ghwc/commands/print_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,16 @@

package commands

import "fmt"
import (
"fmt"
)

type formattable interface {
String() string
JSONString(bool) string
YAMLString() string
}

func printInfo(f formattable) {
func printInfo(f interface{}) {
switch outputFormat {
case outputFormatHuman:
fmt.Printf("%s\n", f)
case outputFormatJSON:
fmt.Printf("%s\n", f.JSONString(pretty))
f = JSONString(f, pretty)
case outputFormatYAML:
fmt.Printf("%s", f.YAMLString())
f = YAMLString(f)
}
fmt.Printf("%s", f)
}
9 changes: 1 addition & 8 deletions cmd/ghwc/commands/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,7 @@ func showProduct(cmd *cobra.Command, args []string) error {
return fmt.Errorf("error getting product info: %w", err)
}

switch outputFormat {
case outputFormatHuman:
fmt.Printf("%v\n", product)
case outputFormatJSON:
fmt.Printf("%s\n", product.JSONString(pretty))
case outputFormatYAML:
fmt.Printf("%s", product.YAMLString())
}
printInfo(product)
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/ghwc/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ func showAll(cmd *cobra.Command, args []string) error {
if err != nil {
return fmt.Errorf("error getting host info: %w", err)
}
fmt.Printf("%s\n", host.JSONString(pretty))
fmt.Printf("%s\n", JSONString(host, pretty))
case outputFormatYAML:
host, err := ghw.Host()
if err != nil {
return fmt.Errorf("error getting host info: %w", err)
}
fmt.Printf("%s", host.YAMLString())
fmt.Printf("%s", YAMLString(host))
}
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/ghwc/commands/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ func showTopology(cmd *cobra.Command, args []string) error {
}
}
case outputFormatJSON:
fmt.Printf("%s\n", topology.JSONString(pretty))
fmt.Printf("%s\n", JSONString(topology, pretty))
case outputFormatYAML:
fmt.Printf("%s", topology.YAMLString())
fmt.Printf("%s", YAMLString(topology))
}
return nil
}
Expand Down
12 changes: 1 addition & 11 deletions cmd/ghwc/commands/usb.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,7 @@ func showUSB(cmd *cobra.Command, args []string) error {
return fmt.Errorf("error getting USB info: %w", err)
}

switch outputFormat {
case outputFormatHuman:
fmt.Printf("%v\n", usb)
for _, usb := range usb.Devices {
fmt.Printf(" %+v\n", usb)
}
case outputFormatJSON:
fmt.Printf("%s\n", usb.JSONString(pretty))
case outputFormatYAML:
fmt.Printf("%s", usb.YAMLString())
}
printInfo(usb)
return nil
}

Expand Down
13 changes: 0 additions & 13 deletions host.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/jaypipes/ghw/pkg/chassis"
"github.com/jaypipes/ghw/pkg/cpu"
"github.com/jaypipes/ghw/pkg/gpu"
"github.com/jaypipes/ghw/pkg/marshal"
"github.com/jaypipes/ghw/pkg/memory"
"github.com/jaypipes/ghw/pkg/net"
"github.com/jaypipes/ghw/pkg/pci"
Expand Down Expand Up @@ -138,15 +137,3 @@ func (info *HostInfo) String() string {
info.USB.String(),
)
}

// YAMLString returns a string with the host information formatted as YAML
// under a top-level "host:" key
func (i *HostInfo) YAMLString() string {
return marshal.SafeYAML(i)
}

// JSONString returns a string with the host information formatted as JSON
// under a top-level "host:" key
func (i *HostInfo) JSONString(indent bool) string {
return marshal.SafeJSON(i, indent)
}
19 changes: 0 additions & 19 deletions pkg/accelerator/accelerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"fmt"

"github.com/jaypipes/ghw/internal/config"
"github.com/jaypipes/ghw/pkg/marshal"
"github.com/jaypipes/ghw/pkg/pci"
)

Expand Down Expand Up @@ -61,21 +60,3 @@ func (i *Info) String() string {
numDevsStr,
)
}

// simple private struct used to encapsulate processing accelerators information in a top-level
// "accelerator" YAML/JSON map/object key
type acceleratorPrinter struct {
Info *Info `json:"accelerator"`
}

// YAMLString returns a string with the processing accelerators information formatted as YAML
// under a top-level "accelerator:" key
func (i *Info) YAMLString() string {
return marshal.SafeYAML(acceleratorPrinter{i})
}

// JSONString returns a string with the processing accelerators information formatted as JSON
// under a top-level "accelerator:" key
func (i *Info) JSONString(indent bool) string {
return marshal.SafeJSON(acceleratorPrinter{i}, indent)
}
19 changes: 0 additions & 19 deletions pkg/baseboard/baseboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package baseboard

import (
"github.com/jaypipes/ghw/internal/config"
"github.com/jaypipes/ghw/pkg/marshal"
"github.com/jaypipes/ghw/pkg/util"
)

Expand Down Expand Up @@ -58,21 +57,3 @@ func New(args ...any) (*Info, error) {
}
return info, nil
}

// simple private struct used to encapsulate baseboard information in a top-level
// "baseboard" YAML/JSON map/object key
type baseboardPrinter struct {
Info *Info `json:"baseboard"`
}

// YAMLString returns a string with the baseboard information formatted as YAML
// under a top-level "dmi:" key
func (info *Info) YAMLString() string {
return marshal.SafeYAML(baseboardPrinter{info})
}

// JSONString returns a string with the baseboard information formatted as JSON
// under a top-level "baseboard:" key
func (info *Info) JSONString(indent bool) string {
return marshal.SafeJSON(baseboardPrinter{info}, indent)
}
19 changes: 0 additions & 19 deletions pkg/bios/bios.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"fmt"

"github.com/jaypipes/ghw/internal/config"
"github.com/jaypipes/ghw/pkg/marshal"
"github.com/jaypipes/ghw/pkg/util"
)

Expand Down Expand Up @@ -55,21 +54,3 @@ func New(args ...any) (*Info, error) {
}
return info, nil
}

// simple private struct used to encapsulate BIOS information in a top-level
// "bios" YAML/JSON map/object key
type biosPrinter struct {
Info *Info `json:"bios"`
}

// YAMLString returns a string with the BIOS information formatted as YAML
// under a top-level "dmi:" key
func (info *Info) YAMLString() string {
return marshal.SafeYAML(biosPrinter{info})
}

// JSONString returns a string with the BIOS information formatted as JSON
// under a top-level "bios:" key
func (info *Info) JSONString(indent bool) string {
return marshal.SafeJSON(biosPrinter{info}, indent)
}
19 changes: 0 additions & 19 deletions pkg/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"strings"

"github.com/jaypipes/ghw/internal/config"
"github.com/jaypipes/ghw/pkg/marshal"
"github.com/jaypipes/ghw/pkg/unitutil"
"github.com/jaypipes/ghw/pkg/util"
)
Expand Down Expand Up @@ -395,21 +394,3 @@ func (p *Partition) String() string {
mountStr,
)
}

// simple private struct used to encapsulate block information in a top-level
// "block" YAML/JSON map/object key
type blockPrinter struct {
Info *Info `json:"block" yaml:"block"`
}

// YAMLString returns a string with the block information formatted as YAML
// under a top-level "block:" key
func (i *Info) YAMLString() string {
return marshal.SafeYAML(blockPrinter{i})
}

// JSONString returns a string with the block information formatted as JSON
// under a top-level "block:" key
func (i *Info) JSONString(indent bool) string {
return marshal.SafeJSON(blockPrinter{i}, indent)
}
Loading