1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #719 from dotcloud/json_stream-feature

* API: push, pull, import, insert -> Json Stream
This commit is contained in:
Guillaume J. Charmes 2013-05-31 16:05:15 -07:00
commit 9bc71c101c
8 changed files with 186 additions and 78 deletions

42
api.go
View file

@ -314,16 +314,25 @@ func postImagesCreate(srv *Server, version float64, w http.ResponseWriter, r *ht
tag := r.Form.Get("tag") tag := r.Form.Get("tag")
repo := r.Form.Get("repo") repo := r.Form.Get("repo")
if version > 1.0 {
w.Header().Set("Content-Type", "application/json")
}
sf := utils.NewStreamFormatter(version > 1.0)
if image != "" { //pull if image != "" { //pull
registry := r.Form.Get("registry") registry := r.Form.Get("registry")
if version > 1.0 { if err := srv.ImagePull(image, tag, registry, w, sf); err != nil {
w.Header().Set("Content-Type", "application/json") if sf.Used() {
} w.Write(sf.FormatError(err))
if err := srv.ImagePull(image, tag, registry, w, version > 1.0); err != nil { return nil
}
return err return err
} }
} else { //import } else { //import
if err := srv.ImageImport(src, repo, tag, r.Body, w); err != nil { if err := srv.ImageImport(src, repo, tag, r.Body, w, sf); err != nil {
if sf.Used() {
w.Write(sf.FormatError(err))
return nil
}
return err return err
} }
} }
@ -359,10 +368,16 @@ func postImagesInsert(srv *Server, version float64, w http.ResponseWriter, r *ht
return fmt.Errorf("Missing parameter") return fmt.Errorf("Missing parameter")
} }
name := vars["name"] name := vars["name"]
if version > 1.0 {
imgId, err := srv.ImageInsert(name, url, path, w) w.Header().Set("Content-Type", "application/json")
}
sf := utils.NewStreamFormatter(version > 1.0)
imgId, err := srv.ImageInsert(name, url, path, w, sf)
if err != nil { if err != nil {
return err if sf.Used() {
w.Write(sf.FormatError(err))
return nil
}
} }
b, err := json.Marshal(&ApiId{Id: imgId}) b, err := json.Marshal(&ApiId{Id: imgId})
if err != nil { if err != nil {
@ -382,8 +397,15 @@ func postImagesPush(srv *Server, version float64, w http.ResponseWriter, r *http
return fmt.Errorf("Missing parameter") return fmt.Errorf("Missing parameter")
} }
name := vars["name"] name := vars["name"]
if version > 1.0 {
if err := srv.ImagePush(name, registry, w); err != nil { w.Header().Set("Content-Type", "application/json")
}
sf := utils.NewStreamFormatter(version > 1.0)
if err := srv.ImagePush(name, registry, w, sf); err != nil {
if sf.Used() {
w.Write(sf.FormatError(err))
return nil
}
return err return err
} }
return nil return nil

View file

@ -61,7 +61,7 @@ func (b *buildFile) CmdFrom(name string) error {
remote = name remote = name
} }
if err := b.srv.ImagePull(remote, tag, "", b.out, false); err != nil { if err := b.srv.ImagePull(remote, tag, "", b.out, utils.NewStreamFormatter(false)); err != nil {
return err return err
} }

View file

