From 7ec33a81b5e4453e92869759a1f0a47f6cbbc795 Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Thu, 30 Jul 2015 17:46:26 -0700 Subject: [PATCH] do not generate JSON by hand Signed-off-by: Victor Vieux --- api/handlers.go | 10 +++++----- api/utils.go | 14 ++++++++++++++ cluster/cluster.go | 8 ++++---- cluster/mesos/cluster.go | 4 ++-- cluster/swarm/cluster.go | 4 ++-- 5 files changed, 27 insertions(+), 13 deletions(-) diff --git a/api/handlers.go b/api/handlers.go index 9930c48de1..f4a7b45a7a 100644 --- a/api/handlers.go +++ b/api/handlers.go @@ -385,9 +385,9 @@ func postImagesCreate(c *context, w http.ResponseWriter, r *http.Request) { } callback := func(what, status string) { if status == "" { - fmt.Fprintf(wf, "{%q:%q,%q:\"Pulling %s...\",%q:{}}", "id", what, "status", image, "progressDetail") + sendJSONMessage(wf, what, fmt.Sprintf("Pulling %s...", image)) } else { - fmt.Fprintf(wf, "{%q:%q,%q:\"Pulling %s... : %s\",%q:{}}", "id", what, "status", image, status, "progressDetail") + sendJSONMessage(wf, what, fmt.Sprintf("Pulling %s... : %s", image, status)) } } c.cluster.Pull(image, &authConfig, callback) @@ -397,7 +397,7 @@ func postImagesCreate(c *context, w http.ResponseWriter, r *http.Request) { tag := r.Form.Get("tag") callback := func(what, status string) { - fmt.Fprintf(wf, "{%q:%q,%q:\"%s\"}", "id", what, "status", status) + sendJSONMessage(wf, what, status) } c.cluster.Import(source, repo, tag, r.Body, callback) } @@ -410,9 +410,9 @@ func postImagesLoad(c *context, w http.ResponseWriter, r *http.Request) { wf := NewWriteFlusher(w) callback := func(what, status string) { if status == "" { - fmt.Fprintf(wf, "%s:Loading Image...\n", what) + sendJSONMessage(wf, what, "Loading Image...") } else { - fmt.Fprintf(wf, "%s:Loading Image... %s\n", what, status) + sendJSONMessage(wf, what, fmt.Sprintf("Loading Image... : %s", status)) } } c.cluster.Load(r.Body, callback) diff --git a/api/utils.go b/api/utils.go index e4e2c4146e..b52bacec88 100644 --- a/api/utils.go +++ b/api/utils.go @@ -2,6 +2,7 @@ package api import ( "crypto/tls" + "encoding/json" "errors" "fmt" "io" @@ -20,6 +21,19 @@ func httpError(w http.ResponseWriter, err string, status int) { http.Error(w, err, status) } +func sendJSONMessage(w io.Writer, id, status string) { + message := struct { + ID string `json:"id,omitempty"` + Status string `json:"status,omitempty"` + Progress interface{} `json:"progressDetail,omitempty"` + }{ + id, + status, + struct{}{}, // this is required by the docker cli to have a proper display + } + json.NewEncoder(w).Encode(message) +} + func newClientAndScheme(tlsConfig *tls.Config) (*http.Client, string) { if tlsConfig != nil { return &http.Client{Transport: &http.Transport{TLSClientConfig: tlsConfig}}, "https" diff --git a/cluster/cluster.go b/cluster/cluster.go index f1c5fd4120..cfc3b22861 100644 --- a/cluster/cluster.go +++ b/cluster/cluster.go @@ -33,15 +33,15 @@ type Cluster interface { // Pull images // `callback` can be called multiple time - // `what` is what is being pulled + // `where` is where it is being pulled // `status` is the current status, like "", "in progress" or "downloaded - Pull(name string, authConfig *dockerclient.AuthConfig, callback func(what, status string)) + Pull(name string, authConfig *dockerclient.AuthConfig, callback func(where, status string)) // Import image // `callback` can be called multiple time - // `what` is what is being imported + // `where` is where it is being imported // `status` is the current status, like "", "in progress" or "imported" - Import(source string, repository string, tag string, imageReader io.Reader, callback func(what, status string)) + Import(source string, repository string, tag string, imageReader io.Reader, callback func(where, status string)) // Load images // `callback` can be called multiple time diff --git a/cluster/mesos/cluster.go b/cluster/mesos/cluster.go index c9520f0389..bfdd3e0766 100644 --- a/cluster/mesos/cluster.go +++ b/cluster/mesos/cluster.go @@ -269,12 +269,12 @@ func (c *Cluster) RemoveImage(image *cluster.Image) ([]*dockerclient.ImageDelete } // Pull will pull images on the cluster nodes -func (c *Cluster) Pull(name string, authConfig *dockerclient.AuthConfig, callback func(what, status string)) { +func (c *Cluster) Pull(name string, authConfig *dockerclient.AuthConfig, callback func(where, status string)) { } // Load images -func (c *Cluster) Load(imageReader io.Reader, callback func(what, status string)) { +func (c *Cluster) Load(imageReader io.Reader, callback func(where, status string)) { } diff --git a/cluster/swarm/cluster.go b/cluster/swarm/cluster.go index e824ab67f0..3226f0c40c 100644 --- a/cluster/swarm/cluster.go +++ b/cluster/swarm/cluster.go @@ -318,7 +318,7 @@ func (c *Cluster) RemoveImages(name string) ([]*dockerclient.ImageDelete, error) } // Pull is exported -func (c *Cluster) Pull(name string, authConfig *dockerclient.AuthConfig, callback func(what, status string)) { +func (c *Cluster) Pull(name string, authConfig *dockerclient.AuthConfig, callback func(where, status string)) { var wg sync.WaitGroup c.RLock() @@ -347,7 +347,7 @@ func (c *Cluster) Pull(name string, authConfig *dockerclient.AuthConfig, callbac } // Load image -func (c *Cluster) Load(imageReader io.Reader, callback func(what, status string)) { +func (c *Cluster) Load(imageReader io.Reader, callback func(where, status string)) { var wg sync.WaitGroup c.RLock()