github.com/jaypipes/ghw@v0.21.1/cmd/ghwc/commands/usb.go (about)

     1  //
     2  // Use and distribution licensed under the Apache license version 2.
     3  //
     4  // See the COPYING file in the root project directory for full text.
     5  //
     6  
     7  package commands
     8  
     9  import (
    10  	"fmt"
    11  
    12  	"github.com/jaypipes/ghw"
    13  	"github.com/pkg/errors"
    14  	"github.com/spf13/cobra"
    15  )
    16  
    17  // usbCmd represents the `usb` command
    18  var usbCmd = &cobra.Command{
    19  	Use:   "usb",
    20  	Short: "Show USB information for the host system",
    21  	RunE:  showUSB,
    22  }
    23  
    24  // showUSB show usb information for the host system.
    25  func showUSB(cmd *cobra.Command, args []string) error {
    26  	usb, err := ghw.USB()
    27  	if err != nil {
    28  		return errors.Wrap(err, "error getting network info")
    29  	}
    30  
    31  	switch outputFormat {
    32  	case outputFormatHuman:
    33  		fmt.Printf("%v\n", usb)
    34  		for _, usb := range usb.Devices {
    35  			fmt.Printf(" %+v\n", usb)
    36  		}
    37  	case outputFormatJSON:
    38  		fmt.Printf("%s\n", usb.JSONString(pretty))
    39  	case outputFormatYAML:
    40  		fmt.Printf("%s", usb.YAMLString())
    41  	}
    42  	return nil
    43  }
    44  
    45  func init() {
    46  	rootCmd.AddCommand(usbCmd)
    47  }