-
Notifications
You must be signed in to change notification settings - Fork 0
/
ostype.go
122 lines (106 loc) · 2.5 KB
/
ostype.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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
// globs are stolen from GNU coreutils' host-os.m4 file.
// see GNU-LICENSE.txt
var globs = map[string]string{
"winnt*": "Windows NT",
"vos*": "VOS",
"sysv*": "Unix System V",
"superux*": "SUPER-UX",
"sunos*": "SunOS",
"stop*": "STOP",
"sco*": "SCO Unix",
"riscos*": "RISC OS",
"riscix*": "RISCiX",
"qnx*": "QNX",
"pw32*": "PW32",
"ptx*": "ptx",
"plan9*": "Plan 9",
"osf*": "Tru64",
"os2*": "OS/2",
"openbsd*": "OpenBSD",
"nsk*": "NonStop Kernel",
"nonstopux*": "NonStop-UX",
"netbsd*-gnu*": "GNU/NetBSD",
"netbsd*": "NetBSD",
"mirbsd*": "MirBSD",
"knetbsd*-gnu": "GNU/kNetBSD",
"kfreebsd*-gnu": "GNU/kFreeBSD",
"msdosdjgpp*": "DJGPP",
"mpeix*": "MPE/iX",
"mint*": "MiNT",
"mingw*": "MinGW",
"lynxos*": "LynxOS",
"linux*": "GNU/Linux",
"hpux*": "HP-UX",
"hiux*": "HI-UX",
"gnu*": "GNU",
"freebsd*": "FreeBSD",
"dgux*": "DG/UX",
"bsdi*": "BSD/OS",
"bsd*": "BSD",
"beos*": "BeOS",
"aux*": "A/UX",
"atheos*": "AtheOS",
"amigaos*": "Amiga OS",
"aix*": "AIX",
}
func match(pattern, name string) bool {
ok, err := filepath.Match(pattern, name)
return ok && err == nil
}
func main() {
pkg := "main"
if len(os.Args) > 1 {
pkg = os.Args[1]
}
// Shell locals...
cmd := exec.Command("sh", "-c", "echo $OSTYPE")
out, err := cmd.Output()
if err != nil {
panic(err)
}
if out[len(out)-1] == '\n' {
out = out[:len(out)-1]
}
file, err := os.Create("host_os.go")
if err != nil {
panic(err)
}
defer file.Close()
rawHostOS := string(out)
fmt.Fprintf(file, "// generated by ostypes. DO NOT EDIT.\n\n")
fmt.Fprintf(file, "package %s\n\nconst HostOS = ", pkg)
// This is what uname does.
if rawHostOS == "" {
fmt.Fprintf(file, "%q\n\n", "Unknown")
return
}
var realHostOS string
for glob, val := range globs {
if match(glob, rawHostOS) {
realHostOS = val
break
}
}
// No matches found, so apply the default heuristic.
if realHostOS == "" {
if match("[A-Za-z]*", rawHostOS) {
upper := strings.ToUpper(string(rawHostOS[0]))
if len(rawHostOS) > 1 {
realHostOS = upper + rawHostOS[1:]
} else {
realHostOS = upper
}
} else {
realHostOS = rawHostOS
}
}
fmt.Fprintf(file, "%q\n\n", realHostOS)
}