-
Notifications
You must be signed in to change notification settings - Fork 0
/
os_darwin.go
89 lines (76 loc) · 1.82 KB
/
os_darwin.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
package report
import (
"bufio"
"bytes"
"fmt"
"strings"
"github.com/lucor/goinfo"
"golang.org/x/sys/execabs"
)
// Info returns the collected info
func (i *OS) Info() (goinfo.Info, error) {
info, err := i.swVers()
if err != nil {
return nil, err
}
arch, err := i.architecture()
if err != nil {
return info, err
}
info["architecture"] = arch
kernel, err := i.kernel()
if err != nil {
return info, err
}
info["kernel"] = kernel
return info, err
}
func (i *OS) swVers() (goinfo.Info, error) {
cmd := execabs.Command("sw_vers")
out, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("could not detect os info using sw_vers command: %w", err)
}
return i.parseSwVersCmdOutput(out)
}
func (i *OS) parseSwVersCmdOutput(data []byte) (goinfo.Info, error) {
// fitlerKeys defines the key to return
filterKeys := map[string]string{
"ProductName": "name",
"ProductVersion": "version",
"BuildVersion": "build_version",
}
info := goinfo.Info{}
buf := bytes.NewBuffer(data)
scanner := bufio.NewScanner(buf)
for scanner.Scan() {
tokens := strings.Split(scanner.Text(), ":")
if len(tokens) != 2 {
continue
}
key, ok := filterKeys[tokens[0]]
if !ok {
continue
}
info[key] = strings.Trim(tokens[1], "\t ")
}
return info, scanner.Err()
}
func (i *OS) architecture() (string, error) {
cmd := execabs.Command("uname", "-m")
out, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("could not detect architecture using uname command: %w", err)
}
arch := strings.Trim(string(out), "\n")
return arch, nil
}
func (i *OS) kernel() (string, error) {
cmd := execabs.Command("uname", "-rsv")
out, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("could not detect the kernel using uname command: %w", err)
}
kernel := strings.Trim(string(out), "\n")
return kernel, nil
}