github.com/a4a881d4/docker@v1.9.0-rc2/api/client/search.go (about)

     1  package client
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/url"
     7  	"sort"
     8  	"strings"
     9  	"text/tabwriter"
    10  
    11  	Cli "github.com/docker/docker/cli"
    12  	flag "github.com/docker/docker/pkg/mflag"
    13  	"github.com/docker/docker/pkg/parsers"
    14  	"github.com/docker/docker/pkg/stringutils"
    15  	"github.com/docker/docker/registry"
    16  )
    17  
    18  // ByStars sorts search results in ascending order by number of stars.
    19  type ByStars []registry.SearchResult
    20  
    21  func (r ByStars) Len() int           { return len(r) }
    22  func (r ByStars) Swap(i, j int)      { r[i], r[j] = r[j], r[i] }
    23  func (r ByStars) Less(i, j int) bool { return r[i].StarCount < r[j].StarCount }
    24  
    25  // CmdSearch searches the Docker Hub for images.
    26  //
    27  // Usage: docker search [OPTIONS] TERM
    28  func (cli *DockerCli) CmdSearch(args ...string) error {
    29  	cmd := Cli.Subcmd("search", []string{"TERM"}, Cli.DockerCommands["search"].Description, true)
    30  	noTrunc := cmd.Bool([]string{"#notrunc", "-no-trunc"}, false, "Don't truncate output")
    31  	trusted := cmd.Bool([]string{"#t", "#trusted", "#-trusted"}, false, "Only show trusted builds")
    32  	automated := cmd.Bool([]string{"-automated"}, false, "Only show automated builds")
    33  	stars := cmd.Uint([]string{"s", "#stars", "-stars"}, 0, "Only displays with at least x stars")
    34  	cmd.Require(flag.Exact, 1)
    35  
    36  	cmd.ParseFlags(args, true)
    37  
    38  	name := cmd.Arg(0)
    39  	v := url.Values{}
    40  	v.Set("term", name)
    41  
    42  	// Resolve the Repository name from fqn to hostname + name
    43  	taglessRemote, _ := parsers.ParseRepositoryTag(name)
    44  
    45  	indexInfo, err := registry.ParseIndexInfo(taglessRemote)
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	rdr, _, err := cli.clientRequestAttemptLogin("GET", "/images/search?"+v.Encode(), nil, nil, indexInfo, "search")
    51  	if err != nil {
    52  		return err
    53  	}
    54  
    55  	defer rdr.Close()
    56  
    57  	results := ByStars{}
    58  	if err := json.NewDecoder(rdr).Decode(&results); err != nil {
    59  		return err
    60  	}
    61  
    62  	sort.Sort(sort.Reverse(results))
    63  
    64  	w := tabwriter.NewWriter(cli.out, 10, 1, 3, ' ', 0)
    65  	fmt.Fprintf(w, "NAME\tDESCRIPTION\tSTARS\tOFFICIAL\tAUTOMATED\n")
    66  	for _, res := range results {
    67  		if (*automated && !res.IsAutomated) || (int(*stars) > res.StarCount) || (*trusted && !res.IsTrusted) {
    68  			continue
    69  		}
    70  		desc := strings.Replace(res.Description, "\n", " ", -1)
    71  		desc = strings.Replace(desc, "\r", " ", -1)
    72  		if !*noTrunc && len(desc) > 45 {
    73  			desc = stringutils.Truncate(desc, 42) + "..."
    74  		}
    75  		fmt.Fprintf(w, "%s\t%s\t%d\t", res.Name, desc, res.StarCount)
    76  		if res.IsOfficial {
    77  			fmt.Fprint(w, "[OK]")
    78  
    79  		}
    80  		fmt.Fprint(w, "\t")
    81  		if res.IsAutomated || res.IsTrusted {
    82  			fmt.Fprint(w, "[OK]")
    83  		}
    84  		fmt.Fprint(w, "\n")
    85  	}
    86  	w.Flush()
    87  	return nil
    88  }