This repository has been archived by the owner on Feb 16, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
uname: Added proper -o, --operating-system switch. Fixed internal/flag
- Loading branch information
1 parent
30afe3c
commit fab43bc
Showing
20 changed files
with
256 additions
and
162,374 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
The -o, --operating-system switch should work just fine for most | ||
standard OSs. | ||
|
||
If you need to set a custom value, please install the tool as such: | ||
|
||
`go install --tags=ostypes -ldflags '-X main.hostOS=FOO'` | ||
|
||
If you do not know the value you need to set, please run `go generate` prior | ||
to installing this command. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package main | ||
|
||
//go:generate go run ostypes.go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
// +build ostypes | ||
|
||
package main | ||
|
||
// HostOS will be overwritten with the go:generate command. | ||
const HostOS = Unknown | ||
var hostOS = unknown |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// +build darwin,!ostypes | ||
|
||
package main | ||
|
||
const hostOS = "Darwin" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// +build dragonfly,!ostypes | ||
|
||
package main | ||
|
||
const hostOS = "Dragonfly" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// +build linux,!ostypes | ||
|
||
package main | ||
|
||
const hostOS = "GNU/Linux" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// +build linux,!ostypes | ||
|
||
package main | ||
|
||
const hostOS = "GNU/Linux" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// +build netbsd,!ostypes | ||
|
||
package main | ||
|
||
const hostOS = "NetBSD" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// +build openbsd,!ostypes | ||
|
||
package main | ||
|
||
const hostOS = "OpenBSD" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// +build plan9,!ostypes | ||
|
||
package main | ||
|
||
const hostOS = "Plan 9" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// +build solaris,!ostypes | ||
|
||
package main | ||
|
||
const hostOS = "Solaris" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// +build windows,!ostypes | ||
|
||
package main | ||
|
||
const hostOS = "Windows NT" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// +build ignore | ||
|
||
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() { | ||
const pkg = "main" | ||
|
||
// Shell locals... | ||
out, err := exec.Command("sh", "-c", "echo $OSTYPE").Output() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if len(out) > 0 && 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.Fprintln(file, "// +build ostypes") | ||
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) { | ||
realHostOS := strings.ToUpper(string(rawHostOS[0])) | ||
if len(rawHostOS) > 1 { | ||
realHostOS += rawHostOS[1:] | ||
} | ||
} else { | ||
realHostOS = rawHostOS | ||
} | ||
} | ||
|
||
fmt.Fprintf(file, "%q\n\n", realHostOS) | ||
} |
Oops, something went wrong.