/
sysinfo.go
215 lines (186 loc) · 5 KB
/
sysinfo.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package sysinfo
import (
"fmt"
"os/exec"
"regexp"
"strconv"
"github.com/patrickmn/go-cache"
)
var sysinfoCache *cache.Cache = cache.New(cache.NoExpiration, cache.NoExpiration)
const VersionOverrideEnvVar = "ACTIVESTATE_CLI_OSVERSION_OVERRIDE"
// Cache keys used for storing/retrieving computed system information.
const (
osVersionInfoCacheKey = "osVersionInfo"
libcInfoCacheKey = "libcInfo"
requestedLibcInfoCacheKey = "requestedLibcInfo"
compilersCacheKey = "compilers"
)
var versionRegex = regexp.MustCompile(`^(\d+)\D(\d+)(?:\D(\d+))?`)
// OsInfo represents an OS returned by OS().
type OsInfo int
const (
// Linux represents the Linux operating system.
Linux OsInfo = iota
// Windows represents the Windows operating system.
Windows
// Mac represents the Macintosh operating system.
Mac
// UnknownOs represents an unknown operating system.
UnknownOs
)
func (i OsInfo) String() string {
switch i {
case Linux:
return "Linux"
case Windows:
return "Windows"
case Mac:
return "MacOS"
default:
return "Unknown"
}
}
type VersionInfo struct {
Version string // raw version string
Major int // major version number
Minor int // minor version number
Micro int // micro version number
}
// OSVersionInfo represents an OS version returned by OSVersion().
type OSVersionInfo struct {
*VersionInfo
Name string // free-form name string (varies by OS)
}
// ArchInfo represents an architecture returned by Architecture().
type ArchInfo int
const (
// I386 represents the Intel x86 (32-bit) architecture.
I386 ArchInfo = iota
// Amd64 represents the x86_64 (64-bit) architecture.
Amd64
// Arm represents the ARM architecture.
Arm
// UnknownArch represents an unknown architecture.
UnknownArch
)
func (i ArchInfo) String() string {
switch i {
case I386:
return "i386"
case Amd64:
return "x86_64"
case Arm:
return "ARM"
default:
return "Unknown"
}
}
// LibcNameInfo represents a C library name.
type LibcNameInfo int
const (
// Glibc represents the GNU C library.
Glibc LibcNameInfo = iota
// Msvcrt represents the Microsoft Visual C++ runtime library.
Msvcrt
// BsdLibc represents the BSD C library.
BsdLibc
// UnknownLibc represents an unknown C library.
UnknownLibc
)
func (i LibcNameInfo) String() string {
switch i {
case Glibc:
return "glibc"
case Msvcrt:
return "msvcrt"
case BsdLibc:
return "libc"
default:
return "Unknown"
}
}
// LibcInfo represents a LibC returned by Libc().
type LibcInfo struct {
Name LibcNameInfo // C library name
Major int // major version number
Minor int // minor version number
}
func (l LibcInfo) Version() string {
return fmt.Sprintf("%d.%d", l.Major, l.Minor)
}
// CompilerNameInfo reprents a compiler toolchain name.
type CompilerNameInfo int
const (
// Gcc represents the GNU C Compiler toolchain.
Gcc CompilerNameInfo = iota
// Msvc represents the Microsoft Visual C++ toolchain.
Msvc
// Mingw represents the Minimalist GNU for Windows toolchain.
Mingw
// Clang represents the LLVM/Clang toolchain.
Clang
)
func (i CompilerNameInfo) String() string {
switch i {
case Gcc:
return "GCC"
case Msvc:
return "MSVC"
case Mingw:
return "MinGW"
case Clang:
return "clang"
default:
return "Unknown"
}
}
// CompilerInfo represents a compiler toolchain returned by Compiler().
type CompilerInfo struct {
Name CompilerNameInfo // C compiler name
Major int // major version number
Minor int // minor version number
}
// Checks whether or not the given compiler exists and returns its major and
// minor version numbers. A major return of 0 indicates the compiler does not
// exist, or that an error occurred.
func getCompilerVersion(args []string) (int, int, error) {
cc, err := exec.Command(args[0], args[1:]...).CombinedOutput()
if err != nil {
return 0, 0, nil
}
regex := regexp.MustCompile(`(\d+)\D(\d+)\D\d+`)
parts := regex.FindStringSubmatch(string(cc))
if len(parts) != 3 {
return 0, 0, fmt.Errorf("Unable to parse version string '%s'", cc)
}
for i := 1; i < len(parts); i++ {
if _, err := strconv.Atoi(parts[i]); err != nil {
return 0, 0, fmt.Errorf("Unable to parse part '%s' of version string '%s'", parts[i], cc)
}
}
major, _ := strconv.Atoi(parts[1])
minor, _ := strconv.Atoi(parts[2])
return major, minor, nil
}
func parseVersionInfo(v string) (*VersionInfo, error) {
parts := versionRegex.FindStringSubmatch(v)
if len(parts) == 0 {
return nil, fmt.Errorf("Unable to parse version string '%s'", v)
}
major, err := strconv.Atoi(parts[1])
if err != nil {
return nil, fmt.Errorf("Unable to parse part '%s' of version string '%s'", parts[1], v)
}
minor, err := strconv.Atoi(parts[2])
if err != nil {
return nil, fmt.Errorf("Unable to parse part '%s' of version string '%s'", parts[2], v)
}
var micro int = 0
if parts[3] != "" {
micro, err = strconv.Atoi(parts[3])
if err != nil {
return nil, fmt.Errorf("Unable to parse part '%s' of version string '%s'", parts[3], v)
}
}
return &VersionInfo{v, major, minor, micro}, nil
}