github.com/jaypipes/ghw@v0.21.1/cmd/ghwc/commands/root.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  	"os"
    12  
    13  	"github.com/jaypipes/ghw"
    14  	"github.com/pkg/errors"
    15  	"github.com/spf13/cobra"
    16  )
    17  
    18  const (
    19  	outputFormatHuman = "human"
    20  	outputFormatJSON  = "json"
    21  	outputFormatYAML  = "yaml"
    22  	usageOutputFormat = `Output format.
    23  Choices are 'json','yaml', and 'human'.`
    24  )
    25  
    26  var (
    27  	version       string
    28  	buildHash     string
    29  	buildDate     string
    30  	debug         bool
    31  	outputFormat  string
    32  	outputFormats = []string{
    33  		outputFormatHuman,
    34  		outputFormatJSON,
    35  		outputFormatYAML,
    36  	}
    37  	pretty bool
    38  )
    39  
    40  // rootCmd represents the base command when called without any subcommands
    41  var rootCmd = &cobra.Command{
    42  	Use:   "ghwc",
    43  	Short: "ghwc - Discover hardware information.",
    44  	Args:  validateRootCommand,
    45  	Long: `
    46            __
    47   .-----. |  |--. .--.--.--.
    48   |  _  | |     | |  |  |  |
    49   |___  | |__|__| |________|
    50   |_____|
    51  
    52  Discover hardware information.
    53  
    54  https://github.com/jaypipes/ghw
    55  `,
    56  	RunE: showAll,
    57  }
    58  
    59  func showAll(cmd *cobra.Command, args []string) error {
    60  
    61  	switch outputFormat {
    62  	case outputFormatHuman:
    63  		for _, f := range []func(*cobra.Command, []string) error{
    64  			showBlock,
    65  			showCPU,
    66  			showGPU,
    67  			showMemory,
    68  			showNetwork,
    69  			showTopology,
    70  			showChassis,
    71  			showBIOS,
    72  			showBaseboard,
    73  			showProduct,
    74  			showAccelerator,
    75  			showUSB,
    76  		} {
    77  			err := f(cmd, args)
    78  			if err != nil {
    79  				return err
    80  			}
    81  
    82  		}
    83  
    84  	case outputFormatJSON:
    85  		host, err := ghw.Host()
    86  		if err != nil {
    87  			return errors.Wrap(err, "error getting host info")
    88  		}
    89  		fmt.Printf("%s\n", host.JSONString(pretty))
    90  	case outputFormatYAML:
    91  		host, err := ghw.Host()
    92  		if err != nil {
    93  			return errors.Wrap(err, "error getting host info")
    94  		}
    95  		fmt.Printf("%s", host.YAMLString())
    96  	}
    97  	return nil
    98  }
    99  
   100  // Execute adds all child commands to the root command and sets flags
   101  // appropriately. This is called by main.main(). It only needs to happen once
   102  // to the rootCmd.
   103  func Execute(v string, bh string, bd string) {
   104  	version = v
   105  	buildHash = bh
   106  	buildDate = bd
   107  
   108  	if err := rootCmd.Execute(); err != nil {
   109  		fmt.Println(err)
   110  		os.Exit(1)
   111  	}
   112  }
   113  
   114  func haveValidOutputFormat() bool {
   115  	for _, choice := range outputFormats {
   116  		if choice == outputFormat {
   117  			return true
   118  		}
   119  	}
   120  	return false
   121  }
   122  
   123  // validateRootCommand ensures any CLI options or arguments are valid,
   124  // returning an error if not
   125  func validateRootCommand(rootCmd *cobra.Command, args []string) error {
   126  	if !haveValidOutputFormat() {
   127  		return fmt.Errorf("invalid output format %q", outputFormat)
   128  	}
   129  	return nil
   130  }
   131  
   132  func init() {
   133  	rootCmd.PersistentFlags().BoolVar(
   134  		&debug, "debug", false, "Enable or disable debug mode",
   135  	)
   136  	rootCmd.PersistentFlags().StringVarP(
   137  		&outputFormat,
   138  		"format", "f",
   139  		outputFormatHuman,
   140  		usageOutputFormat,
   141  	)
   142  	rootCmd.PersistentFlags().BoolVar(
   143  		&pretty, "pretty", false, "When outputting JSON, use indentation",
   144  	)
   145  }