-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsystem_linux.go
89 lines (71 loc) · 2 KB
/
system_linux.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 system
import (
"fmt"
"io/ioutil"
"syscall"
)
func newSystemInfo() SystemInfo {
var sysInfo SystemInfo
var unameInfo syscall.Utsname
if err := syscall.Uname(&unameInfo); err != nil {
return sysInfo
}
sysInfo.Sysname = nativeCharsToString(unameInfo.Sysname)
sysInfo.Nodename = nativeCharsToString(unameInfo.Nodename)
sysInfo.Release = nativeCharsToString(unameInfo.Release)
sysInfo.Version = nativeCharsToString(unameInfo.Version)
sysInfo.Machine = nativeCharsToString(unameInfo.Machine)
sysInfo.Domainname = nativeCharsToString(unameInfo.Domainname)
sysInfo.OsName = osName()
return sysInfo
}
var defaultSysInfo = newSystemInfo()
func GetSystemInfo() SystemInfo {
return defaultSysInfo
}
func osName() string {
bdata, err := ioutil.ReadFile("/etc/os-release")
if err != nil {
fmt.Printf("error reading /etc/os-release: %v\n", err)
return "other"
}
if osr, err := NewOsRelease(bdata); err == nil {
var nameMain, nameVersion string
nameMain = osr.Name
if len(osr.Version) > 0 {
nameVersion = osr.Version
} else {
nameVersion = osr.VersionID
}
return fmt.Sprintf("%v %v", nameMain, nameVersion)
}
return "other"
}
/*
func getOperatingSystem() string {
bdata, err := ioutil.ReadFile("/etc/os-release")
if err != nil {
print("error reading /etc/os-release")
return ""
}
var nameMain, nameVersion string
if i := bytes.Index(bdata, []byte("NAME")); i >= 0 {
offset := i+ len("NAME") + 2
nameData = bdata[offset:]
nameMain = string(nameData[:bytes.IndexByte(nameData, '"')])
}
if i := bytes.Index(bdata, []byte("VERSION")); i >= 0 {
offset := i+ len("VERSION") + 2
nameData = bdata[offset:]
nameMain = string(nameData[:bytes.IndexByte(nameData, '"')])
} else {
if i := bytes.Index(bdata, []byte("VERSION_ID")); i >= 0 {
//version id could be with or without quotes
offset := i+ len("VERSION_ID") + 2
nameData = bdata[offset:]
nameMain = string(nameData[:bytes.IndexByte(nameData, '"')])
}
}
return fmt.Sprintf("%v %v",nameMain,nameVersion)
}
*/