11package client
22
33import (
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\t DESCRIPTION\t STARS\t OFFICIAL\t AUTOMATED\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 " )
0 commit comments