forked from jaypipes/ghw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
142 lines (127 loc) · 2.95 KB
/
root.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//
// 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"
"os"
"github.com/jaypipes/ghw"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
const (
outputFormatHuman = "human"
outputFormatJSON = "json"
outputFormatYAML = "yaml"
usageOutputFormat = `Output format.
Choices are 'json','yaml', and 'human'.`
)
var (
version string
buildHash string
buildDate string
debug bool
outputFormat string
outputFormats = []string{
outputFormatHuman,
outputFormatJSON,
outputFormatYAML,
}
pretty bool
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "ghwc",
Short: "ghwc - Discover hardware information.",
Args: validateRootCommand,
Long: `
__
.-----. | |--. .--.--.--.
| _ | | | | | | |
|___ | |__|__| |________|
|_____|
Discover hardware information.
https://github.com/jaypipes/ghw
`,
RunE: showAll,
}
func showAll(cmd *cobra.Command, args []string) error {
switch outputFormat {
case outputFormatHuman:
if err := showBlock(cmd, args); err != nil {
return err
}
if err := showCPU(cmd, args); err != nil {
return err
}
if err := showGPU(cmd, args); err != nil {
return err
}
if err := showMemory(cmd, args); err != nil {
return err
}
if err := showNetwork(cmd, args); err != nil {
return err
}
if err := showTopology(cmd, args); err != nil {
return err
}
case outputFormatJSON:
host, err := ghw.Host()
if err != nil {
return errors.Wrap(err, "error getting host info")
}
fmt.Printf("%s\n", host.JSONString(pretty))
case outputFormatYAML:
host, err := ghw.Host()
if err != nil {
return errors.Wrap(err, "error getting host info")
}
fmt.Printf("%s", host.YAMLString())
}
return nil
}
// Execute adds all child commands to the root command and sets flags
// appropriately. This is called by main.main(). It only needs to happen once
// to the rootCmd.
func Execute(v string, bh string, bd string) {
version = v
buildHash = bh
buildDate = bd
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func haveValidOutputFormat() bool {
for _, choice := range outputFormats {
if choice == outputFormat {
return true
}
}
return false
}
// validateRootCommand ensures any CLI options or arguments are valid,
// returning an error if not
func validateRootCommand(rootCmd *cobra.Command, args []string) error {
if !haveValidOutputFormat() {
return fmt.Errorf("invalid output format %q", outputFormat)
}
return nil
}
func init() {
rootCmd.PersistentFlags().BoolVar(
&debug, "debug", false, "Enable or disable debug mode",
)
rootCmd.PersistentFlags().StringVarP(
&outputFormat,
"format", "f",
outputFormatHuman,
usageOutputFormat,
)
rootCmd.PersistentFlags().BoolVar(
&pretty, "pretty", false, "When outputting JSON, use indentation",
)
}