From 809829811142486eb704e26ce122459ef300e599 Mon Sep 17 00:00:00 2001 From: Shirou WAKAYAMA Date: Thu, 1 May 2014 18:43:11 +0900 Subject: [PATCH] implements String() which return as JSON to every structs. --- README.rst | 8 +++----- cpu.go | 6 ++++++ disk.go | 19 +++++++++++++++++++ disk_test.go | 7 +++---- host.go | 14 ++++++++++++++ load.go | 9 +++++++++ mem.go | 14 ++++++++++++++ mem_test.go | 7 ++----- net.go | 19 +++++++++++++++++++ process.go | 29 +++++++++++++++++++++++++++++ process_test.go | 4 +--- 11 files changed, 119 insertions(+), 17 deletions(-) diff --git a/README.rst b/README.rst index e1870a3..4776b38 100644 --- a/README.rst +++ b/README.rst @@ -26,7 +26,6 @@ Usage :: import ( - "encoding/json" "fmt" "github.com/shirou/gopsutil" @@ -35,12 +34,11 @@ Usage func main() { v, _ := gopsutil.VirtualMemory() - // return value is struct + // almost every return value is struct fmt.Printf("Total: %v, Free:%v, UsedPercent:%f%%\n", v.Total, v.Free, v.UsedPercent) - // convert to JSON - d, _ := json.Marshal(v) - fmt.Printf("%s\n", d) + // convert to JSON. String() is also implemented + fmt.Println(d) } The output is below. diff --git a/cpu.go b/cpu.go index 200c1ff..ec139bd 100644 --- a/cpu.go +++ b/cpu.go @@ -1,6 +1,7 @@ package gopsutil import ( + "encoding/json" "runtime" ) @@ -22,3 +23,8 @@ type CPUTimesStat struct { func CPUCounts(logical bool) (int, error) { return runtime.NumCPU(), nil } + +func (c CPUTimesStat) String() string { + s, _ := json.Marshal(c) + return string(s) +} diff --git a/disk.go b/disk.go index 807b41a..45cb36b 100644 --- a/disk.go +++ b/disk.go @@ -1,5 +1,9 @@ package gopsutil +import ( + "encoding/json" +) + type DiskUsageStat struct { Path string `json:"path"` Total uint64 `json:"total"` @@ -24,3 +28,18 @@ type DiskIOCountersStat struct { WriteTime uint64 `json:"writeTime"` Name string `json:"name"` } + +func (d DiskUsageStat) String() string { + s, _ := json.Marshal(d) + return string(s) +} + +func (d DiskPartitionStat) String() string { + s, _ := json.Marshal(d) + return string(s) +} + +func (d DiskIOCountersStat) String() string { + s, _ := json.Marshal(d) + return string(s) +} diff --git a/disk_test.go b/disk_test.go index f92cdd0..4a7bc93 100644 --- a/disk_test.go +++ b/disk_test.go @@ -1,7 +1,7 @@ package gopsutil import ( - // "fmt" + "fmt" "runtime" "testing" ) @@ -11,12 +11,11 @@ func TestDisk_usage(t *testing.T) { if runtime.GOOS == "windows" { path = "C:" } - _, err := DiskUsage(path) + v, err := DiskUsage(path) if err != nil { t.Errorf("error %v", err) } - // d, _ := json.Marshal(v) - // fmt.Printf("%s\n", d) + fmt.Println(v) } func TestDisk_partitions(t *testing.T) { diff --git a/host.go b/host.go index d332d33..cc4be78 100644 --- a/host.go +++ b/host.go @@ -1,5 +1,9 @@ package gopsutil +import ( + "encoding/json" +) + // A HostInfoStat describes the host status. // This is not in the psutil but it useful. type HostInfoStat struct { @@ -14,3 +18,13 @@ type UserStat struct { Host string `json:"host"` Started int `json:"started"` } + +func (h HostInfoStat) String() string { + s, _ := json.Marshal(h) + return string(s) +} + +func (u UserStat) String() string { + s, _ := json.Marshal(u) + return string(s) +} diff --git a/load.go b/load.go index 75da167..7b124d6 100644 --- a/load.go +++ b/load.go @@ -1,7 +1,16 @@ package gopsutil +import ( + "encoding/json" +) + type LoadAvgStat struct { Load1 float64 `json:"load1"` Load5 float64 `json:"load5"` Load15 float64 `json:"load15"` } + +func (l LoadAvgStat) String() string { + s, _ := json.Marshal(l) + return string(s) +} diff --git a/mem.go b/mem.go index d612b84..6f2e293 100644 --- a/mem.go +++ b/mem.go @@ -1,5 +1,9 @@ package gopsutil +import ( + "encoding/json" +) + type VirtualMemoryStat struct { Total uint64 `json:"total"` Available uint64 `json:"available"` @@ -22,3 +26,13 @@ type SwapMemoryStat struct { Sin uint64 `json:"sin"` Sout uint64 `json:"sout"` } + +func (m VirtualMemoryStat) String() string { + s, _ := json.Marshal(m) + return string(s) +} + +func (m SwapMemoryStat) String() string { + s, _ := json.Marshal(m) + return string(s) +} diff --git a/mem_test.go b/mem_test.go index 347ae89..31eb10e 100644 --- a/mem_test.go +++ b/mem_test.go @@ -1,7 +1,6 @@ package gopsutil import ( - "encoding/json" "fmt" "testing" ) @@ -11,8 +10,7 @@ func TestVirtual_memory(t *testing.T) { if err != nil { t.Errorf("error %v", err) } - d, _ := json.Marshal(v) - fmt.Printf("%s\n", d) + fmt.Println(v) } func TestSwap_memory(t *testing.T) { @@ -20,6 +18,5 @@ func TestSwap_memory(t *testing.T) { if err != nil { t.Errorf("error %v", err) } - d, _ := json.Marshal(v) - fmt.Printf("%s\n", d) + fmt.Println(v) } diff --git a/net.go b/net.go index 4bca1c9..7864bd0 100644 --- a/net.go +++ b/net.go @@ -1,5 +1,9 @@ package gopsutil +import ( + "encoding/json" +) + type NetIOCountersStat struct { Name string `json:"name"` // interface name BytesSent uint64 `json:"bytes_sent"` // number of bytes sent @@ -26,3 +30,18 @@ type NetConnectionStat struct { Status string `json:"status"` Pid int32 `json:"pid"` } + +func (n NetConnectionStat) String() string { + s, _ := json.Marshal(n) + return string(s) +} + +func (n NetIOCountersStat) String() string { + s, _ := json.Marshal(n) + return string(s) +} + +func (a Addr) String() string { + s, _ := json.Marshal(a) + return string(s) +} diff --git a/process.go b/process.go index 35c548a..64a1627 100644 --- a/process.go +++ b/process.go @@ -1,5 +1,9 @@ package gopsutil +import ( + "encoding/json" +) + type Process struct { Pid int32 `json:"pid"` } @@ -27,6 +31,31 @@ type IOCountersStat struct { WriteBytes int32 `json:"write_bytes"` } +func (p Process) String() string { + s, _ := json.Marshal(p) + return string(s) +} + +func (o OpenFilesStat) String() string { + s, _ := json.Marshal(o) + return string(s) +} + +func (m MemoryInfoStat) String() string { + s, _ := json.Marshal(m) + return string(s) +} + +func (r RlimitStat) String() string { + s, _ := json.Marshal(r) + return string(s) +} + +func (i IOCountersStat) String() string { + s, _ := json.Marshal(i) + return string(s) +} + func PidExists(pid int32) (bool, error) { pids, err := Pids() if err != nil { diff --git a/process_test.go b/process_test.go index 7612144..501b932 100644 --- a/process_test.go +++ b/process_test.go @@ -1,7 +1,6 @@ package gopsutil import ( - "encoding/json" "fmt" "os" "runtime" @@ -54,8 +53,7 @@ func Test_NewProcess(t *testing.T) { t.Errorf("error %v", err) } - d, _ := json.Marshal(ret) - fmt.Println(string(d)) + fmt.Println(ret) } func Test_Process_memory_maps(t *testing.T) {