common: Rename StringContains to StringsContains and add StringsHas.

pull/58/head
Shirou WAKAYAMA 10 years ago
parent 9822959f18
commit b5ffc220a6

@ -110,8 +110,8 @@ func mustParseFloat64(val string) float64 {
return vv return vv
} }
// Check the target string slice containes src or not // StringsHas checks the target string slice containes src or not
func StringContains(target []string, src string) bool { func StringsHas(target []string, src string) bool {
for _, t := range target { for _, t := range target {
if strings.TrimSpace(t) == src { if strings.TrimSpace(t) == src {
return true return true
@ -120,6 +120,16 @@ func StringContains(target []string, src string) bool {
return false return false
} }
// StringsContains checks the src in any string of the target string slice
func StringsContains(target []string, src string) bool {
for _, t := range target {
if strings.Contains(t, src) {
return true
}
}
return false
}
// get struct attributes. // get struct attributes.
// This method is used only for debugging platform dependent code. // This method is used only for debugging platform dependent code.
func attributes(m interface{}) map[string]reflect.Type { func attributes(m interface{}) map[string]reflect.Type {

@ -70,12 +70,12 @@ func TestmustParseFloat64(t *testing.T) {
t.Error("could not parse") t.Error("could not parse")
} }
} }
func TestStringContains(t *testing.T) { func TestStringsContains(t *testing.T) {
target, err := ReadLines("common_test.go") target, err := ReadLines("common_test.go")
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
if !StringContains(target, "func TestStringContains(t *testing.T) {") { if !StringsContains(target, "func TestStringsContains(t *testing.T) {") {
t.Error("cloud not test correctly") t.Error("cloud not test correctly")
} }
} }

@ -39,7 +39,7 @@ func TestDisk_io_counters(t *testing.T) {
t.Errorf("error %v", err) t.Errorf("error %v", err)
} }
if len(ret) == 0 { if len(ret) == 0 {
t.Errorf("ret is empty", ret) t.Errorf("ret is empty, %v", ret)
} }
empty := DiskIOCountersStat{} empty := DiskIOCountersStat{}
for part, io := range ret { for part, io := range ret {

@ -328,7 +328,7 @@ func GetVirtualization() (string, string, error) {
if common.PathExists("/proc/xen/capabilities") { if common.PathExists("/proc/xen/capabilities") {
contents, err := common.ReadLines("/proc/xen/capabilities") contents, err := common.ReadLines("/proc/xen/capabilities")
if err == nil { if err == nil {
if common.StringContains(contents, "control_d") { if common.StringsContains(contents, "control_d") {
role = "host" role = "host"
} }
} }
@ -337,13 +337,13 @@ func GetVirtualization() (string, string, error) {
if common.PathExists("/proc/modules") { if common.PathExists("/proc/modules") {
contents, err := common.ReadLines("/proc/modules") contents, err := common.ReadLines("/proc/modules")
if err == nil { if err == nil {
if common.StringContains(contents, "kvm") { if common.StringsContains(contents, "kvm") {
system = "kvm" system = "kvm"
role = "host" role = "host"
} else if common.StringContains(contents, "vboxdrv") { } else if common.StringsContains(contents, "vboxdrv") {
system = "vbox" system = "vbox"
role = "host" role = "host"
} else if common.StringContains(contents, "vboxguest") { } else if common.StringsContains(contents, "vboxguest") {
system = "vbox" system = "vbox"
role = "guest" role = "guest"
} }
@ -353,9 +353,9 @@ func GetVirtualization() (string, string, error) {
if common.PathExists("/proc/cpuinfo") { if common.PathExists("/proc/cpuinfo") {
contents, err := common.ReadLines("/proc/cpuinfo") contents, err := common.ReadLines("/proc/cpuinfo")
if err == nil { if err == nil {
if common.StringContains(contents, "QEMU Virtual CPU") || if common.StringsContains(contents, "QEMU Virtual CPU") ||
common.StringContains(contents, "Common KVM processor") || common.StringsContains(contents, "Common KVM processor") ||
common.StringContains(contents, "Common 32-bit KVM processor") { common.StringsContains(contents, "Common 32-bit KVM processor") {
system = "kvm" system = "kvm"
role = "guest" role = "guest"
} }
@ -376,8 +376,8 @@ func GetVirtualization() (string, string, error) {
contents, err := common.ReadLines("/proc/self/status") contents, err := common.ReadLines("/proc/self/status")
if err == nil { if err == nil {
if common.StringContains(contents, "s_context:") || if common.StringsContains(contents, "s_context:") ||
common.StringContains(contents, "VxID:") { common.StringsContains(contents, "VxID:") {
system = "linux-vserver" system = "linux-vserver"
} }
// TODO: guest or host // TODO: guest or host
@ -387,9 +387,8 @@ func GetVirtualization() (string, string, error) {
if common.PathExists("/proc/self/cgroup") { if common.PathExists("/proc/self/cgroup") {
contents, err := common.ReadLines("/proc/self/cgroup") contents, err := common.ReadLines("/proc/self/cgroup")
if err == nil { if err == nil {
if common.StringsContains(contents, "lxc") ||
if common.StringContains(contents, "lxc") || common.StringsContains(contents, "docker") {
common.StringContains(contents, "docker") {
system = "lxc" system = "lxc"
role = "guest" role = "guest"
} else if common.PathExists("/usr/bin/lxc-version") { // TODO: which } else if common.PathExists("/usr/bin/lxc-version") { // TODO: which

@ -26,7 +26,7 @@ func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
// skip first line // skip first line
continue continue
} }
if common.StringContains(exists, values[0]) { if common.StringsHas(exists, values[0]) {
// skip if already get // skip if already get
continue continue
} }

@ -25,7 +25,7 @@ func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
if len(values) < 1 || values[0] == "Name" { if len(values) < 1 || values[0] == "Name" {
continue continue
} }
if common.StringContains(exists, values[0]) { if common.StringsHas(exists, values[0]) {
// skip if already get // skip if already get
continue continue
} }

Loading…
Cancel
Save