Skip to content

Commit b2fbd9e

Browse files
author
Jessie Frazelle
committed
Merge pull request moby#12060 from duglin/RemoveTable3
Remove engine.Table from docker search and fix missing field
2 parents 9cdad8a + 67b4cce commit b2fbd9e

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

api/client/search.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
package client
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"net/url"
7+
"sort"
68
"strings"
79
"text/tabwriter"
810

9-
"github.com/docker/docker/engine"
1011
flag "github.com/docker/docker/pkg/mflag"
1112
"github.com/docker/docker/pkg/parsers"
1213
"github.com/docker/docker/registry"
1314
"github.com/docker/docker/utils"
1415
)
1516

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+
1623
// CmdSearch searches the Docker Hub for images.
1724
//
1825
// Usage: docker search [OPTIONS] TERM
@@ -39,35 +46,37 @@ func (cli *DockerCli) CmdSearch(args ...string) error {
3946

4047
cli.LoadConfigFile()
4148

42-
body, statusCode, errReq := cli.clientRequestAttemptLogin("GET", "/images/search?"+v.Encode(), nil, nil, repoInfo.Index, "search")
43-
rawBody, _, err := readBody(body, statusCode, errReq)
49+
rdr, _, err := cli.clientRequestAttemptLogin("GET", "/images/search?"+v.Encode(), nil, nil, repoInfo.Index, "search")
4450
if err != nil {
4551
return err
4652
}
4753

48-
outs := engine.NewTable("star_count", 0)
49-
if _, err := outs.ReadListFrom(rawBody); err != nil {
54+
results := ByStars{}
55+
err = json.NewDecoder(rdr).Decode(&results)
56+
if err != nil {
5057
return err
5158
}
52-
outs.ReverseSort()
59+
60+
sort.Sort(sort.Reverse(results))
61+
5362
w := tabwriter.NewWriter(cli.out, 10, 1, 3, ' ', 0)
5463
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"))) {
64+
for _, res := range results {
65+
if ((*automated || *trusted) && (!res.IsTrusted && !res.IsAutomated)) || (int(*stars) > res.StarCount) {
5766
continue
5867
}
59-
desc := strings.Replace(out.Get("description"), "\n", " ", -1)
68+
desc := strings.Replace(res.Description, "\n", " ", -1)
6069
desc = strings.Replace(desc, "\r", " ", -1)
6170
if !*noTrunc && len(desc) > 45 {
6271
desc = utils.Trunc(desc, 42) + "..."
6372
}
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") {
73+
fmt.Fprintf(w, "%s\t%s\t%d\t", res.Name, desc, res.StarCount)
74+
if res.IsOfficial {
6675
fmt.Fprint(w, "[OK]")
6776

6877
}
6978
fmt.Fprint(w, "\t")
70-
if out.GetBool("is_automated") || out.GetBool("is_trusted") {
79+
if res.IsAutomated || res.IsTrusted {
7180
fmt.Fprint(w, "[OK]")
7281
}
7382
fmt.Fprint(w, "\n")

registry/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ type SearchResult struct {
55
IsOfficial bool `json:"is_official"`
66
Name string `json:"name"`
77
IsTrusted bool `json:"is_trusted"`
8+
IsAutomated bool `json:"is_automated"`
89
Description string `json:"description"`
910
}
1011

0 commit comments

Comments
 (0)