github.com/ncdc/docker@v0.10.1-0.20160129113957-6c6729ef5b74/api/client/search.go (about)

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