-
Notifications
You must be signed in to change notification settings - Fork 1
/
os.go
64 lines (58 loc) · 1.29 KB
/
os.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 utils
import (
"bytes"
"log"
"os/exec"
"strings"
)
var GOOSDIST string
var GOOSVERS string
var SUPPORT_OS = map[string][]string{
"ubuntu": {"16.04"},
"centos": {"^7.0"},
}
func init() {
cmd := exec.Command("cat", "/etc/os-release")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return
log.Fatal("Can't get the os info, for now we only support: \n", SUPPORT_OS)
}
rs := strings.Split(out.String(), "\n")
for _, r := range rs {
m := strings.Split(r, "=")
if len(m) == 2 {
switch m[0] {
case "ID":
GOOSDIST = strings.Replace(m[1], "\"", "", -1)
case "VERSION_ID":
GOOSVERS = strings.Replace(m[1], "\"", "", -1)
}
}
}
if GOOSDIST == "" || GOOSVERS == "" {
log.Fatal("Can't get the os info, for now we only support: \n", SUPPORT_OS)
}
if versions := SUPPORT_OS[GOOSDIST]; versions != nil {
for _, vs := range versions {
if strings.Index(vs, "^") == 0 {
vMj := vs[1:]
if idx := strings.Index(vMj, "."); idx != -1 {
vMj = vMj[0:idx]
}
dvMj := GOOSVERS
if idx := strings.Index(dvMj, "."); idx != -1 {
dvMj = dvMj[0:idx]
}
if vMj == dvMj {
return
}
} else if vs == GOOSVERS {
return
}
}
}
//log.Fatal("Your os is not supported, for now we only support: \n", SUPPORT_OS)
}