-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
64 lines (48 loc) · 1.36 KB
/
main.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
package main
import (
"errors"
"flag"
"fmt"
"strings"
"github.com/JusethAg/os-info-cli/utils"
)
// Custom type for splitting values from filter flag
type filter []string
// Method for formatting flag's value (Part of the flag.Value interface).
// Reference: https://golang.org/pkg/flag/#String
func (filters *filter) String() string {
return fmt.Sprint(*filters)
}
// Method for setting the flag value (Part of the flag.Value interface).
// Reference: https://golang.org/pkg/flag/#Set
func (filters *filter) Set(value string) error {
if len(*filters) > 0 {
return errors.New("filters flag already set")
}
for _, f := range strings.Split(value, ",") {
*filters = append(*filters, f)
}
return nil
}
type flags struct {
all bool
filters filter
}
func main() {
GetFlagsFromCommandLine()
netInfo := utils.GetNetworkInfo()
values := utils.GetMemoryInfo()
fmt.Printf("Private IP: %v\n", netInfo.PrivateIp)
fmt.Printf("Public IP: %v\n", netInfo.PublicIp)
fmt.Printf("Memory values: %v\n", values)
}
func GetFlagsFromCommandLine() flags {
var all bool
var filters filter
flag.BoolVar(&all, "a", true, "Short access to 'all' flag")
flag.BoolVar(&all, "all", true, "Show all info (CPU, Network and memory)")
flag.Var(&filters, "f", "Short access to 'filter' flag")
flag.Var(&filters, "filter", "cpu | net | mem")
flag.Parse()
return flags{all, filters}
}