@ -180,7 +180,8 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
return err return err
} else { } else {
// FIXME: Find a way to have a progressbar for the upload too // FIXME: Find a way to have a progressbar for the upload too
io.Copy(wField, utils.ProgressReader(ioutil.NopCloser(context), -1, os.Stdout, "Caching Context %v/%v (%v)\r", false)) sf := utils.NewStreamFormatter(false)
io.Copy(wField, utils.ProgressReader(ioutil.NopCloser(context), -1, os.Stdout, sf.FormatProgress("Caching Context", "%v/%v (%v)"), sf))
} }
multipartBody = io.MultiReader(multipartBody, boundary) multipartBody = io.MultiReader(multipartBody, boundary)
} }
@ -1367,13 +1368,9 @@ func (cli *DockerCli) stream(method, path string, in io.Reader, out io.Writer) e
} }
if resp.Header.Get("Content-Type") == "application/json" { if resp.Header.Get("Content-Type") == "application/json" {
type Message struct {
Status string `json:"status,omitempty"`
Progress string `json:"progress,omitempty"`
}
dec := json.NewDecoder(resp.Body) dec := json.NewDecoder(resp.Body)
for { for {
var m Message var m utils.JsonMessage
if err := dec.Decode(&m); err == io.EOF { if err := dec.Decode(&m); err == io.EOF {
break break
} else if err != nil { } else if err != nil {
@ -1381,6 +1378,8 @@ func (cli *DockerCli) stream(method, path string, in io.Reader, out io.Writer) e
} }
if m.Progress != "" { if m.Progress != "" {
fmt.Fprintf(out, "Downloading %s\r", m.Progress) fmt.Fprintf(out, "Downloading %s\r", m.Progress)
} else if m.Error != "" {
return fmt.Errorf(m.Error)
} else { } else {
fmt.Fprintf(out, "%s\n", m.Status) fmt.Fprintf(out, "%s\n", m.Status)
} }

View file

@ -15,10 +15,17 @@ Docker Remote API
- Default port in the docker deamon is 4243 - Default port in the docker deamon is 4243
- The API tends to be REST, but for some complex commands, like attach or pull, the HTTP connection is hijacked to transport stdout stdin and stderr - The API tends to be REST, but for some complex commands, like attach or pull, the HTTP connection is hijacked to transport stdout stdin and stderr
2. Endpoints 2. Version
==========
The current verson of the API is 1.1
Calling /images/<name>/insert is the same as calling /v1.1/images/<name>/insert
You can still call an old version of the api using /v1.0/images/<name>/insert
3. Endpoints
============ ============
2.1 Containers 3.1 Containers
-------------- --------------
List containers List containers
@ -460,7 +467,7 @@ Remove a container
:statuscode 500: server error :statuscode 500: server error
2.2 Images 3.2 Images
---------- ----------
List Images List Images
@ -549,7 +556,19 @@ Create an image
POST /images/create?fromImage=base HTTP/1.1 POST /images/create?fromImage=base HTTP/1.1
**Example response**: **Example response v1.1**:
.. sourcecode:: http
HTTP/1.1 200 OK
Content-Type: application/json
{"status":"Pulling..."}
{"progress":"1/? (n/a)"}
{"error":"Invalid..."}
...
**Example response v1.0**:
.. sourcecode:: http .. sourcecode:: http
@ -580,7 +599,19 @@ Insert a file in a image
POST /images/test/insert?path=/usr&url=myurl HTTP/1.1 POST /images/test/insert?path=/usr&url=myurl HTTP/1.1
**Example response**: **Example response v1.1**:
.. sourcecode:: http
HTTP/1.1 200 OK
Content-Type: application/json
{"status":"Inserting..."}
{"progress":"1/? (n/a)"}
{"error":"Invalid..."}
...
**Example response v1.0**:
.. sourcecode:: http .. sourcecode:: http
@ -695,7 +726,19 @@ Push an image on the registry
POST /images/test/push HTTP/1.1 POST /images/test/push HTTP/1.1
**Example response**: **Example response v1.1**:
.. sourcecode:: http
HTTP/1.1 200 OK
Content-Type: application/json
{"status":"Pushing..."}
{"progress":"1/? (n/a)"}
{"error":"Invalid..."}
...
**Example response v1.0**:
.. sourcecode:: http .. sourcecode:: http
@ -801,7 +844,7 @@ Search images
:statuscode 500: server error :statuscode 500: server error
2.3 Misc 3.3 Misc
-------- --------
Build an image from Dockerfile via stdin Build an image from Dockerfile via stdin

View file

@ -165,7 +165,8 @@ func (graph *Graph) TempLayerArchive(id string, compression Compression, output
if err != nil { if err != nil {
return nil, err return nil, err
} }
return NewTempArchive(utils.ProgressReader(ioutil.NopCloser(archive), 0, output, "Buffering to disk %v/%v (%v)", false), tmp.Root) sf := utils.NewStreamFormatter(false)
return NewTempArchive(utils.ProgressReader(ioutil.NopCloser(archive), 0, output, sf.FormatProgress("Buffering to disk", "%v/%v (%v)"), sf), tmp.Root)
} }
// Mktemp creates a temporary sub-directory inside the graph's filesystem. // Mktemp creates a temporary sub-directory inside the graph's filesystem.

View file

@ -68,7 +68,7 @@ func init() {
runtime: runtime, runtime: runtime,
} }
// Retrieve the Image // Retrieve the Image
if err := srv.ImagePull(unitTestImageName, "", "", os.Stdout, false); err != nil { if err := srv.ImagePull(unitTestImageName, "", "", os.Stdout, utils.NewStreamFormatter(false)); err != nil {
panic(err) panic(err)
} }
} }

