github.com/jaypipes/ghw@v0.21.1/cmd/ghwc/commands/block.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  // blockCmd represents the install command
    18  var blockCmd = &cobra.Command{
    19  	Use:   "block",
    20  	Short: "Show block storage information for the host system",
    21  	RunE:  showBlock,
    22  }
    23  
    24  // showBlock show block storage information for the host system.
    25  func showBlock(cmd *cobra.Command, args []string) error {
    26  	block, err := ghw.Block()
    27  	if err != nil {
    28  		return errors.Wrap(err, "error getting block device info")
    29  	}
    30  
    31  	switch outputFormat {
    32  	case outputFormatHuman:
    33  		fmt.Printf("%v\n", block)
    34  
    35  		for _, disk := range block.Disks {
    36  			fmt.Printf(" %v\n", disk)
    37  			for _, part := range disk.Partitions {
    38  				fmt.Printf("  %v\n", part)
    39  			}
    40  		}
    41  	case outputFormatJSON:
    42  		fmt.Printf("%s\n", block.JSONString(pretty))
    43  	case outputFormatYAML:
    44  		fmt.Printf("%s", block.YAMLString())
    45  	}
    46  	return nil
    47  }
    48  
    49  func init() {
    50  	rootCmd.AddCommand(blockCmd)
    51  }