From 0ee1e9f6049226191f773c0c87cdd7edba3c2b1e Mon Sep 17 00:00:00 2001 From: bobby abbott Date: Tue, 17 Mar 2015 19:18:41 -0700 Subject: [PATCH] Fixes hacks from progressreader refactor related to #10959 Signed-off-by: bobby abbott Upstream-commit: 0cd6c05d8112e9246b734107d54e2855e3d5fec5 Component: engine --- components/engine/api/client/build.go | 6 ++-- components/engine/api/client/utils.go | 4 +-- components/engine/api/server/server.go | 7 +++-- components/engine/builder/evaluator.go | 3 +- components/engine/builder/internals.go | 3 +- components/engine/builder/job.go | 7 +++-- components/engine/events/events.go | 14 ++++----- components/engine/events/events_test.go | 16 +++++----- components/engine/graph/graph.go | 3 +- components/engine/graph/import.go | 3 +- components/engine/graph/pull.go | 13 ++++---- components/engine/graph/push.go | 15 ++++----- .../{utils => pkg/jsonmessage}/jsonmessage.go | 2 +- .../jsonmessage}/jsonmessage_test.go | 2 +- .../pkg/progressreader/progressreader.go | 31 +++---------------- .../streamformatter}/streamformatter.go | 30 +++++++----------- .../streamformatter}/streamformatter_test.go | 9 +++--- components/engine/utils/utils.go | 3 +- 18 files changed, 77 insertions(+), 94 deletions(-) rename components/engine/{utils => pkg/jsonmessage}/jsonmessage.go (99%) rename components/engine/{utils => pkg/jsonmessage}/jsonmessage_test.go (97%) rename components/engine/{utils => pkg/streamformatter}/streamformatter.go (72%) rename components/engine/{utils => pkg/streamformatter}/streamformatter_test.go (88%) diff --git a/components/engine/api/client/build.go b/components/engine/api/client/build.go index 91446132ab..87547fc8a4 100644 --- a/components/engine/api/client/build.go +++ b/components/engine/api/client/build.go @@ -22,9 +22,11 @@ import ( "github.com/docker/docker/graph" "github.com/docker/docker/pkg/archive" "github.com/docker/docker/pkg/fileutils" + "github.com/docker/docker/pkg/jsonmessage" flag "github.com/docker/docker/pkg/mflag" "github.com/docker/docker/pkg/parsers" "github.com/docker/docker/pkg/progressreader" + "github.com/docker/docker/pkg/streamformatter" "github.com/docker/docker/pkg/symlink" "github.com/docker/docker/pkg/units" "github.com/docker/docker/pkg/urlutil" @@ -198,7 +200,7 @@ func (cli *DockerCli) CmdBuild(args ...string) error { // Setup an upload progress bar // FIXME: ProgressReader shouldn't be this annoying to use if context != nil { - sf := utils.NewStreamFormatter(false) + sf := streamformatter.NewStreamFormatter(false) body = progressreader.New(progressreader.Config{ In: context, Out: cli.out, @@ -291,7 +293,7 @@ func (cli *DockerCli) CmdBuild(args ...string) error { headers.Set("Content-Type", "application/tar") } err = cli.stream("POST", fmt.Sprintf("/build?%s", v.Encode()), body, cli.out, headers) - if jerr, ok := err.(*utils.JSONError); ok { + if jerr, ok := err.(*jsonmessage.JSONError); ok { // If no error code is set, default to 1 if jerr.Code == 0 { jerr.Code = 1 diff --git a/components/engine/api/client/utils.go b/components/engine/api/client/utils.go index 103bfdec3a..65ed2c7c44 100644 --- a/components/engine/api/client/utils.go +++ b/components/engine/api/client/utils.go @@ -19,11 +19,11 @@ import ( "github.com/docker/docker/api" "github.com/docker/docker/autogen/dockerversion" "github.com/docker/docker/engine" + "github.com/docker/docker/pkg/jsonmessage" "github.com/docker/docker/pkg/signal" "github.com/docker/docker/pkg/stdcopy" "github.com/docker/docker/pkg/term" "github.com/docker/docker/registry" - "github.com/docker/docker/utils" ) var ( @@ -164,7 +164,7 @@ func (cli *DockerCli) streamHelper(method, path string, setRawTerminal bool, in } if api.MatchesContentType(resp.Header.Get("Content-Type"), "application/json") { - return utils.DisplayJSONMessagesStream(resp.Body, stdout, cli.outFd, cli.isTerminalOut) + return jsonmessage.DisplayJSONMessagesStream(resp.Body, stdout, cli.outFd, cli.isTerminalOut) } if stdout != nil || stderr != nil { // When TTY is ON, use regular copy diff --git a/components/engine/api/server/server.go b/components/engine/api/server/server.go index 9c50bfb523..080d9ad106 100644 --- a/components/engine/api/server/server.go +++ b/components/engine/api/server/server.go @@ -32,6 +32,7 @@ import ( "github.com/docker/docker/pkg/listenbuffer" "github.com/docker/docker/pkg/parsers" "github.com/docker/docker/pkg/stdcopy" + "github.com/docker/docker/pkg/streamformatter" "github.com/docker/docker/pkg/version" "github.com/docker/docker/registry" "github.com/docker/docker/utils" @@ -595,7 +596,7 @@ func postImagesCreate(eng *engine.Engine, version version.Version, w http.Respon if !job.Stdout.Used() { return err } - sf := utils.NewStreamFormatter(version.GreaterThan("1.0")) + sf := streamformatter.NewStreamFormatter(version.GreaterThan("1.0")) w.Write(sf.FormatError(err)) } @@ -680,7 +681,7 @@ func postImagesPush(eng *engine.Engine, version version.Version, w http.Response if !job.Stdout.Used() { return err } - sf := utils.NewStreamFormatter(version.GreaterThan("1.0")) + sf := streamformatter.NewStreamFormatter(version.GreaterThan("1.0")) w.Write(sf.FormatError(err)) } return nil @@ -1107,7 +1108,7 @@ func postBuild(eng *engine.Engine, version version.Version, w http.ResponseWrite if !job.Stdout.Used() { return err } - sf := utils.NewStreamFormatter(version.GreaterThanOrEqualTo("1.8")) + sf := streamformatter.NewStreamFormatter(version.GreaterThanOrEqualTo("1.8")) w.Write(sf.FormatError(err)) } return nil diff --git a/components/engine/builder/evaluator.go b/components/engine/builder/evaluator.go index ec568d1fca..78c4c12f84 100644 --- a/components/engine/builder/evaluator.go +++ b/components/engine/builder/evaluator.go @@ -33,6 +33,7 @@ import ( "github.com/docker/docker/daemon" "github.com/docker/docker/engine" "github.com/docker/docker/pkg/fileutils" + "github.com/docker/docker/pkg/streamformatter" "github.com/docker/docker/pkg/stringid" "github.com/docker/docker/pkg/symlink" "github.com/docker/docker/pkg/tarsum" @@ -105,7 +106,7 @@ type Builder struct { // Deprecated, original writer used for ImagePull. To be removed. OutOld io.Writer - StreamFormatter *utils.StreamFormatter + StreamFormatter *streamformatter.StreamFormatter Config *runconfig.Config // runconfig for cmd, run, entrypoint etc. diff --git a/components/engine/builder/internals.go b/components/engine/builder/internals.go index e7e792aa0f..1c90bf2d5e 100644 --- a/components/engine/builder/internals.go +++ b/components/engine/builder/internals.go @@ -26,6 +26,7 @@ import ( "github.com/docker/docker/pkg/archive" "github.com/docker/docker/pkg/chrootarchive" "github.com/docker/docker/pkg/ioutils" + "github.com/docker/docker/pkg/jsonmessage" "github.com/docker/docker/pkg/parsers" "github.com/docker/docker/pkg/progressreader" "github.com/docker/docker/pkg/stringid" @@ -601,7 +602,7 @@ func (b *Builder) run(c *daemon.Container) error { // Wait for it to finish if ret, _ := c.WaitStop(-1 * time.Second); ret != 0 { - err := &utils.JSONError{ + err := &jsonmessage.JSONError{ Message: fmt.Sprintf("The command %v returned a non-zero code: %d", b.Config.Cmd, ret), Code: ret, } diff --git a/components/engine/builder/job.go b/components/engine/builder/job.go index 665b268b61..89ed52f873 100644 --- a/components/engine/builder/job.go +++ b/components/engine/builder/job.go @@ -17,6 +17,7 @@ import ( "github.com/docker/docker/graph" "github.com/docker/docker/pkg/archive" "github.com/docker/docker/pkg/parsers" + "github.com/docker/docker/pkg/streamformatter" "github.com/docker/docker/pkg/urlutil" "github.com/docker/docker/registry" "github.com/docker/docker/runconfig" @@ -127,16 +128,16 @@ func (b *BuilderJob) CmdBuild(job *engine.Job) error { } defer context.Close() - sf := utils.NewStreamFormatter(job.GetenvBool("json")) + sf := streamformatter.NewStreamFormatter(job.GetenvBool("json")) builder := &Builder{ Daemon: b.Daemon, Engine: b.Engine, - OutStream: &utils.StdoutFormater{ + OutStream: &streamformatter.StdoutFormater{ Writer: job.Stdout, StreamFormatter: sf, }, - ErrStream: &utils.StderrFormater{ + ErrStream: &streamformatter.StderrFormater{ Writer: job.Stdout, StreamFormatter: sf, }, diff --git a/components/engine/events/events.go b/components/engine/events/events.go index a093f359b3..6940bafbbc 100644 --- a/components/engine/events/events.go +++ b/components/engine/events/events.go @@ -10,23 +10,23 @@ import ( "time" "github.com/docker/docker/engine" + "github.com/docker/docker/pkg/jsonmessage" "github.com/docker/docker/pkg/parsers/filters" - "github.com/docker/docker/utils" ) const eventsLimit = 64 -type listener chan<- *utils.JSONMessage +type listener chan<- *jsonmessage.JSONMessage type Events struct { mu sync.RWMutex - events []*utils.JSONMessage + events []*jsonmessage.JSONMessage subscribers []listener } func New() *Events { return &Events{ - events: make([]*utils.JSONMessage, 0, eventsLimit), + events: make([]*jsonmessage.JSONMessage, 0, eventsLimit), } } @@ -63,7 +63,7 @@ func (e *Events) Get(job *engine.Job) error { timeout.Stop() } - listener := make(chan *utils.JSONMessage) + listener := make(chan *jsonmessage.JSONMessage) e.subscribe(listener) defer e.unsubscribe(listener) @@ -107,7 +107,7 @@ func (e *Events) SubscribersCount(job *engine.Job) error { return nil } -func writeEvent(job *engine.Job, event *utils.JSONMessage, eventFilters filters.Args) error { +func writeEvent(job *engine.Job, event *jsonmessage.JSONMessage, eventFilters filters.Args) error { isFiltered := func(field string, filter []string) bool { if len(filter) == 0 { return false @@ -170,7 +170,7 @@ func (e *Events) subscribersCount() int { func (e *Events) log(action, id, from string) { e.mu.Lock() now := time.Now().UTC().Unix() - jm := &utils.JSONMessage{Status: action, ID: id, From: from, Time: now} + jm := &jsonmessage.JSONMessage{Status: action, ID: id, From: from, Time: now} if len(e.events) == cap(e.events) { // discard oldest event copy(e.events, e.events[1:]) diff --git a/components/engine/events/events_test.go b/components/engine/events/events_test.go index d4fc664baa..a232576fe5 100644 --- a/components/engine/events/events_test.go +++ b/components/engine/events/events_test.go @@ -9,13 +9,13 @@ import ( "time" "github.com/docker/docker/engine" - "github.com/docker/docker/utils" + "github.com/docker/docker/pkg/jsonmessage" ) func TestEventsPublish(t *testing.T) { e := New() - l1 := make(chan *utils.JSONMessage) - l2 := make(chan *utils.JSONMessage) + l1 := make(chan *jsonmessage.JSONMessage) + l2 := make(chan *jsonmessage.JSONMessage) e.subscribe(l1) e.subscribe(l2) count := e.subscribersCount() @@ -61,7 +61,7 @@ func TestEventsPublish(t *testing.T) { func TestEventsPublishTimeout(t *testing.T) { e := New() - l := make(chan *utils.JSONMessage) + l := make(chan *jsonmessage.JSONMessage) e.subscribe(l) c := make(chan struct{}) @@ -108,9 +108,9 @@ func TestLogEvents(t *testing.T) { } buf = bytes.NewBuffer(buf.Bytes()) dec := json.NewDecoder(buf) - var msgs []utils.JSONMessage + var msgs []jsonmessage.JSONMessage for { - var jm utils.JSONMessage + var jm jsonmessage.JSONMessage if err := dec.Decode(&jm); err != nil { if err == io.EOF { break @@ -138,8 +138,8 @@ func TestEventsCountJob(t *testing.T) { if err := e.Install(eng); err != nil { t.Fatal(err) } - l1 := make(chan *utils.JSONMessage) - l2 := make(chan *utils.JSONMessage) + l1 := make(chan *jsonmessage.JSONMessage) + l2 := make(chan *jsonmessage.JSONMessage) e.subscribe(l1) e.subscribe(l2) job := eng.Job("subscribers_count") diff --git a/components/engine/graph/graph.go b/components/engine/graph/graph.go index 0aaf8b3614..902018e39c 100644 --- a/components/engine/graph/graph.go +++ b/components/engine/graph/graph.go @@ -18,6 +18,7 @@ import ( "github.com/docker/docker/image" "github.com/docker/docker/pkg/archive" "github.com/docker/docker/pkg/progressreader" + "github.com/docker/docker/pkg/streamformatter" "github.com/docker/docker/pkg/stringid" "github.com/docker/docker/pkg/truncindex" "github.com/docker/docker/runconfig" @@ -198,7 +199,7 @@ func (graph *Graph) Register(img *image.Image, layerData archive.ArchiveReader) // The archive is stored on disk and will be automatically deleted as soon as has been read. // If output is not nil, a human-readable progress bar will be written to it. // FIXME: does this belong in Graph? How about MktempFile, let the caller use it for archives? -func (graph *Graph) TempLayerArchive(id string, sf *utils.StreamFormatter, output io.Writer) (*archive.TempArchive, error) { +func (graph *Graph) TempLayerArchive(id string, sf *streamformatter.StreamFormatter, output io.Writer) (*archive.TempArchive, error) { image, err := graph.Get(id) if err != nil { return nil, err diff --git a/components/engine/graph/import.go b/components/engine/graph/import.go index 2235fcf6b5..2b3e8bdd6e 100644 --- a/components/engine/graph/import.go +++ b/components/engine/graph/import.go @@ -11,6 +11,7 @@ import ( "github.com/docker/docker/engine" "github.com/docker/docker/pkg/archive" "github.com/docker/docker/pkg/progressreader" + "github.com/docker/docker/pkg/streamformatter" "github.com/docker/docker/runconfig" "github.com/docker/docker/utils" ) @@ -23,7 +24,7 @@ func (s *TagStore) CmdImport(job *engine.Job) error { src = job.Args[0] repo = job.Args[1] tag string - sf = utils.NewStreamFormatter(job.GetenvBool("json")) + sf = streamformatter.NewStreamFormatter(job.GetenvBool("json")) archive archive.ArchiveReader resp *http.Response stdoutBuffer = bytes.NewBuffer(nil) diff --git a/components/engine/graph/pull.go b/components/engine/graph/pull.go index f359bb70c3..0a6b2800c0 100644 --- a/components/engine/graph/pull.go +++ b/components/engine/graph/pull.go @@ -15,6 +15,7 @@ import ( "github.com/docker/docker/engine" "github.com/docker/docker/image" "github.com/docker/docker/pkg/progressreader" + "github.com/docker/docker/pkg/streamformatter" "github.com/docker/docker/pkg/stringid" "github.com/docker/docker/registry" "github.com/docker/docker/utils" @@ -28,7 +29,7 @@ func (s *TagStore) CmdPull(job *engine.Job) error { var ( localName = job.Args[0] tag string - sf = utils.NewStreamFormatter(job.GetenvBool("json")) + sf = streamformatter.NewStreamFormatter(job.GetenvBool("json")) authConfig = ®istry.AuthConfig{} metaHeaders map[string][]string ) @@ -107,7 +108,7 @@ func (s *TagStore) CmdPull(job *engine.Job) error { return nil } -func (s *TagStore) pullRepository(r *registry.Session, out io.Writer, repoInfo *registry.RepositoryInfo, askedTag string, sf *utils.StreamFormatter, parallel bool) error { +func (s *TagStore) pullRepository(r *registry.Session, out io.Writer, repoInfo *registry.RepositoryInfo, askedTag string, sf *streamformatter.StreamFormatter, parallel bool) error { out.Write(sf.FormatStatus("", "Pulling repository %s", repoInfo.CanonicalName)) repoData, err := r.GetRepositoryData(repoInfo.RemoteName) @@ -265,7 +266,7 @@ func (s *TagStore) pullRepository(r *registry.Session, out io.Writer, repoInfo * return nil } -func (s *TagStore) pullImage(r *registry.Session, out io.Writer, imgID, endpoint string, token []string, sf *utils.StreamFormatter) (bool, error) { +func (s *TagStore) pullImage(r *registry.Session, out io.Writer, imgID, endpoint string, token []string, sf *streamformatter.StreamFormatter) (bool, error) { history, err := r.GetRemoteHistory(imgID, endpoint, token) if err != nil { return false, err @@ -363,7 +364,7 @@ func (s *TagStore) pullImage(r *registry.Session, out io.Writer, imgID, endpoint return layers_downloaded, nil } -func WriteStatus(requestedTag string, out io.Writer, sf *utils.StreamFormatter, layers_downloaded bool) { +func WriteStatus(requestedTag string, out io.Writer, sf *streamformatter.StreamFormatter, layers_downloaded bool) { if layers_downloaded { out.Write(sf.FormatStatus("", "Status: Downloaded newer image for %s", requestedTag)) } else { @@ -382,7 +383,7 @@ type downloadInfo struct { err chan error } -func (s *TagStore) pullV2Repository(eng *engine.Engine, r *registry.Session, out io.Writer, repoInfo *registry.RepositoryInfo, tag string, sf *utils.StreamFormatter, parallel bool) error { +func (s *TagStore) pullV2Repository(eng *engine.Engine, r *registry.Session, out io.Writer, repoInfo *registry.RepositoryInfo, tag string, sf *streamformatter.StreamFormatter, parallel bool) error { endpoint, err := r.V2RegistryEndpoint(repoInfo.Index) if err != nil { if repoInfo.Index.Official { @@ -428,7 +429,7 @@ func (s *TagStore) pullV2Repository(eng *engine.Engine, r *registry.Session, out return nil } -func (s *TagStore) pullV2Tag(eng *engine.Engine, r *registry.Session, out io.Writer, endpoint *registry.Endpoint, repoInfo *registry.RepositoryInfo, tag string, sf *utils.StreamFormatter, parallel bool, auth *registry.RequestAuthorization) (bool, error) { +func (s *TagStore) pullV2Tag(eng *engine.Engine, r *registry.Session, out io.Writer, endpoint *registry.Endpoint, repoInfo *registry.RepositoryInfo, tag string, sf *streamformatter.StreamFormatter, parallel bool, auth *registry.RequestAuthorization) (bool, error) { log.Debugf("Pulling tag from V2 registry: %q", tag) manifestBytes, manifestDigest, err := r.GetV2ImageManifest(endpoint, repoInfo.RemoteName, tag, auth) diff --git a/components/engine/graph/push.go b/components/engine/graph/push.go index 3bbb9676d1..4bd80f1207 100644 --- a/components/engine/graph/push.go +++ b/components/engine/graph/push.go @@ -17,6 +17,7 @@ import ( "github.com/docker/docker/engine" "github.com/docker/docker/image" "github.com/docker/docker/pkg/progressreader" + "github.com/docker/docker/pkg/streamformatter" "github.com/docker/docker/pkg/stringid" "github.com/docker/docker/registry" "github.com/docker/docker/runconfig" @@ -130,7 +131,7 @@ type imagePushData struct { // lookupImageOnEndpoint checks the specified endpoint to see if an image exists // and if it is absent then it sends the image id to the channel to be pushed. -func lookupImageOnEndpoint(wg *sync.WaitGroup, r *registry.Session, out io.Writer, sf *utils.StreamFormatter, +func lookupImageOnEndpoint(wg *sync.WaitGroup, r *registry.Session, out io.Writer, sf *streamformatter.StreamFormatter, images chan imagePushData, imagesToPush chan string) { defer wg.Done() for image := range images { @@ -144,7 +145,7 @@ func lookupImageOnEndpoint(wg *sync.WaitGroup, r *registry.Session, out io.Write } func (s *TagStore) pushImageToEndpoint(endpoint string, out io.Writer, remoteName string, imageIDs []string, - tags map[string][]string, repo *registry.RepositoryData, sf *utils.StreamFormatter, r *registry.Session) error { + tags map[string][]string, repo *registry.RepositoryData, sf *streamformatter.StreamFormatter, r *registry.Session) error { workerCount := len(imageIDs) // start a maximum of 5 workers to check if images exist on the specified endpoint. if workerCount > 5 { @@ -203,7 +204,7 @@ func (s *TagStore) pushImageToEndpoint(endpoint string, out io.Writer, remoteNam // pushRepository pushes layers that do not already exist on the registry. func (s *TagStore) pushRepository(r *registry.Session, out io.Writer, repoInfo *registry.RepositoryInfo, localRepo map[string]string, - tag string, sf *utils.StreamFormatter) error { + tag string, sf *streamformatter.StreamFormatter) error { log.Debugf("Local repo: %s", localRepo) out = utils.NewWriteFlusher(out) imgList, tags, err := s.getImageList(localRepo, tag) @@ -238,7 +239,7 @@ func (s *TagStore) pushRepository(r *registry.Session, out io.Writer, return err } -func (s *TagStore) pushImage(r *registry.Session, out io.Writer, imgID, ep string, token []string, sf *utils.StreamFormatter) (checksum string, err error) { +func (s *TagStore) pushImage(r *registry.Session, out io.Writer, imgID, ep string, token []string, sf *streamformatter.StreamFormatter) (checksum string, err error) { out = utils.NewWriteFlusher(out) jsonRaw, err := ioutil.ReadFile(path.Join(s.graph.Root, imgID, "json")) if err != nil { @@ -292,7 +293,7 @@ func (s *TagStore) pushImage(r *registry.Session, out io.Writer, imgID, ep strin return imgData.Checksum, nil } -func (s *TagStore) pushV2Repository(r *registry.Session, localRepo Repository, out io.Writer, repoInfo *registry.RepositoryInfo, tag string, sf *utils.StreamFormatter) error { +func (s *TagStore) pushV2Repository(r *registry.Session, localRepo Repository, out io.Writer, repoInfo *registry.RepositoryInfo, tag string, sf *streamformatter.StreamFormatter) error { endpoint, err := r.V2RegistryEndpoint(repoInfo.Index) if err != nil { if repoInfo.Index.Official { @@ -442,7 +443,7 @@ func (s *TagStore) pushV2Repository(r *registry.Session, localRepo Repository, o } // PushV2Image pushes the image content to the v2 registry, first buffering the contents to disk -func (s *TagStore) pushV2Image(r *registry.Session, img *image.Image, endpoint *registry.Endpoint, imageName string, sf *utils.StreamFormatter, out io.Writer, auth *registry.RequestAuthorization) (string, error) { +func (s *TagStore) pushV2Image(r *registry.Session, img *image.Image, endpoint *registry.Endpoint, imageName string, sf *streamformatter.StreamFormatter, out io.Writer, auth *registry.RequestAuthorization) (string, error) { out.Write(sf.FormatProgress(stringid.TruncateID(img.ID), "Buffering to Disk", nil)) image, err := s.graph.Get(img.ID) @@ -498,7 +499,7 @@ func (s *TagStore) CmdPush(job *engine.Job) error { } var ( localName = job.Args[0] - sf = utils.NewStreamFormatter(job.GetenvBool("json")) + sf = streamformatter.NewStreamFormatter(job.GetenvBool("json")) authConfig = ®istry.AuthConfig{} metaHeaders map[string][]string ) diff --git a/components/engine/utils/jsonmessage.go b/components/engine/pkg/jsonmessage/jsonmessage.go similarity index 99% rename from components/engine/utils/jsonmessage.go rename to components/engine/pkg/jsonmessage/jsonmessage.go index 74d3112719..7db1626e48 100644 --- a/components/engine/utils/jsonmessage.go +++ b/components/engine/pkg/jsonmessage/jsonmessage.go @@ -1,4 +1,4 @@ -package utils +package jsonmessage import ( "encoding/json" diff --git a/components/engine/utils/jsonmessage_test.go b/components/engine/pkg/jsonmessage/jsonmessage_test.go similarity index 97% rename from components/engine/utils/jsonmessage_test.go rename to components/engine/pkg/jsonmessage/jsonmessage_test.go index b9103da1a4..4c3f5666b3 100644 --- a/components/engine/utils/jsonmessage_test.go +++ b/components/engine/pkg/jsonmessage/jsonmessage_test.go @@ -1,4 +1,4 @@ -package utils +package jsonmessage import ( "testing" diff --git a/components/engine/pkg/progressreader/progressreader.go b/components/engine/pkg/progressreader/progressreader.go index 730559e9fb..e548b07558 100644 --- a/components/engine/pkg/progressreader/progressreader.go +++ b/components/engine/pkg/progressreader/progressreader.go @@ -1,37 +1,16 @@ package progressreader import ( + "github.com/docker/docker/pkg/jsonmessage" + "github.com/docker/docker/pkg/streamformatter" "io" ) -type StreamFormatter interface { - FormatProg(string, string, interface{}) []byte - FormatStatus(string, string, ...interface{}) []byte - FormatError(error) []byte -} - -type PR_JSONProgress interface { - GetCurrent() int - GetTotal() int -} - -type JSONProg struct { - Current int - Total int -} - -func (j *JSONProg) GetCurrent() int { - return j.Current -} -func (j *JSONProg) GetTotal() int { - return j.Total -} - // Reader with progress bar type Config struct { In io.ReadCloser // Stream to read from Out io.Writer // Where to send progress bar to - Formatter StreamFormatter + Formatter *streamformatter.StreamFormatter Size int Current int LastUpdate int @@ -54,7 +33,7 @@ func (config *Config) Read(p []byte) (n int, err error) { } } if config.Current-config.LastUpdate > updateEvery || err != nil { - config.Out.Write(config.Formatter.FormatProg(config.ID, config.Action, &JSONProg{Current: config.Current, Total: config.Size})) + config.Out.Write(config.Formatter.FormatProgress(config.ID, config.Action, &jsonmessage.JSONProgress{Current: config.Current, Total: config.Size})) config.LastUpdate = config.Current } // Send newline when complete @@ -64,6 +43,6 @@ func (config *Config) Read(p []byte) (n int, err error) { return read, err } func (config *Config) Close() error { - config.Out.Write(config.Formatter.FormatProg(config.ID, config.Action, &JSONProg{Current: config.Current, Total: config.Size})) + config.Out.Write(config.Formatter.FormatProgress(config.ID, config.Action, &jsonmessage.JSONProgress{Current: config.Current, Total: config.Size})) return config.In.Close() } diff --git a/components/engine/utils/streamformatter.go b/components/engine/pkg/streamformatter/streamformatter.go similarity index 72% rename from components/engine/utils/streamformatter.go rename to components/engine/pkg/streamformatter/streamformatter.go index e5b15f9835..383e7adf9e 100644 --- a/components/engine/utils/streamformatter.go +++ b/components/engine/pkg/streamformatter/streamformatter.go @@ -1,9 +1,9 @@ -package utils +package streamformatter import ( "encoding/json" "fmt" - "github.com/docker/docker/pkg/progressreader" + "github.com/docker/docker/pkg/jsonmessage" "io" ) @@ -21,7 +21,7 @@ var streamNewlineBytes = []byte(streamNewline) func (sf *StreamFormatter) FormatStream(str string) []byte { if sf.json { - b, err := json.Marshal(&JSONMessage{Stream: str}) + b, err := json.Marshal(&jsonmessage.JSONMessage{Stream: str}) if err != nil { return sf.FormatError(err) } @@ -33,7 +33,7 @@ func (sf *StreamFormatter) FormatStream(str string) []byte { func (sf *StreamFormatter) FormatStatus(id, format string, a ...interface{}) []byte { str := fmt.Sprintf(format, a...) if sf.json { - b, err := json.Marshal(&JSONMessage{ID: id, Status: str}) + b, err := json.Marshal(&jsonmessage.JSONMessage{ID: id, Status: str}) if err != nil { return sf.FormatError(err) } @@ -44,33 +44,25 @@ func (sf *StreamFormatter) FormatStatus(id, format string, a ...interface{}) []b func (sf *StreamFormatter) FormatError(err error) []byte { if sf.json { - jsonError, ok := err.(*JSONError) + jsonError, ok := err.(*jsonmessage.JSONError) if !ok { - jsonError = &JSONError{Message: err.Error()} + jsonError = &jsonmessage.JSONError{Message: err.Error()} } - if b, err := json.Marshal(&JSONMessage{Error: jsonError, ErrorMessage: err.Error()}); err == nil { + if b, err := json.Marshal(&jsonmessage.JSONMessage{Error: jsonError, ErrorMessage: err.Error()}); err == nil { return append(b, streamNewlineBytes...) } return []byte("{\"error\":\"format error\"}" + streamNewline) } return []byte("Error: " + err.Error() + streamNewline) } -func (sf *StreamFormatter) FormatProg(id, action string, p interface{}) []byte { - switch progress := p.(type) { - case *JSONProgress: - return sf.FormatProgress(id, action, progress) - case progressreader.PR_JSONProgress: - return sf.FormatProgress(id, action, &JSONProgress{Current: progress.GetCurrent(), Total: progress.GetTotal()}) - } - return nil -} -func (sf *StreamFormatter) FormatProgress(id, action string, progress *JSONProgress) []byte { + +func (sf *StreamFormatter) FormatProgress(id, action string, progress *jsonmessage.JSONProgress) []byte { if progress == nil { - progress = &JSONProgress{} + progress = &jsonmessage.JSONProgress{} } if sf.json { - b, err := json.Marshal(&JSONMessage{ + b, err := json.Marshal(&jsonmessage.JSONMessage{ Status: action, ProgressMessage: progress.String(), Progress: progress, diff --git a/components/engine/utils/streamformatter_test.go b/components/engine/pkg/streamformatter/streamformatter_test.go similarity index 88% rename from components/engine/utils/streamformatter_test.go rename to components/engine/pkg/streamformatter/streamformatter_test.go index 20610f6c01..edc432e900 100644 --- a/components/engine/utils/streamformatter_test.go +++ b/components/engine/pkg/streamformatter/streamformatter_test.go @@ -1,8 +1,9 @@ -package utils +package streamformatter import ( "encoding/json" "errors" + "github.com/docker/docker/pkg/jsonmessage" "reflect" "testing" ) @@ -33,7 +34,7 @@ func TestFormatSimpleError(t *testing.T) { func TestFormatJSONError(t *testing.T) { sf := NewStreamFormatter(true) - err := &JSONError{Code: 50, Message: "Json error"} + err := &jsonmessage.JSONError{Code: 50, Message: "Json error"} res := sf.FormatError(err) if string(res) != `{"errorDetail":{"code":50,"message":"Json error"},"error":"Json error"}`+"\r\n" { t.Fatalf("%q", res) @@ -42,13 +43,13 @@ func TestFormatJSONError(t *testing.T) { func TestFormatProgress(t *testing.T) { sf := NewStreamFormatter(true) - progress := &JSONProgress{ + progress := &jsonmessage.JSONProgress{ Current: 15, Total: 30, Start: 1, } res := sf.FormatProgress("id", "action", progress) - msg := &JSONMessage{} + msg := &jsonmessage.JSONMessage{} if err := json.Unmarshal(res, msg); err != nil { t.Fatal(err) } diff --git a/components/engine/utils/utils.go b/components/engine/utils/utils.go index d5ebb68c99..4a765eb09c 100644 --- a/components/engine/utils/utils.go +++ b/components/engine/utils/utils.go @@ -23,6 +23,7 @@ import ( "github.com/docker/docker/pkg/archive" "github.com/docker/docker/pkg/fileutils" "github.com/docker/docker/pkg/ioutils" + "github.com/docker/docker/pkg/jsonmessage" "github.com/docker/docker/pkg/stringutils" ) @@ -254,7 +255,7 @@ func NewWriteFlusher(w io.Writer) *WriteFlusher { } func NewHTTPRequestError(msg string, res *http.Response) error { - return &JSONError{ + return &jsonmessage.JSONError{ Message: msg, Code: res.StatusCode, }