浏览代码

Merge pull request #12060 from duglin/RemoveTable3

Remove engine.Table from docker search and fix missing field
Jessie Frazelle 10 年之前
父节点
当前提交
b2fbd9e632
共有 2 个文件被更改,包括 22 次插入12 次删除
  1. 21 12
      api/client/search.go
  2. 1 0
      registry/types.go

+ 21 - 12
api/client/search.go

@@ -1,18 +1,25 @@
 package client
 
 import (
+	"encoding/json"
 	"fmt"
 	"net/url"
+	"sort"
 	"strings"
 	"text/tabwriter"
 
-	"github.com/docker/docker/engine"
 	flag "github.com/docker/docker/pkg/mflag"
 	"github.com/docker/docker/pkg/parsers"
 	"github.com/docker/docker/registry"
 	"github.com/docker/docker/utils"
 )
 
+type ByStars []registry.SearchResult
+
+func (r ByStars) Len() int           { return len(r) }
+func (r ByStars) Swap(i, j int)      { r[i], r[j] = r[j], r[i] }
+func (r ByStars) Less(i, j int) bool { return r[i].StarCount < r[j].StarCount }
+
 // CmdSearch searches the Docker Hub for images.
 //
 // Usage: docker search [OPTIONS] TERM
@@ -39,35 +46,37 @@ func (cli *DockerCli) CmdSearch(args ...string) error {
 
 	cli.LoadConfigFile()
 
-	body, statusCode, errReq := cli.clientRequestAttemptLogin("GET", "/images/search?"+v.Encode(), nil, nil, repoInfo.Index, "search")
-	rawBody, _, err := readBody(body, statusCode, errReq)
+	rdr, _, err := cli.clientRequestAttemptLogin("GET", "/images/search?"+v.Encode(), nil, nil, repoInfo.Index, "search")
 	if err != nil {
 		return err
 	}
 
-	outs := engine.NewTable("star_count", 0)
-	if _, err := outs.ReadListFrom(rawBody); err != nil {
+	results := ByStars{}
+	err = json.NewDecoder(rdr).Decode(&results)
+	if err != nil {
 		return err
 	}
-	outs.ReverseSort()
+
+	sort.Sort(sort.Reverse(results))
+
 	w := tabwriter.NewWriter(cli.out, 10, 1, 3, ' ', 0)
 	fmt.Fprintf(w, "NAME\tDESCRIPTION\tSTARS\tOFFICIAL\tAUTOMATED\n")
-	for _, out := range outs.Data {
-		if ((*automated || *trusted) && (!out.GetBool("is_trusted") && !out.GetBool("is_automated"))) || (*stars > uint(out.GetInt("star_count"))) {
+	for _, res := range results {
+		if ((*automated || *trusted) && (!res.IsTrusted && !res.IsAutomated)) || (int(*stars) > res.StarCount) {
 			continue
 		}
-		desc := strings.Replace(out.Get("description"), "\n", " ", -1)
+		desc := strings.Replace(res.Description, "\n", " ", -1)
 		desc = strings.Replace(desc, "\r", " ", -1)
 		if !*noTrunc && len(desc) > 45 {
 			desc = utils.Trunc(desc, 42) + "..."
 		}
-		fmt.Fprintf(w, "%s\t%s\t%d\t", out.Get("name"), desc, uint(out.GetInt("star_count")))
-		if out.GetBool("is_official") {
+		fmt.Fprintf(w, "%s\t%s\t%d\t", res.Name, desc, res.StarCount)
+		if res.IsOfficial {
 			fmt.Fprint(w, "[OK]")
 
 		}
 		fmt.Fprint(w, "\t")
-		if out.GetBool("is_automated") || out.GetBool("is_trusted") {
+		if res.IsAutomated || res.IsTrusted {
 			fmt.Fprint(w, "[OK]")
 		}
 		fmt.Fprint(w, "\n")

+ 1 - 0
registry/types.go

@@ -5,6 +5,7 @@ type SearchResult struct {
 	IsOfficial  bool   `json:"is_official"`
 	Name        string `json:"name"`
 	IsTrusted   bool   `json:"is_trusted"`
+	IsAutomated bool   `json:"is_automated"`
 	Description string `json:"description"`
 }