From b5ffc220a671e8ed5eb2c0136b24524f708ea361 Mon Sep 17 00:00:00 2001 From: Shirou WAKAYAMA Date: Tue, 21 Jul 2015 15:25:04 +0900 Subject: [PATCH] common: Rename StringContains to StringsContains and add StringsHas. --- common/common.go | 14 ++++++++++++-- common/common_test.go | 4 ++-- disk/disk_test.go | 2 +- host/host_linux.go | 23 +++++++++++------------ net/net_darwin.go | 2 +- net/net_freebsd.go | 2 +- 6 files changed, 28 insertions(+), 19 deletions(-) diff --git a/common/common.go b/common/common.go index 1b77e36..a795361 100644 --- a/common/common.go +++ b/common/common.go @@ -110,8 +110,8 @@ func mustParseFloat64(val string) float64 { return vv } -// Check the target string slice containes src or not -func StringContains(target []string, src string) bool { +// StringsHas checks the target string slice containes src or not +func StringsHas(target []string, src string) bool { for _, t := range target { if strings.TrimSpace(t) == src { return true @@ -120,6 +120,16 @@ func StringContains(target []string, src string) bool { 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. // This method is used only for debugging platform dependent code. func attributes(m interface{}) map[string]reflect.Type { diff --git a/common/common_test.go b/common/common_test.go index 231fb47..b2660b2 100644 --- a/common/common_test.go +++ b/common/common_test.go @@ -70,12 +70,12 @@ func TestmustParseFloat64(t *testing.T) { t.Error("could not parse") } } -func TestStringContains(t *testing.T) { +func TestStringsContains(t *testing.T) { target, err := ReadLines("common_test.go") if err != nil { 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") } } diff --git a/disk/disk_test.go b/disk/disk_test.go index d83160e..ca0f53d 100644 --- a/disk/disk_test.go +++ b/disk/disk_test.go @@ -39,7 +39,7 @@ func TestDisk_io_counters(t *testing.T) { t.Errorf("error %v", err) } if len(ret) == 0 { - t.Errorf("ret is empty", ret) + t.Errorf("ret is empty, %v", ret) } empty := DiskIOCountersStat{} for part, io := range ret { diff --git a/host/host_linux.go b/host/host_linux.go index 24d829f..a0093c3 100644 --- a/host/host_linux.go +++ b/host/host_linux.go @@ -328,7 +328,7 @@ func GetVirtualization() (string, string, error) { if common.PathExists("/proc/xen/capabilities") { contents, err := common.ReadLines("/proc/xen/capabilities") if err == nil { - if common.StringContains(contents, "control_d") { + if common.StringsContains(contents, "control_d") { role = "host" } } @@ -337,13 +337,13 @@ func GetVirtualization() (string, string, error) { if common.PathExists("/proc/modules") { contents, err := common.ReadLines("/proc/modules") if err == nil { - if common.StringContains(contents, "kvm") { + if common.StringsContains(contents, "kvm") { system = "kvm" role = "host" - } else if common.StringContains(contents, "vboxdrv") { + } else if common.StringsContains(contents, "vboxdrv") { system = "vbox" role = "host" - } else if common.StringContains(contents, "vboxguest") { + } else if common.StringsContains(contents, "vboxguest") { system = "vbox" role = "guest" } @@ -353,9 +353,9 @@ func GetVirtualization() (string, string, error) { if common.PathExists("/proc/cpuinfo") { contents, err := common.ReadLines("/proc/cpuinfo") if err == nil { - if common.StringContains(contents, "QEMU Virtual CPU") || - common.StringContains(contents, "Common KVM processor") || - common.StringContains(contents, "Common 32-bit KVM processor") { + if common.StringsContains(contents, "QEMU Virtual CPU") || + common.StringsContains(contents, "Common KVM processor") || + common.StringsContains(contents, "Common 32-bit KVM processor") { system = "kvm" role = "guest" } @@ -376,8 +376,8 @@ func GetVirtualization() (string, string, error) { contents, err := common.ReadLines("/proc/self/status") if err == nil { - if common.StringContains(contents, "s_context:") || - common.StringContains(contents, "VxID:") { + if common.StringsContains(contents, "s_context:") || + common.StringsContains(contents, "VxID:") { system = "linux-vserver" } // TODO: guest or host @@ -387,9 +387,8 @@ func GetVirtualization() (string, string, error) { if common.PathExists("/proc/self/cgroup") { contents, err := common.ReadLines("/proc/self/cgroup") if err == nil { - - if common.StringContains(contents, "lxc") || - common.StringContains(contents, "docker") { + if common.StringsContains(contents, "lxc") || + common.StringsContains(contents, "docker") { system = "lxc" role = "guest" } else if common.PathExists("/usr/bin/lxc-version") { // TODO: which diff --git a/net/net_darwin.go b/net/net_darwin.go index 2c4be76..5dc0c11 100644 --- a/net/net_darwin.go +++ b/net/net_darwin.go @@ -26,7 +26,7 @@ func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) { // skip first line continue } - if common.StringContains(exists, values[0]) { + if common.StringsHas(exists, values[0]) { // skip if already get continue } diff --git a/net/net_freebsd.go b/net/net_freebsd.go index b9e1428..703d321 100644 --- a/net/net_freebsd.go +++ b/net/net_freebsd.go @@ -25,7 +25,7 @@ func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) { if len(values) < 1 || values[0] == "Name" { continue } - if common.StringContains(exists, values[0]) { + if common.StringsHas(exists, values[0]) { // skip if already get continue }