From 85b9338205da0c8f1d62f277db342cf4b9feaf13 Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Thu, 12 Dec 2013 13:35:50 -0800 Subject: [PATCH] add GetenvInt64 ans SetenvInt64 --- commands.go | 17 ++++++----------- engine/env.go | 14 +++++++++++--- engine/job.go | 12 ++++++++++-- integration/api_test.go | 2 +- server.go | 12 ++++++------ 5 files changed, 34 insertions(+), 23 deletions(-) diff --git a/commands.go b/commands.go index 982d4b3d8..df49b5449 100644 --- a/commands.go +++ b/commands.go @@ -448,17 +448,12 @@ func (cli *DockerCli) CmdInfo(args ...string) error { fmt.Fprintf(cli.out, "Containers: %d\n", remoteInfo.GetInt("Containers")) fmt.Fprintf(cli.out, "Images: %d\n", remoteInfo.GetInt("Images")) fmt.Fprintf(cli.out, "Driver: %s\n", remoteInfo.Get("Driver")) - - //FIXME:Cleanup this mess - DriverStatus := remoteInfo.GetJson("DriverStatus") - if DriverStatus != nil { - if tab, ok := DriverStatus.([]interface{}); ok { - for _, line := range tab { - if pair, ok := line.([]interface{}); ok { - fmt.Fprintf(cli.out, " %s: %s\n", pair[0], pair[1]) - } - } - } + var driverStatus [][2]string + if err := remoteInfo.GetJson("DriverStatus", &driverStatus); err != nil { + return err + } + for _, pair := range driverStatus { + fmt.Fprintf(cli.out, " %s: %s\n", pair[0], pair[1]) } if remoteInfo.GetBool("Debug") || os.Getenv("DEBUG") != "" { fmt.Fprintf(cli.out, "Debug mode (server): %v\n", remoteInfo.GetBool("Debug")) diff --git a/engine/env.go b/engine/env.go index 81c76d5ad..a65c8438d 100644 --- a/engine/env.go +++ b/engine/env.go @@ -51,7 +51,11 @@ func (env *Env) SetBool(key string, value bool) { } } -func (env *Env) GetInt(key string) int64 { +func (env *Env) GetInt(key string) int { + return int(env.GetInt64(key)) +} + +func (env *Env) GetInt64(key string) int64 { s := strings.Trim(env.Get(key), " \t") val, err := strconv.ParseInt(s, 10, 64) if err != nil { @@ -60,7 +64,11 @@ func (env *Env) GetInt(key string) int64 { return val } -func (env *Env) SetInt(key string, value int64) { +func (env *Env) SetInt(key string, value int) { + env.Set(key, fmt.Sprintf("%d", value)) +} + +func (env *Env) SetInt64(key string, value int64) { env.Set(key, fmt.Sprintf("%d", value)) } @@ -145,7 +153,7 @@ func (env *Env) SetAuto(k string, v interface{}) { // encoding/json decodes integers to float64, but cannot encode them back. // (See http://golang.org/src/pkg/encoding/json/decode.go#L46) if fval, ok := v.(float64); ok { - env.SetInt(k, int64(fval)) + env.SetInt64(k, int64(fval)) } else if sval, ok := v.(string); ok { env.Set(k, sval) } else if val, err := json.Marshal(v); err == nil { diff --git a/engine/job.go b/engine/job.go index e960cffda..68b1715d9 100644 --- a/engine/job.go +++ b/engine/job.go @@ -113,11 +113,19 @@ func (job *Job) SetenvBool(key string, value bool) { job.env.SetBool(key, value) } -func (job *Job) GetenvInt(key string) int64 { +func (job *Job) GetenvInt64(key string) int64 { + return job.env.GetInt64(key) +} + +func (job *Job) GetenvInt(key string) int { return job.env.GetInt(key) } -func (job *Job) SetenvInt(key string, value int64) { +func (job *Job) SetenvInt64(key string, value int64) { + job.env.SetInt64(key, value) +} + +func (job *Job) SetenvInt(key string, value int) { job.env.SetInt(key, value) } diff --git a/integration/api_test.go b/integration/api_test.go index 90a63ba55..b635dd81c 100644 --- a/integration/api_test.go +++ b/integration/api_test.go @@ -81,7 +81,7 @@ func TestGetInfo(t *testing.T) { t.Fatal(err) } out.Close() - if images := i.GetInt("Images"); images != int64(len(initialImages)) { + if images := i.GetInt("Images"); images != len(initialImages) { t.Errorf("Expected images: %d, %d found", len(initialImages), images) } } diff --git a/server.go b/server.go index 1a168fe59..a0e619109 100644 --- a/server.go +++ b/server.go @@ -616,11 +616,11 @@ func (srv *Server) Images(all bool, filter string) ([]APIImages, error) { func (srv *Server) DockerInfo(job *engine.Job) engine.Status { images, _ := srv.runtime.graph.Map() - var imgcount int64 + var imgcount int if images == nil { imgcount = 0 } else { - imgcount = int64(len(images)) + imgcount = len(images) } lxcVersion := "" if output, err := exec.Command("lxc-version").CombinedOutput(); err == nil { @@ -635,7 +635,7 @@ func (srv *Server) DockerInfo(job *engine.Job) engine.Status { } v := &engine.Env{} - v.SetInt("Containers", int64(len(srv.runtime.List()))) + v.SetInt("Containers", len(srv.runtime.List())) v.SetInt("Images", imgcount) v.Set("Driver", srv.runtime.driver.String()) v.SetJson("DriverStatus", srv.runtime.driver.Status()) @@ -643,10 +643,10 @@ func (srv *Server) DockerInfo(job *engine.Job) engine.Status { v.SetBool("SwapLimit", srv.runtime.capabilities.SwapLimit) v.SetBool("IPv4Forwarding", !srv.runtime.capabilities.IPv4ForwardingDisabled) v.SetBool("Debug", os.Getenv("DEBUG") != "") - v.SetInt("NFd", int64(utils.GetTotalUsedFds())) - v.SetInt("NGoroutines", int64(runtime.NumGoroutine())) + v.SetInt("NFd", utils.GetTotalUsedFds()) + v.SetInt("NGoroutines", runtime.NumGoroutine()) v.Set("LXCVersion", lxcVersion) - v.SetInt("NEventsListener", int64(len(srv.events))) + v.SetInt("NEventsListener", len(srv.events)) v.Set("KernelVersion", kernelVersion) v.Set("IndexServerAddress", auth.IndexServerAddress()) if _, err := v.WriteTo(job.Stdout); err != nil {