View file

@ -68,7 +68,7 @@ func (srv *Server) ImagesSearch(term string) ([]ApiSearch, error) {
return outs, nil return outs, nil
} }
func (srv *Server) ImageInsert(name, url, path string, out io.Writer) (string, error) { func (srv *Server) ImageInsert(name, url, path string, out io.Writer, sf *utils.StreamFormatter) (string, error) {
out = utils.NewWriteFlusher(out) out = utils.NewWriteFlusher(out)
img, err := srv.runtime.repositories.LookupImage(name) img, err := srv.runtime.repositories.LookupImage(name)
if err != nil { if err != nil {
@ -92,7 +92,7 @@ func (srv *Server) ImageInsert(name, url, path string, out io.Writer) (string, e
return "", err return "", err
} }
if err := c.Inject(utils.ProgressReader(file.Body, int(file.ContentLength), out, "Downloading %v/%v (%v)\r", false), path); err != nil { if err := c.Inject(utils.ProgressReader(file.Body, int(file.ContentLength), out, sf.FormatProgress("Downloading", "%v/%v (%v)"), sf), path); err != nil {
return "", err return "", err
} }
// FIXME: Handle custom repo, tag comment, author // FIXME: Handle custom repo, tag comment, author
@ -100,7 +100,7 @@ func (srv *Server) ImageInsert(name, url, path string, out io.Writer) (string, e
if err != nil { if err != nil {
return "", err return "", err
} }
fmt.Fprintf(out, "%s\n", img.Id) out.Write(sf.FormatStatus(img.Id))
return img.ShortId(), nil return img.ShortId(), nil
} }
@ -292,7 +292,7 @@ func (srv *Server) ContainerTag(name, repo, tag string, force bool) error {
return nil return nil
} }
func (srv *Server) pullImage(r *registry.Registry, out io.Writer, imgId, endpoint string, token []string, json bool) error { func (srv *Server) pullImage(r *registry.Registry, out io.Writer, imgId, endpoint string, token []string, sf *utils.StreamFormatter) error {
history, err := r.GetRemoteHistory(imgId, endpoint, token) history, err := r.GetRemoteHistory(imgId, endpoint, token)
if err != nil { if err != nil {
return err return err
@ -302,7 +302,7 @@ func (srv *Server) pullImage(r *registry.Registry, out io.Writer, imgId, endpoin
// FIXME: Launch the getRemoteImage() in goroutines // FIXME: Launch the getRemoteImage() in goroutines
for _, id := range history { for _, id := range history {
if !srv.runtime.graph.Exists(id) { if !srv.runtime.graph.Exists(id) {
fmt.Fprintf(out, utils.FormatStatus("Pulling %s metadata", json), id) out.Write(sf.FormatStatus("Pulling %s metadata", id))
imgJson, err := r.GetRemoteImageJson(id, endpoint, token) imgJson, err := r.GetRemoteImageJson(id, endpoint, token)
if err != nil { if err != nil {
// FIXME: Keep goging in case of error? // FIXME: Keep goging in case of error?
@ -314,12 +314,12 @@ func (srv *Server) pullImage(r *registry.Registry, out io.Writer, imgId, endpoin
} }
// Get the layer // Get the layer
fmt.Fprintf(out, utils.FormatStatus("Pulling %s fs layer", json), id) out.Write(sf.FormatStatus("Pulling %s fs layer", id))
layer, contentLength, err := r.GetRemoteImageLayer(img.Id, endpoint, token) layer, contentLength, err := r.GetRemoteImageLayer(img.Id, endpoint, token)
if err != nil { if err != nil {
return err return err
} }
if err := srv.runtime.graph.Register(utils.ProgressReader(layer, contentLength, out, utils.FormatProgress("%v/%v (%v)", json), json), false, img); err != nil { if err := srv.runtime.graph.Register(utils.ProgressReader(layer, contentLength, out, sf.FormatProgress("Downloading", "%v/%v (%v)"), sf), false, img); err != nil {
return err return err
} }
} }
@ -327,8 +327,8 @@ func (srv *Server) pullImage(r *registry.Registry, out io.Writer, imgId, endpoin
return nil return nil
} }
func (srv *Server) pullRepository(r *registry.Registry, out io.Writer, remote, askedTag string, json bool) error { func (srv *Server) pullRepository(r *registry.Registry, out io.Writer, remote, askedTag string, sf *utils.StreamFormatter) error {
fmt.Fprintf(out, utils.FormatStatus("Pulling repository %s from %s", json), remote, auth.IndexServerAddress()) out.Write(sf.FormatStatus("Pulling repository %s from %s", remote, auth.IndexServerAddress()))
repoData, err := r.GetRepositoryData(remote) repoData, err := r.GetRepositoryData(remote)
if err != nil { if err != nil {
return err return err
@ -365,11 +365,11 @@ func (srv *Server) pullRepository(r *registry.Registry, out io.Writer, remote, a
utils.Debugf("(%s) does not match %s (id: %s), skipping", img.Tag, askedTag, img.Id) utils.Debugf("(%s) does not match %s (id: %s), skipping", img.Tag, askedTag, img.Id)
continue continue
} }
fmt.Fprintf(out, utils.FormatStatus("Pulling image %s (%s) from %s", json), img.Id, img.Tag, remote) out.Write(sf.FormatStatus("Pulling image %s (%s) from %s", img.Id, img.Tag, remote))
success := false success := false
for _, ep := range repoData.Endpoints { for _, ep := range repoData.Endpoints {
if err := srv.pullImage(r, out, img.Id, "https://"+ep+"/v1", repoData.Tokens, json); err != nil { if err := srv.pullImage(r, out, img.Id, "https://"+ep+"/v1", repoData.Tokens, sf); err != nil {
fmt.Fprintf(out, utils.FormatStatus("Error while retrieving image for tag: %s (%s); checking next endpoint\n", json), askedTag, err) out.Write(sf.FormatStatus("Error while retrieving image for tag: %s (%s); checking next endpoint", askedTag, err))
continue continue
} }
success = true success = true
@ -394,17 +394,17 @@ func (srv *Server) pullRepository(r *registry.Registry, out io.Writer, remote, a
return nil return nil
} }
func (srv *Server) ImagePull(name, tag, endpoint string, out io.Writer, json bool) error { func (srv *Server) ImagePull(name, tag, endpoint string, out io.Writer, sf *utils.StreamFormatter) error {
r := registry.NewRegistry(srv.runtime.root) r := registry.NewRegistry(srv.runtime.root)
out = utils.NewWriteFlusher(out) out = utils.NewWriteFlusher(out)
if endpoint != "" { if endpoint != "" {
if err := srv.pullImage(r, out, name, endpoint, nil, json); err != nil { if err := srv.pullImage(r, out, name, endpoint, nil, sf); err != nil {
return err return err
} }
return nil return nil
} }
if err := srv.pullRepository(r, out, name, tag, json); err != nil { if err := srv.pullRepository(r, out, name, tag, sf); err != nil {
return err return err
} }
@ -477,14 +477,14 @@ func (srv *Server) getImageList(localRepo map[string]string) ([]*registry.ImgDat
return imgList, nil return imgList, nil
} }
func (srv *Server) pushRepository(r *registry.Registry, out io.Writer, name string, localRepo map[string]string) error { func (srv *Server) pushRepository(r *registry.Registry, out io.Writer, name string, localRepo map[string]string, sf *utils.StreamFormatter) error {
out = utils.NewWriteFlusher(out) out = utils.NewWriteFlusher(out)
fmt.Fprintf(out, "Processing checksums\n") out.Write(sf.FormatStatus("Processing checksums"))
imgList, err := srv.getImageList(localRepo) imgList, err := srv.getImageList(localRepo)
if err != nil { if err != nil {
return err return err
} }
fmt.Fprintf(out, "Sending images list\n") out.Write(sf.FormatStatus("Sending image list"))
repoData, err := r.PushImageJsonIndex(name, imgList, false) repoData, err := r.PushImageJsonIndex(name, imgList, false)
if err != nil { if err != nil {
@ -492,18 +492,18 @@ func (srv *Server) pushRepository(r *registry.Registry, out io.Writer, name stri
} }
for _, ep := range repoData.Endpoints { for _, ep := range repoData.Endpoints {
fmt.Fprintf(out, "Pushing repository %s to %s (%d tags)\r\n", name, ep, len(localRepo)) out.Write(sf.FormatStatus("Pushing repository %s to %s (%d tags)", name, ep, len(localRepo)))
// For each image within the repo, push them // For each image within the repo, push them
for _, elem := range imgList { for _, elem := range imgList {
if _, exists := repoData.ImgList[elem.Id]; exists { if _, exists := repoData.ImgList[elem.Id]; exists {
fmt.Fprintf(out, "Image %s already on registry, skipping\n", name) out.Write(sf.FormatStatus("Image %s already on registry, skipping", name))
continue continue
} }
if err := srv.pushImage(r, out, name, elem.Id, ep, repoData.Tokens); err != nil { if err := srv.pushImage(r, out, name, elem.Id, ep, repoData.Tokens, sf); err != nil {
// FIXME: Continue on error? // FIXME: Continue on error?
return err return err
} }
fmt.Fprintf(out, "Pushing tags for rev [%s] on {%s}\n", elem.Id, ep+"/users/"+name+"/"+elem.Tag) out.Write(sf.FormatStatus("Pushing tags for rev [%s] on {%s}", elem.Id, ep+"/users/"+name+"/"+elem.Tag))
if err := r.PushRegistryTag(name, elem.Id, elem.Tag, ep, repoData.Tokens); err != nil { if err := r.PushRegistryTag(name, elem.Id, elem.Tag, ep, repoData.Tokens); err != nil {
return err return err
} }
@ -516,13 +516,13 @@ func (srv *Server) pushRepository(r *registry.Registry, out io.Writer, name stri
return nil return nil
} }
func (srv *Server) pushImage(r *registry.Registry, out io.Writer, remote, imgId, ep string, token []string) error { func (srv *Server) pushImage(r *registry.Registry, out io.Writer, remote, imgId, ep string, token []string, sf *utils.StreamFormatter) error {
out = utils.NewWriteFlusher(out) out = utils.NewWriteFlusher(out)
jsonRaw, err := ioutil.ReadFile(path.Join(srv.runtime.graph.Root, imgId, "json")) jsonRaw, err := ioutil.ReadFile(path.Join(srv.runtime.graph.Root, imgId, "json"))
if err != nil { if err != nil {
return fmt.Errorf("Error while retreiving the path for {%s}: %s", imgId, err) return fmt.Errorf("Error while retreiving the path for {%s}: %s", imgId, err)
} }
fmt.Fprintf(out, "Pushing %s\r\n", imgId) out.Write(sf.FormatStatus("Pushing %s", imgId))
// Make sure we have the image's checksum // Make sure we have the image's checksum
checksum, err := srv.getChecksum(imgId) checksum, err := srv.getChecksum(imgId)
@ -537,7 +537,7 @@ func (srv *Server) pushImage(r *registry.Registry, out io.Writer, remote, imgId,
// Send the json // Send the json
if err := r.PushImageJsonRegistry(imgData, jsonRaw, ep, token); err != nil { if err := r.PushImageJsonRegistry(imgData, jsonRaw, ep, token); err != nil {
if err == registry.ErrAlreadyExists { if err == registry.ErrAlreadyExists {
fmt.Fprintf(out, "Image %s already uploaded ; skipping\n", imgData.Id) out.Write(sf.FormatStatus("Image %s already uploaded ; skipping", imgData.Id))
return nil return nil
} }
return err return err
@ -570,22 +570,22 @@ func (srv *Server) pushImage(r *registry.Registry, out io.Writer, remote, imgId,
} }
// Send the layer // Send the layer
if err := r.PushImageLayerRegistry(imgData.Id, utils.ProgressReader(layerData, int(layerData.Size), out, "", false), ep, token); err != nil { if err := r.PushImageLayerRegistry(imgData.Id, utils.ProgressReader(layerData, int(layerData.Size), out, sf.FormatProgress("", "%v/%v (%v)"), sf), ep, token); err != nil {
return err return err
} }
return nil return nil
} }
func (srv *Server) ImagePush(name, endpoint string, out io.Writer) error { func (srv *Server) ImagePush(name, endpoint string, out io.Writer, sf *utils.StreamFormatter) error {
out = utils.NewWriteFlusher(out) out = utils.NewWriteFlusher(out)
img, err := srv.runtime.graph.Get(name) img, err := srv.runtime.graph.Get(name)
r := registry.NewRegistry(srv.runtime.root) r := registry.NewRegistry(srv.runtime.root)
if err != nil { if err != nil {
fmt.Fprintf(out, "The push refers to a repository [%s] (len: %d)\n", name, len(srv.runtime.repositories.Repositories[name])) out.Write(sf.FormatStatus("The push refers to a repository [%s] (len: %d)", name, len(srv.runtime.repositories.Repositories[name])))
// If it fails, try to get the repository // If it fails, try to get the repository
if localRepo, exists := srv.runtime.repositories.Repositories[name]; exists { if localRepo, exists := srv.runtime.repositories.Repositories[name]; exists {
if err := srv.pushRepository(r, out, name, localRepo); err != nil { if err := srv.pushRepository(r, out, name, localRepo, sf); err != nil {
return err return err
} }
return nil return nil
@ -593,14 +593,14 @@ func (srv *Server) ImagePush(name, endpoint string, out io.Writer) error {
return err return err
} }
fmt.Fprintf(out, "The push refers to an image: [%s]\n", name) out.Write(sf.FormatStatus("The push refers to an image: [%s]", name))
if err := srv.pushImage(r, out, name, img.Id, endpoint, nil); err != nil { if err := srv.pushImage(r, out, name, img.Id, endpoint, nil, sf); err != nil {
return err return err
} }
return nil return nil
} }
func (srv *Server) ImageImport(src, repo, tag string, in io.Reader, out io.Writer) error { func (srv *Server) ImageImport(src, repo, tag string, in io.Reader, out io.Writer, sf *utils.StreamFormatter) error {
var archive io.Reader var archive io.Reader
var resp *http.Response var resp *http.Response
@ -609,21 +609,21 @@ func (srv *Server) ImageImport(src, repo, tag string, in io.Reader, out io.Write
} else { } else {
u, err := url.Parse(src) u, err := url.Parse(src)
if err != nil { if err != nil {
fmt.Fprintf(out, "Error: %s\n", err) return err
} }
if u.Scheme == "" { if u.Scheme == "" {
u.Scheme = "http" u.Scheme = "http"
u.Host = src u.Host = src
u.Path = "" u.Path = ""
} }
fmt.Fprintf(out, "Downloading from %s\n", u) out.Write(sf.FormatStatus("Downloading from %s", u))
// Download with curl (pretty progress bar) // Download with curl (pretty progress bar)
// If curl is not available, fallback to http.Get() // If curl is not available, fallback to http.Get()
resp, err = utils.Download(u.String(), out) resp, err = utils.Download(u.String(), out)
if err != nil { if err != nil {
return err return err
} }
archive = utils.ProgressReader(resp.Body, int(resp.ContentLength), out, "Importing %v/%v (%v)\r", false) archive = utils.ProgressReader(resp.Body, int(resp.ContentLength), out, sf.FormatProgress("Importing", "%v/%v (%v)"), sf)
} }
img, err := srv.runtime.graph.Create(archive, nil, "Imported from "+src, "", nil) img, err := srv.runtime.graph.Create(archive, nil, "Imported from "+src, "", nil)
if err != nil { if err != nil {
@ -635,7 +635,7 @@ func (srv *Server) ImageImport(src, repo, tag string, in io.Reader, out io.Write
return err return err
} }
} }
fmt.Fprintf(out, "%s\n", img.ShortId()) out.Write(sf.FormatStatus(img.ShortId()))
return nil return nil
} }

View file

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"crypto/sha256" "crypto/sha256"
"encoding/hex" "encoding/hex"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"index/suffixarray" "index/suffixarray"
@ -69,7 +70,7 @@ type progressReader struct {
readProgress int // How much has been read so far (bytes) readProgress int // How much has been read so far (bytes)
lastUpdate int // How many bytes read at least update lastUpdate int // How many bytes read at least update
template string // Template to print. Default "%v/%v (%v)" template string // Template to print. Default "%v/%v (%v)"
json bool sf *StreamFormatter
} }
func (r *progressReader) Read(p []byte) (n int, err error) { func (r *progressReader) Read(p []byte) (n int, err error) {
@ -93,7 +94,7 @@ func (r *progressReader) Read(p []byte) (n int, err error) {
} }
// Send newline when complete // Send newline when complete
if err != nil { if err != nil {
fmt.Fprintf(r.output, FormatStatus("", r.json)) r.output.Write(r.sf.FormatStatus(""))
} }
return read, err return read, err
@ -101,11 +102,12 @@ func (r *progressReader) Read(p []byte) (n int, err error) {
func (r *progressReader) Close() error { func (r *progressReader) Close() error {
return io.ReadCloser(r.reader).Close() return io.ReadCloser(r.reader).Close()
} }
func ProgressReader(r io.ReadCloser, size int, output io.Writer, template string, json bool) *progressReader { func ProgressReader(r io.ReadCloser, size int, output io.Writer, template []byte, sf *StreamFormatter) *progressReader {
if template == "" { tpl := string(template)
template = "%v/%v (%v)\r" if tpl == "" {
tpl = string(sf.FormatProgress("", "%v/%v (%v)"))
} }
return &progressReader{r, NewWriteFlusher(output), size, 0, 0, template, json} return &progressReader{r, NewWriteFlusher(output), size, 0, 0, tpl, sf}
} }
// HumanDuration returns a human-readable approximation of a duration // HumanDuration returns a human-readable approximation of a duration
@ -564,16 +566,57 @@ func NewWriteFlusher(w io.Writer) *WriteFlusher {
return &WriteFlusher{w: w, flusher: flusher} return &WriteFlusher{w: w, flusher: flusher}
} }
func FormatStatus(str string, json bool) string { type JsonMessage struct {
if json { Status string `json:"status,omitempty"`
return "{\"status\" : \"" + str + "\"}" Progress string `json:"progress,omitempty"`
} Error string `json:"error,omitempty"`
return str + "\r\n"
} }
func FormatProgress(str string, json bool) string { type StreamFormatter struct {
if json { json bool
return "{\"progress\" : \"" + str + "\"}" used bool
} }
return "Downloading " + str + "\r"
func NewStreamFormatter(json bool) *StreamFormatter {
return &StreamFormatter{json, false}
}
func (sf *StreamFormatter) FormatStatus(format string, a ...interface{}) []byte {
sf.used = true
str := fmt.Sprintf(format, a...)
if sf.json {
b, err := json.Marshal(&JsonMessage{Status:str});
if err != nil {
return sf.FormatError(err)
}
return b
}
return []byte(str + "\r\n")
}
func (sf *StreamFormatter) FormatError(err error) []byte {
sf.used = true
if sf.json {
if b, err := json.Marshal(&JsonMessage{Error:err.Error()}); err == nil {
return b
}
return []byte("{\"error\":\"format error\"}")
}
return []byte("Error: " + err.Error() + "\r\n")
}
func (sf *StreamFormatter) FormatProgress(action, str string) []byte {
sf.used = true
if sf.json {
b, err := json.Marshal(&JsonMessage{Progress:str})
if err != nil {
return nil
}
return b
}
return []byte(action + " " + str + "\r")
}
func (sf *StreamFormatter) Used() bool {
return sf.used
} }