Browse code

add legacy compat

Docker-DCO-1.1-Signed-off-by: Victor Vieux <victor.vieux@docker.com> (github: vieux)

Victor Vieux authored on 2014/01/14 04:20:33
Showing 4 changed files
... ...
@@ -185,6 +185,7 @@ func getImagesJSON(srv *Server, version float64, w http.ResponseWriter, r *http.
185 185
 	job.Setenv("filter", r.Form.Get("filter"))
186 186
 	job.Setenv("all", r.Form.Get("all"))
187 187
 	job.SetenvBool("list", version <= 1.8)
188
+	job.SetenvBool("legacy", version <= 1.7)
188 189
 	job.Stdout.Add(w)
189 190
 	w.WriteHeader(http.StatusOK)
190 191
 	if err := job.Run(); err != nil {
... ...
@@ -1,7 +1,5 @@
1 1
 package docker
2 2
 
3
-import "strings"
4
-
5 3
 type (
6 4
 	APIHistory struct {
7 5
 		ID        string   `json:"Id"`
... ...
@@ -11,24 +9,6 @@ type (
11 11
 		Size      int64
12 12
 	}
13 13
 
14
-	APIImages struct {
15
-		ID          string   `json:"Id"`
16
-		RepoTags    []string `json:",omitempty"`
17
-		Created     int64
18
-		Size        int64
19
-		VirtualSize int64
20
-		ParentId    string `json:",omitempty"`
21
-	}
22
-
23
-	APIImagesOld struct {
24
-		Repository  string `json:",omitempty"`
25
-		Tag         string `json:",omitempty"`
26
-		ID          string `json:"Id"`
27
-		Created     int64
28
-		Size        int64
29
-		VirtualSize int64
30
-	}
31
-
32 14
 	APITop struct {
33 15
 		Titles    []string
34 16
 		Processes [][]string
... ...
@@ -101,22 +81,6 @@ type (
101 101
 	}
102 102
 )
103 103
 
104
-func (api APIImages) ToLegacy() []APIImagesOld {
105
-	outs := []APIImagesOld{}
106
-	for _, repotag := range api.RepoTags {
107
-		components := strings.SplitN(repotag, ":", 2)
108
-		outs = append(outs, APIImagesOld{
109
-			ID:          api.ID,
110
-			Repository:  components[0],
111
-			Tag:         components[1],
112
-			Created:     api.Created,
113
-			Size:        api.Size,
114
-			VirtualSize: api.VirtualSize,
115
-		})
116
-	}
117
-	return outs
118
-}
119
-
120 104
 func (api APIContainers) ToLegacy() *APIContainersOld {
121 105
 	return &APIContainersOld{
122 106
 		ID:         api.ID,
... ...
@@ -601,13 +601,27 @@ func (srv *Server) Images(job *engine.Job) engine.Status {
601 601
 			}
602 602
 
603 603
 			if out, exists := lookup[id]; exists {
604
-				repotag := fmt.Sprintf("%s:%s", name, tag)
605
-				out.SetList("RepoTags", append(out.GetList("RepoTags"), repotag))
604
+				if job.GetenvBool("legacy") {
605
+					out2 := &engine.Env{}
606
+					out2.Set("Repository", name)
607
+					out2.Set("Tag", tag)
608
+					out2.Set("ID", out.Get("ID"))
609
+					out2.SetInt64("Created", out.GetInt64("Created"))
610
+					out2.SetInt64("Size", out.GetInt64("Size"))
611
+					out2.SetInt64("VirtualSize", out.GetInt64("VirtualSize"))
612
+				} else {
613
+					out.SetList("RepoTags", append(out.GetList("RepoTags"), fmt.Sprintf("%s:%s", name, tag)))
614
+				}
606 615
 			} else {
607 616
 				out := &engine.Env{}
608 617
 				delete(allImages, id)
609
-				out.Set("ParentId", image.Parent)
610
-				out.SetList("RepoTags", []string{fmt.Sprintf("%s:%s", name, tag)})
618
+				if job.GetenvBool("legacy") {
619
+					out.Set("Repository", name)
620
+					out.Set("Tag", tag)
621
+				} else {
622
+					out.Set("ParentId", image.Parent)
623
+					out.SetList("RepoTags", []string{fmt.Sprintf("%s:%s", name, tag)})
624
+				}
611 625
 				out.Set("ID", image.ID)
612 626
 				out.SetInt64("Created", image.Created.Unix())
613 627
 				out.SetInt64("Size", image.Size)
... ...
@@ -627,8 +641,13 @@ func (srv *Server) Images(job *engine.Job) engine.Status {
627 627
 	if job.Getenv("filter") == "" {
628 628
 		for _, image := range allImages {
629 629
 			out := &engine.Env{}
630
-			out.Set("ParentId", image.Parent)
631
-			out.SetList("RepoTags", []string{"<none>:<none>"})
630
+			if job.GetenvBool("legacy") {
631
+				out.Set("Repository", "<none>")
632
+				out.Set("Tag", "<none>")
633
+			} else {
634
+				out.Set("ParentId", image.Parent)
635
+				out.SetList("RepoTags", []string{"<none>:<none>"})
636
+			}
632 637
 			out.Set("ID", image.ID)
633 638
 			out.SetInt64("Created", image.Created.Unix())
634 639
 			out.SetInt64("Size", image.Size)
... ...
@@ -2,39 +2,6 @@ package docker
2 2
 
3 3
 import "sort"
4 4
 
5
-type imageSorter struct {
6
-	images []APIImages
7
-	by     func(i1, i2 *APIImages) bool // Closure used in the Less method.
8
-}
9
-
10
-// Len is part of sort.Interface.
11
-func (s *imageSorter) Len() int {
12
-	return len(s.images)
13
-}
14
-
15
-// Swap is part of sort.Interface.
16
-func (s *imageSorter) Swap(i, j int) {
17
-	s.images[i], s.images[j] = s.images[j], s.images[i]
18
-}
19
-
20
-// Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.
21
-func (s *imageSorter) Less(i, j int) bool {
22
-	return s.by(&s.images[i], &s.images[j])
23
-}
24
-
25
-// Sort []ApiImages by most recent creation date and tag name.
26
-func sortImagesByCreationAndTag(images []APIImages) {
27
-	creationAndTag := func(i1, i2 *APIImages) bool {
28
-		return i1.Created > i2.Created
29
-	}
30
-
31
-	sorter := &imageSorter{
32
-		images: images,
33
-		by:     creationAndTag}
34
-
35
-	sort.Sort(sorter)
36
-}
37
-
38 5
 type portSorter struct {
39 6
 	ports []Port
40 7
 	by    func(i, j Port) bool