Merge pull request #1111 from vieux/no_json_manual

do not generate JSON by hand
This commit is contained in:
Andrea Luzzardi 2015-08-04 15:30:30 -07:00
commit 3bb4373f9f
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) { callback := func(what, status string) {
if status == "" { 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 { } 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) 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") tag := r.Form.Get("tag")
callback := func(what, status string) { 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) 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) wf := NewWriteFlusher(w)
callback := func(what, status string) { callback := func(what, status string) {
if status == "" { if status == "" {
fmt.Fprintf(wf, "%s:Loading Image...\n", what) sendJSONMessage(wf, what, "Loading Image...")
} else { } 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) c.cluster.Load(r.Body, callback)

View File

@ -2,6 +2,7 @@ package api
import ( import (
"crypto/tls" "crypto/tls"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -20,6 +21,19 @@ func httpError(w http.ResponseWriter, err string, status int) {
http.Error(w, err, status) 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) { func newClientAndScheme(tlsConfig *tls.Config) (*http.Client, string) {
if tlsConfig != nil { if tlsConfig != nil {
return &http.Client{Transport: &http.Transport{TLSClientConfig: tlsConfig}}, "https" return &http.Client{Transport: &http.Transport{TLSClientConfig: tlsConfig}}, "https"

View File

@ -33,15 +33,15 @@ type Cluster interface {
// Pull images // Pull images
// `callback` can be called multiple time // `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 // `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 // Import image
// `callback` can be called multiple time // `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" // `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 // Load images
// `callback` can be called multiple time // `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 // 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 // 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 // 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 var wg sync.WaitGroup
c.RLock() c.RLock()
@ -347,7 +347,7 @@ func (c *Cluster) Pull(name string, authConfig *dockerclient.AuthConfig, callbac
} }
// Load image // 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 var wg sync.WaitGroup
c.RLock() c.RLock()