Merge pull request #2647 from dgageot/windows-version

Try multiple commands to find windows version
This commit is contained in:
Jean-Laurent de Morlhon 2015-12-21 17:06:00 +01:00
commit cd6690ccd5
2 changed files with 38 additions and 7 deletions

View File

@ -6,14 +6,31 @@ import (
)
func LocalOSVersion() string {
command := exec.Command(`ver`)
command := exec.Command("ver")
output, err := command.Output()
if err != nil {
return ""
if err == nil {
return parseVerOutput(string(output))
}
return parseOutput(string(output))
command = exec.Command("systeminfo")
output, err = command.Output()
if err == nil {
return parseSystemInfoOutput(string(output))
}
return ""
}
func parseOutput(output string) string {
func parseSystemInfoOutput(output string) string {
lines := strings.Split(string(output), "\n")
for _, line := range lines {
if strings.HasPrefix(line, "OS Version") {
return strings.TrimSpace(line[len("OS Version:"):])
}
}
return ""
}
func parseVerOutput(output string) string {
return strings.TrimSpace(output)
}

View File

@ -6,12 +6,26 @@ import (
"github.com/stretchr/testify/assert"
)
func TestOSWindows(t *testing.T) {
func TestParseVerOutput(t *testing.T) {
output := `
Microsoft Windows [version 6.3.9600]
`
assert.Equal(t, "Microsoft Windows [version 6.3.9600]", parseOutput(output))
assert.Equal(t, "Microsoft Windows [version 6.3.9600]", parseVerOutput(output))
}
func TestParseSystemInfoOutput(t *testing.T) {
output := `
Host Name: DESKTOP-3A5PULA
OS Name: Microsoft Windows 10 Enterprise
OS Version: 10.0.10240 N/A Build 10240
OS Manufacturer: Microsoft Corporation
OS Configuration: Standalone Workstation
OS Build Type: Multiprocessor Free
Registered Owner: Windows User
`
assert.Equal(t, "10.0.10240 N/A Build 10240", parseOutput(output))
}