-
Notifications
You must be signed in to change notification settings - Fork 14
/
os_windows.go
80 lines (65 loc) · 1.25 KB
/
os_windows.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
package osutil
import (
"bytes"
"os/exec"
"strings"
"syscall"
)
const (
Name = "Windows"
)
func getEdition() string {
cmd := exec.Command("cmd")
var out bytes.Buffer
cmd.Stdout = &out
// the next 2 lines makes a console window hidden, that is expected behavior in most cases.
cmd.SysProcAttr = new(syscall.SysProcAttr)
cmd.SysProcAttr.HideWindow = true
err := cmd.Run()
if err != nil {
panic(err)
}
raw := out.String()
i1 := strings.Index(raw, "[Version")
i2 := strings.Index(raw, "]")
var ver string
if i1 == -1 || i2 == -1 {
ver = ""
} else {
ver = raw[i1+len("[Version") : i2]
}
return strings.Trim(ver, " ")
}
func GetVersion() string {
version := getEdition()
parts := strings.Split(version, ".")
majormin := parts[0] + "." + parts[1]
var edition string
switch majormin {
case "10.0": // 10 Server
edition = "10"
case "6.3": // Server 2012 R2
edition = "8.1"
case "6.2": // Server 2012
edition = "8"
case "6.1":
edition = "7"
case "6.0":
edition = "Vista"
case "5.2":
edition = "Server 2003"
case "5.1":
edition = "XP"
case "5.0":
edition = "2000"
}
return edition
}
func GetDisplay() string {
display := Name
version := GetVersion()
if version != "" {
display += " " + version
}
return display
}