implement redhatishVersion and platform on Linux.

This commit is contained in:
WAKAYAMA Shirou 2014-05-27 22:37:23 +09:00
parent 710c02905f
commit 76866feb03
3 changed files with 91 additions and 14 deletions

View File

@ -3,7 +3,6 @@ package gopsutil
import ( import (
"fmt" "fmt"
"testing" "testing"
) )
func TestCpu_times(t *testing.T) { func TestCpu_times(t *testing.T) {

View File

@ -8,6 +8,7 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
"regexp"
"runtime" "runtime"
"strings" "strings"
"syscall" "syscall"
@ -156,21 +157,21 @@ func GetPlatformInformation() (string, string, string, error) {
version := "" version := ""
lsb, err := getLSB() lsb, err := getLSB()
if err != nil{ if err != nil {
lsb = LSB{} lsb = &LSB{}
} }
if pathExists("/etc/oracle-release") { if pathExists("/etc/oracle-release") {
platform = "oracle" platform = "oracle"
contents, err := readLines("/etc/oracle-release") contents, err := readLines("/etc/oracle-release")
if err == nil { if err == nil {
version, _ = getRedhatishVersion(contents) version = getRedhatishVersion(contents)
} }
} else if pathExists("/etc/enterprise-release") { } else if pathExists("/etc/enterprise-release") {
platform = "oracle" platform = "oracle"
contents, err := readLines("/etc/enterprise-release") contents, err := readLines("/etc/enterprise-release")
if err == nil { if err == nil {
version, _ = getRedhatishVersion(contents) version = getRedhatishVersion(contents)
} }
} else if pathExists("/etc/debian_version") { } else if pathExists("/etc/debian_version") {
if lsb.ID == "Ubuntu" { if lsb.ID == "Ubuntu" {
@ -193,20 +194,20 @@ func GetPlatformInformation() (string, string, string, error) {
} else if pathExists("/etc/redhat-release") { } else if pathExists("/etc/redhat-release") {
contents, err := readLines("/etc/redhat-release") contents, err := readLines("/etc/redhat-release")
if err == nil { if err == nil {
version, _ = getRedhatishVersion(contents) version = getRedhatishVersion(contents)
platform, _ = getRedhatishPlatform(contents) platform = getRedhatishPlatform(contents)
} }
} else if pathExists("/etc/system-release") { } else if pathExists("/etc/system-release") {
contents, err := readLines("/etc/system-release") contents, err := readLines("/etc/system-release")
if err == nil { if err == nil {
version, _ = getRedhatishVersion(contents) version = getRedhatishVersion(contents)
platform, _ = getRedhatishPlatform(contents) platform = getRedhatishPlatform(contents)
} }
} else if pathExists("/etc/gentoo-release") { } else if pathExists("/etc/gentoo-release") {
platform = "gentoo" platform = "gentoo"
contents, err := readLines("/etc/gentoo-release") contents, err := readLines("/etc/gentoo-release")
if err == nil { if err == nil {
version, _ = getRedhatishVersion(contents) version = getRedhatishVersion(contents)
} }
// TODO: suse detection // TODO: suse detection
// TODO: slackware detecion // TODO: slackware detecion
@ -253,12 +254,28 @@ func GetPlatformInformation() (string, string, string, error) {
} }
func getRedhatishVersion(contents []string) (string, error) { func getRedhatishVersion(contents []string) string {
return "", nil c := strings.ToLower(strings.Join(contents, ""))
if strings.Contains(c, "rawhide") {
return "rawhide"
}
if matches := regexp.MustCompile(`release (\d[\d.]*)`).FindStringSubmatch(c); matches != nil {
return matches[1]
} else {
return ""
}
} }
func getRedhatishPlatform(contents []string) (string, error) { func getRedhatishPlatform(contents []string) string {
return "", nil c := strings.ToLower(strings.Join(contents, ""))
if strings.Contains(c, "red hat") {
return "redhat"
}
f := strings.Split(c, " ")
return f[0]
} }
func GetVirtualization() (string, string, error) { func GetVirtualization() (string, string, error) {

61
host_linux_test.go Normal file
View File

@ -0,0 +1,61 @@
// +build linux
package gopsutil
import (
"testing"
)
func TestGetRedhatishVersion(t *testing.T) {
var ret string
c := []string{"Rawhide"}
ret = getRedhatishVersion(c)
if ret != "rawhide" {
t.Errorf("Could not get version rawhide: %v", ret)
}
c = []string{"Fedora release 15 (Lovelock)"}
ret = getRedhatishVersion(c)
if ret != "15" {
t.Errorf("Could not get version fedora: %v", ret)
}
c = []string{"Enterprise Linux Server release 5.5 (Carthage)"}
ret = getRedhatishVersion(c)
if ret != "5.5" {
t.Errorf("Could not get version redhat enterprise: %v", ret)
}
c = []string{""}
ret = getRedhatishVersion(c)
if ret != "" {
t.Errorf("Could not get version with no value: %v", ret)
}
}
func TestGetRedhatishPlatform(t *testing.T) {
var ret string
c := []string{"red hat"}
ret = getRedhatishPlatform(c)
if ret != "redhat" {
t.Errorf("Could not get platform redhat: %v", ret)
}
c = []string{"Fedora release 15 (Lovelock)"}
ret = getRedhatishPlatform(c)
if ret != "fedora" {
t.Errorf("Could not get platform fedora: %v", ret)
}
c = []string{"Enterprise Linux Server release 5.5 (Carthage)"}
ret = getRedhatishPlatform(c)
if ret != "enterprise" {
t.Errorf("Could not get platform redhat enterprise: %v", ret)
}
c = []string{""}
ret = getRedhatishPlatform(c)
if ret != "" {
t.Errorf("Could not get platform with no value: %v", ret)
}
}