github.com/jaypipes/ghw@v0.21.1/cmd/ghwc/commands/net.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  // netCmd represents the install command
    18  var netCmd = &cobra.Command{
    19  	Use:   "net",
    20  	Short: "Show network information for the host system",
    21  	RunE:  showNetwork,
    22  }
    23  
    24  // showNetwork show network information for the host system.
    25  func showNetwork(cmd *cobra.Command, args []string) error {
    26  	net, err := ghw.Network()
    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", net)
    34  
    35  		for _, nic := range net.NICs {
    36  			fmt.Printf(" %v\n", nic)
    37  
    38  			enabledCaps := make([]int, 0)
    39  			for x, cap := range nic.Capabilities {
    40  				if cap.IsEnabled {
    41  					enabledCaps = append(enabledCaps, x)
    42  				}
    43  			}
    44  			if len(enabledCaps) > 0 {
    45  				fmt.Printf("  enabled capabilities:\n")
    46  				for _, x := range enabledCaps {
    47  					fmt.Printf("   - %s\n", nic.Capabilities[x].Name)
    48  				}
    49  			}
    50  		}
    51  	case outputFormatJSON:
    52  		fmt.Printf("%s\n", net.JSONString(pretty))
    53  	case outputFormatYAML:
    54  		fmt.Printf("%s", net.YAMLString())
    55  	}
    56  	return nil
    57  }
    58  
    59  func init() {
    60  	rootCmd.AddCommand(netCmd)
    61  }