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