forked from jaypipes/ghw
/
net.go
61 lines (52 loc) · 1.28 KB
/
net.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//
// 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 (
"fmt"
"github.com/jaypipes/ghw"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
// netCmd represents the install command
var netCmd = &cobra.Command{
Use: "net",
Short: "Show network information for the host system",
RunE: showNetwork,
}
// showNetwork show network information for the host system.
func showNetwork(cmd *cobra.Command, args []string) error {
net, err := ghw.Network()
if err != nil {
return errors.Wrap(err, "error getting network info")
}
switch outputFormat {
case outputFormatHuman:
fmt.Printf("%v\n", net)
for _, nic := range net.NICs {
fmt.Printf(" %v\n", nic)
enabledCaps := make([]int, 0)
for x, cap := range nic.Capabilities {
if cap.IsEnabled {
enabledCaps = append(enabledCaps, x)
}
}
if len(enabledCaps) > 0 {
fmt.Printf(" enabled capabilities:\n")
for _, x := range enabledCaps {
fmt.Printf(" - %s\n", nic.Capabilities[x].Name)
}
}
}
case outputFormatJSON:
fmt.Printf("%s\n", net.JSONString(pretty))
case outputFormatYAML:
fmt.Printf("%s", net.YAMLString())
}
return nil
}
func init() {
rootCmd.AddCommand(netCmd)
}