do not generate JSON by hand

Signed-off-by: Victor Vieux <vieux@docker.com>
This commit is contained in:
Victor Vieux 2015-07-30 17:46:26 -07:00
parent 95c4c9467e
commit 7ec33a81b5
5 changed files with 27 additions and 13 deletions

View File

@ -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)

View File

@ -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"

View File

@ -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

View File

@ -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)) {
}

View File

@ -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()