提交 76866feb 编写于 作者: W WAKAYAMA Shirou

implement redhatishVersion and platform on Linux.

上级 710c0290
......@@ -3,7 +3,6 @@ package gopsutil
import (
"fmt"
"testing"
)
func TestCpu_times(t *testing.T) {
......
......@@ -8,6 +8,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"regexp"
"runtime"
"strings"
"syscall"
......@@ -156,21 +157,21 @@ func GetPlatformInformation() (string, string, string, error) {
version := ""
lsb, err := getLSB()
if err != nil{
lsb = LSB{}
if err != nil {
lsb = &LSB{}
}
if pathExists("/etc/oracle-release") {
platform = "oracle"
contents, err := readLines("/etc/oracle-release")
if err == nil {
version, _ = getRedhatishVersion(contents)
version = getRedhatishVersion(contents)
}
} else if pathExists("/etc/enterprise-release") {
platform = "oracle"
contents, err := readLines("/etc/enterprise-release")
if err == nil {
version, _ = getRedhatishVersion(contents)
version = getRedhatishVersion(contents)
}
} else if pathExists("/etc/debian_version") {
if lsb.ID == "Ubuntu" {
......@@ -193,20 +194,20 @@ func GetPlatformInformation() (string, string, string, error) {
} else if pathExists("/etc/redhat-release") {
contents, err := readLines("/etc/redhat-release")
if err == nil {
version, _ = getRedhatishVersion(contents)
platform, _ = getRedhatishPlatform(contents)
version = getRedhatishVersion(contents)
platform = getRedhatishPlatform(contents)
}
} else if pathExists("/etc/system-release") {
contents, err := readLines("/etc/system-release")
if err == nil {
version, _ = getRedhatishVersion(contents)
platform, _ = getRedhatishPlatform(contents)
version = getRedhatishVersion(contents)
platform = getRedhatishPlatform(contents)
}
} else if pathExists("/etc/gentoo-release") {
platform = "gentoo"
contents, err := readLines("/etc/gentoo-release")
if err == nil {
version, _ = getRedhatishVersion(contents)
version = getRedhatishVersion(contents)
}
// TODO: suse detection
// TODO: slackware detecion
......@@ -253,12 +254,28 @@ func GetPlatformInformation() (string, string, string, error) {
}
func getRedhatishVersion(contents []string) (string, error) {
return "", nil
func getRedhatishVersion(contents []string) string {
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) {
return "", nil
func getRedhatishPlatform(contents []string) string {
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) {
......
// +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)
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册