diff --git a/api.go b/api.go index 55fb8d5a66..c5472fa2c2 100644 --- a/api.go +++ b/api.go @@ -297,7 +297,7 @@ func postImagesCreate(srv *Server, version float64, w http.ResponseWriter, r *ht registry := r.Form.Get("registry") if err := srv.ImagePull(image, tag, registry, w, sf); err != nil { if sf.Used() { - fmt.Fprintf(w, sf.FormatError(err)) + w.Write(sf.FormatError(err)) return nil } return err @@ -305,7 +305,7 @@ func postImagesCreate(srv *Server, version float64, w http.ResponseWriter, r *ht } else { //import if err := srv.ImageImport(src, repo, tag, r.Body, w, sf); err != nil { if sf.Used() { - fmt.Fprintf(w, sf.FormatError(err)) + w.Write(sf.FormatError(err)) return nil } return err @@ -349,7 +349,7 @@ func postImagesInsert(srv *Server, version float64, w http.ResponseWriter, r *ht sf := utils.NewStreamFormatter(version > 1.0) if err := srv.ImageInsert(name, url, path, w, sf); err != nil { if sf.Used() { - fmt.Fprintf(w, sf.FormatError(err)) + w.Write(sf.FormatError(err)) return nil } return err @@ -373,7 +373,7 @@ func postImagesPush(srv *Server, version float64, w http.ResponseWriter, r *http sf := utils.NewStreamFormatter(version > 1.0) if err := srv.ImagePush(name, registry, w, sf); err != nil { if sf.Used() { - fmt.Fprintf(w, sf.FormatError(err)) + w.Write(sf.FormatError(err)) return nil } return err diff --git a/graph.go b/graph.go index 0090d51636..befb5ace5e 100644 --- a/graph.go +++ b/graph.go @@ -165,7 +165,8 @@ func (graph *Graph) TempLayerArchive(id string, compression Compression, output if err != nil { return nil, err } - return NewTempArchive(utils.ProgressReader(ioutil.NopCloser(archive), 0, output, "Buffering to disk %v/%v (%v)", utils.NewStreamFormatter(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. diff --git a/server.go b/server.go index 6ebf337dd0..8799c6dc51 100644 --- a/server.go +++ b/server.go @@ -99,7 +99,7 @@ func (srv *Server) ImageInsert(name, url, path string, out io.Writer, sf *utils. if err != nil { return err } - fmt.Fprintf(out, sf.FormatStatus("%s"), img.Id) + out.Write(sf.FormatStatus(img.Id)) return nil } @@ -301,7 +301,7 @@ func (srv *Server) pullImage(out io.Writer, imgId, registry string, token []stri // FIXME: Launch the getRemoteImage() in goroutines for _, id := range history { if !srv.runtime.graph.Exists(id) { - fmt.Fprintf(out, sf.FormatStatus("Pulling %s metadata"), id) + out.Write(sf.FormatStatus("Pulling %s metadata", id)) imgJson, err := srv.registry.GetRemoteImageJson(id, registry, token) if err != nil { // FIXME: Keep goging in case of error? @@ -313,7 +313,7 @@ func (srv *Server) pullImage(out io.Writer, imgId, registry string, token []stri } // Get the layer - fmt.Fprintf(out, sf.FormatStatus("Pulling %s fs layer"), id) + out.Write(sf.FormatStatus("Pulling %s fs layer", id)) layer, contentLength, err := srv.registry.GetRemoteImageLayer(img.Id, registry, token) if err != nil { return err @@ -327,7 +327,7 @@ func (srv *Server) pullImage(out io.Writer, imgId, registry string, token []stri } func (srv *Server) pullRepository(out io.Writer, remote, askedTag string, sf *utils.StreamFormatter) error { - fmt.Fprintf(out, sf.FormatStatus("Pulling repository %s from %s"), remote, auth.IndexServerAddress()) + out.Write(sf.FormatStatus("Pulling repository %s from %s", remote, auth.IndexServerAddress())) repoData, err := srv.registry.GetRepositoryData(remote) if err != nil { return err @@ -364,11 +364,11 @@ func (srv *Server) pullRepository(out io.Writer, remote, askedTag string, sf *ut utils.Debugf("(%s) does not match %s (id: %s), skipping", img.Tag, askedTag, img.Id) continue } - fmt.Fprintf(out, sf.FormatStatus("Pulling image %s (%s) from %s"), img.Id, img.Tag, remote) + out.Write(sf.FormatStatus("Pulling image %s (%s) from %s", img.Id, img.Tag, remote)) success := false for _, ep := range repoData.Endpoints { if err := srv.pullImage(out, img.Id, "https://"+ep+"/v1", repoData.Tokens, sf); err != nil { - fmt.Fprintf(out, sf.FormatStatus("Error while retrieving image for tag: %s (%s); checking next endpoint"), askedTag, err) + out.Write(sf.FormatStatus("Error while retrieving image for tag: %s (%s); checking next endpoint", askedTag, err)) continue } success = true @@ -477,12 +477,12 @@ func (srv *Server) getImageList(localRepo map[string]string) ([]*registry.ImgDat func (srv *Server) pushRepository(out io.Writer, name string, localRepo map[string]string, sf *utils.StreamFormatter) error { out = utils.NewWriteFlusher(out) - fmt.Fprintf(out, sf.FormatStatus("Processing checksums")) + out.Write(sf.FormatStatus("Processing checksums")) imgList, err := srv.getImageList(localRepo) if err != nil { return err } - fmt.Fprintf(out, sf.FormatStatus("Sending image list")) + out.Write(sf.FormatStatus("Sending image list")) repoData, err := srv.registry.PushImageJsonIndex(name, imgList, false) if err != nil { @@ -491,18 +491,18 @@ func (srv *Server) pushRepository(out io.Writer, name string, localRepo map[stri // FIXME: Send only needed images for _, ep := range repoData.Endpoints { - fmt.Fprintf(out, sf.FormatStatus("Pushing repository %s to %s (%d tags)"), 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 _, elem := range imgList { if _, exists := repoData.ImgList[elem.Id]; exists { - fmt.Fprintf(out, sf.FormatStatus("Image %s already on registry, skipping"), name) + out.Write(sf.FormatStatus("Image %s already on registry, skipping", name)) continue } if err := srv.pushImage(out, name, elem.Id, ep, repoData.Tokens, sf); err != nil { // FIXME: Continue on error? return err } - fmt.Fprintf(out, sf.FormatStatus("Pushing tags for rev [%s] on {%s}"), 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 := srv.registry.PushRegistryTag(name, elem.Id, elem.Tag, ep, repoData.Tokens); err != nil { return err } @@ -521,7 +521,7 @@ func (srv *Server) pushImage(out io.Writer, remote, imgId, ep string, token []st if err != nil { return fmt.Errorf("Error while retreiving the path for {%s}: %s", imgId, err) } - fmt.Fprintf(out, sf.FormatStatus("Pushing %s"), imgId) + out.Write(sf.FormatStatus("Pushing %s", imgId)) // Make sure we have the image's checksum checksum, err := srv.getChecksum(imgId) @@ -536,7 +536,7 @@ func (srv *Server) pushImage(out io.Writer, remote, imgId, ep string, token []st // Send the json if err := srv.registry.PushImageJsonRegistry(imgData, jsonRaw, ep, token); err != nil { if err == registry.ErrAlreadyExists { - fmt.Fprintf(out, sf.FormatStatus("Image %s already uploaded ; skipping"), imgData.Id) + out.Write(sf.FormatStatus("Image %s already uploaded ; skipping", imgData.Id)) return nil } return err @@ -579,7 +579,7 @@ func (srv *Server) ImagePush(name, registry string, out io.Writer, sf *utils.Str out = utils.NewWriteFlusher(out) img, err := srv.runtime.graph.Get(name) if err != nil { - fmt.Fprintf(out, sf.FormatStatus("The push refers to a repository [%s] (len: %d)"), 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 localRepo, exists := srv.runtime.repositories.Repositories[name]; exists { if err := srv.pushRepository(out, name, localRepo, sf); err != nil { @@ -590,7 +590,7 @@ func (srv *Server) ImagePush(name, registry string, out io.Writer, sf *utils.Str return err } - fmt.Fprintf(out, sf.FormatStatus("The push refers to an image: [%s]"), name) + out.Write(sf.FormatStatus("The push refers to an image: [%s]", name)) if err := srv.pushImage(out, name, img.Id, registry, nil, sf); err != nil { return err } @@ -613,7 +613,7 @@ func (srv *Server) ImageImport(src, repo, tag string, in io.Reader, out io.Write u.Host = src u.Path = "" } - fmt.Fprintf(out, sf.FormatStatus("Downloading from %s"), u) + out.Write(sf.FormatStatus("Downloading from %s", u)) // Download with curl (pretty progress bar) // If curl is not available, fallback to http.Get() resp, err = utils.Download(u.String(), out) @@ -632,7 +632,7 @@ func (srv *Server) ImageImport(src, repo, tag string, in io.Reader, out io.Write return err } } - fmt.Fprintf(out, sf.FormatStatus(img.ShortId())) + out.Write(sf.FormatStatus(img.ShortId())) return nil } diff --git a/utils/utils.go b/utils/utils.go index 1fce76e5aa..64aa111717 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -94,7 +94,7 @@ func (r *progressReader) Read(p []byte) (n int, err error) { } // Send newline when complete if err != nil { - fmt.Fprintf(r.output, r.sf.FormatStatus("")) + r.output.Write(r.sf.FormatStatus("")) } return read, err @@ -102,11 +102,12 @@ func (r *progressReader) Read(p []byte) (n int, err error) { func (r *progressReader) Close() error { return io.ReadCloser(r.reader).Close() } -func ProgressReader(r io.ReadCloser, size int, output io.Writer, template string, sf *StreamFormatter) *progressReader { - if template == "" { - template = "%v/%v (%v)" +func ProgressReader(r io.ReadCloser, size int, output io.Writer, template []byte, sf *StreamFormatter) *progressReader { + tpl := string(template) + if tpl == "" { + tpl = "%v/%v (%v)" } - return &progressReader{r, NewWriteFlusher(output), size, 0, 0, template, sf} + return &progressReader{r, NewWriteFlusher(output), size, 0, 0, tpl, sf} } // HumanDuration returns a human-readable approximation of a duration @@ -573,39 +574,40 @@ func NewStreamFormatter(json bool) *StreamFormatter { return &StreamFormatter{json, false} } -func (sf *StreamFormatter) FormatStatus(str string) string { +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 string(b) + return b } - return str + "\r\n" + return []byte(str + "\r\n") } -func (sf *StreamFormatter) FormatError(err error) string { +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 string(b) + return b } - return "{\"error\":\"format error\"}" + return []byte("{\"error\":\"format error\"}") } - return "Error: " + err.Error() + "\r\n" + return []byte("Error: " + err.Error() + "\r\n") } -func (sf *StreamFormatter) FormatProgress(action, str string) string { +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 sf.FormatError(err) + return nil } - return string(b) + return b } - return action + " " + str + "\r" + return []byte(action + " " + str + "\r") } func (sf *StreamFormatter) Used() bool {