From aca1270104802347f5aa3104959282924b631daa Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Mon, 13 Jan 2014 11:20:33 -0800 Subject: [PATCH] add legacy compat Docker-DCO-1.1-Signed-off-by: Victor Vieux (github: vieux) --- api.go | 1 + api_params.go | 36 ------------------------------------ server.go | 31 +++++++++++++++++++++++++------ sorter.go | 33 --------------------------------- 4 files changed, 26 insertions(+), 75 deletions(-) diff --git a/api.go b/api.go index 7e874c9f12..3bf19fd948 100644 --- a/api.go +++ b/api.go @@ -185,6 +185,7 @@ func getImagesJSON(srv *Server, version float64, w http.ResponseWriter, r *http. job.Setenv("filter", r.Form.Get("filter")) job.Setenv("all", r.Form.Get("all")) job.SetenvBool("list", version <= 1.8) + job.SetenvBool("legacy", version <= 1.7) job.Stdout.Add(w) w.WriteHeader(http.StatusOK) if err := job.Run(); err != nil { diff --git a/api_params.go b/api_params.go index fa9eab0c15..cfe80c8b0b 100644 --- a/api_params.go +++ b/api_params.go @@ -1,7 +1,5 @@ package docker -import "strings" - type ( APIHistory struct { ID string `json:"Id"` @@ -11,24 +9,6 @@ type ( Size int64 } - APIImages struct { - ID string `json:"Id"` - RepoTags []string `json:",omitempty"` - Created int64 - Size int64 - VirtualSize int64 - ParentId string `json:",omitempty"` - } - - APIImagesOld struct { - Repository string `json:",omitempty"` - Tag string `json:",omitempty"` - ID string `json:"Id"` - Created int64 - Size int64 - VirtualSize int64 - } - APITop struct { Titles []string Processes [][]string @@ -101,22 +81,6 @@ type ( } ) -func (api APIImages) ToLegacy() []APIImagesOld { - outs := []APIImagesOld{} - for _, repotag := range api.RepoTags { - components := strings.SplitN(repotag, ":", 2) - outs = append(outs, APIImagesOld{ - ID: api.ID, - Repository: components[0], - Tag: components[1], - Created: api.Created, - Size: api.Size, - VirtualSize: api.VirtualSize, - }) - } - return outs -} - func (api APIContainers) ToLegacy() *APIContainersOld { return &APIContainersOld{ ID: api.ID, diff --git a/server.go b/server.go index a0c545fbc4..18bda2abd2 100644 --- a/server.go +++ b/server.go @@ -601,13 +601,27 @@ func (srv *Server) Images(job *engine.Job) engine.Status { } if out, exists := lookup[id]; exists { - repotag := fmt.Sprintf("%s:%s", name, tag) - out.SetList("RepoTags", append(out.GetList("RepoTags"), repotag)) + if job.GetenvBool("legacy") { + out2 := &engine.Env{} + out2.Set("Repository", name) + out2.Set("Tag", tag) + out2.Set("ID", out.Get("ID")) + out2.SetInt64("Created", out.GetInt64("Created")) + out2.SetInt64("Size", out.GetInt64("Size")) + out2.SetInt64("VirtualSize", out.GetInt64("VirtualSize")) + } else { + out.SetList("RepoTags", append(out.GetList("RepoTags"), fmt.Sprintf("%s:%s", name, tag))) + } } else { out := &engine.Env{} delete(allImages, id) - out.Set("ParentId", image.Parent) - out.SetList("RepoTags", []string{fmt.Sprintf("%s:%s", name, tag)}) + if job.GetenvBool("legacy") { + out.Set("Repository", name) + out.Set("Tag", tag) + } else { + out.Set("ParentId", image.Parent) + out.SetList("RepoTags", []string{fmt.Sprintf("%s:%s", name, tag)}) + } out.Set("ID", image.ID) out.SetInt64("Created", image.Created.Unix()) out.SetInt64("Size", image.Size) @@ -627,8 +641,13 @@ func (srv *Server) Images(job *engine.Job) engine.Status { if job.Getenv("filter") == "" { for _, image := range allImages { out := &engine.Env{} - out.Set("ParentId", image.Parent) - out.SetList("RepoTags", []string{":"}) + if job.GetenvBool("legacy") { + out.Set("Repository", "") + out.Set("Tag", "") + } else { + out.Set("ParentId", image.Parent) + out.SetList("RepoTags", []string{":"}) + } out.Set("ID", image.ID) out.SetInt64("Created", image.Created.Unix()) out.SetInt64("Size", image.Size) diff --git a/sorter.go b/sorter.go index c9a86b45c0..9b3e1a9486 100644 --- a/sorter.go +++ b/sorter.go @@ -2,39 +2,6 @@ package docker import "sort" -type imageSorter struct { - images []APIImages - by func(i1, i2 *APIImages) bool // Closure used in the Less method. -} - -// Len is part of sort.Interface. -func (s *imageSorter) Len() int { - return len(s.images) -} - -// Swap is part of sort.Interface. -func (s *imageSorter) Swap(i, j int) { - s.images[i], s.images[j] = s.images[j], s.images[i] -} - -// Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter. -func (s *imageSorter) Less(i, j int) bool { - return s.by(&s.images[i], &s.images[j]) -} - -// Sort []ApiImages by most recent creation date and tag name. -func sortImagesByCreationAndTag(images []APIImages) { - creationAndTag := func(i1, i2 *APIImages) bool { - return i1.Created > i2.Created - } - - sorter := &imageSorter{ - images: images, - by: creationAndTag} - - sort.Sort(sorter) -} - type portSorter struct { ports []Port by func(i, j Port) bool