Browse code

Remove engine.Table from docker search and fix missing field

registry/SearchResults was missing the "is_automated" field.
I added it back in.

Pull this 'table' removal one from the others because it fixed
a bug too

Signed-off-by: Doug Davis <dug@us.ibm.com>

Doug Davis authored on 2015/04/04 02:29:30
Showing 2 changed files
... ...
@@ -1,18 +1,25 @@
1 1
 package client
2 2
 
3 3
 import (
4
+	"encoding/json"
4 5
 	"fmt"
5 6
 	"net/url"
7
+	"sort"
6 8
 	"strings"
7 9
 	"text/tabwriter"
8 10
 
9
-	"github.com/docker/docker/engine"
10 11
 	flag "github.com/docker/docker/pkg/mflag"
11 12
 	"github.com/docker/docker/pkg/parsers"
12 13
 	"github.com/docker/docker/registry"
13 14
 	"github.com/docker/docker/utils"
14 15
 )
15 16
 
17
+type ByStars []registry.SearchResult
18
+
19
+func (r ByStars) Len() int           { return len(r) }
20
+func (r ByStars) Swap(i, j int)      { r[i], r[j] = r[j], r[i] }
21
+func (r ByStars) Less(i, j int) bool { return r[i].StarCount < r[j].StarCount }
22
+
16 23
 // CmdSearch searches the Docker Hub for images.
17 24
 //
18 25
 // Usage: docker search [OPTIONS] TERM
... ...
@@ -39,35 +46,37 @@ func (cli *DockerCli) CmdSearch(args ...string) error {
39 39
 
40 40
 	cli.LoadConfigFile()
41 41
 
42
-	body, statusCode, errReq := cli.clientRequestAttemptLogin("GET", "/images/search?"+v.Encode(), nil, nil, repoInfo.Index, "search")
43
-	rawBody, _, err := readBody(body, statusCode, errReq)
42
+	rdr, _, err := cli.clientRequestAttemptLogin("GET", "/images/search?"+v.Encode(), nil, nil, repoInfo.Index, "search")
44 43
 	if err != nil {
45 44
 		return err
46 45
 	}
47 46
 
48
-	outs := engine.NewTable("star_count", 0)
49
-	if _, err := outs.ReadListFrom(rawBody); err != nil {
47
+	results := ByStars{}
48
+	err = json.NewDecoder(rdr).Decode(&results)
49
+	if err != nil {
50 50
 		return err
51 51
 	}
52
-	outs.ReverseSort()
52
+
53
+	sort.Sort(sort.Reverse(results))
54
+
53 55
 	w := tabwriter.NewWriter(cli.out, 10, 1, 3, ' ', 0)
54 56
 	fmt.Fprintf(w, "NAME\tDESCRIPTION\tSTARS\tOFFICIAL\tAUTOMATED\n")
55
-	for _, out := range outs.Data {
56
-		if ((*automated || *trusted) && (!out.GetBool("is_trusted") && !out.GetBool("is_automated"))) || (*stars > uint(out.GetInt("star_count"))) {
57
+	for _, res := range results {
58
+		if ((*automated || *trusted) && (!res.IsTrusted && !res.IsAutomated)) || (int(*stars) > res.StarCount) {
57 59
 			continue
58 60
 		}
59
-		desc := strings.Replace(out.Get("description"), "\n", " ", -1)
61
+		desc := strings.Replace(res.Description, "\n", " ", -1)
60 62
 		desc = strings.Replace(desc, "\r", " ", -1)
61 63
 		if !*noTrunc && len(desc) > 45 {
62 64
 			desc = utils.Trunc(desc, 42) + "..."
63 65
 		}
64
-		fmt.Fprintf(w, "%s\t%s\t%d\t", out.Get("name"), desc, uint(out.GetInt("star_count")))
65
-		if out.GetBool("is_official") {
66
+		fmt.Fprintf(w, "%s\t%s\t%d\t", res.Name, desc, res.StarCount)
67
+		if res.IsOfficial {
66 68
 			fmt.Fprint(w, "[OK]")
67 69
 
68 70
 		}
69 71
 		fmt.Fprint(w, "\t")
70
-		if out.GetBool("is_automated") || out.GetBool("is_trusted") {
72
+		if res.IsAutomated || res.IsTrusted {
71 73
 			fmt.Fprint(w, "[OK]")
72 74
 		}
73 75
 		fmt.Fprint(w, "\n")
... ...
@@ -5,6 +5,7 @@ type SearchResult struct {
5 5
 	IsOfficial  bool   `json:"is_official"`
6 6
 	Name        string `json:"name"`
7 7
 	IsTrusted   bool   `json:"is_trusted"`
8
+	IsAutomated bool   `json:"is_automated"`
8 9
 	Description string `json:"description"`
9 10
 }
10 11