github.com/xeptore/docker-cli@v20.10.14+incompatible/cli/command/registry/search.go (about)

     1  package registry
     2  
     3  import (
     4  	"context"
     5  	"sort"
     6  
     7  	"github.com/docker/cli/cli"
     8  	"github.com/docker/cli/cli/command"
     9  	"github.com/docker/cli/cli/command/formatter"
    10  	"github.com/docker/cli/opts"
    11  	"github.com/docker/docker/api/types"
    12  	"github.com/docker/docker/registry"
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  type searchOptions struct {
    17  	format  string
    18  	term    string
    19  	noTrunc bool
    20  	limit   int
    21  	filter  opts.FilterOpt
    22  }
    23  
    24  // NewSearchCommand creates a new `docker search` command
    25  func NewSearchCommand(dockerCli command.Cli) *cobra.Command {
    26  	options := searchOptions{filter: opts.NewFilterOpt()}
    27  
    28  	cmd := &cobra.Command{
    29  		Use:   "search [OPTIONS] TERM",
    30  		Short: "Search the Docker Hub for images",
    31  		Args:  cli.ExactArgs(1),
    32  		RunE: func(cmd *cobra.Command, args []string) error {
    33  			options.term = args[0]
    34  			return runSearch(dockerCli, options)
    35  		},
    36  	}
    37  
    38  	flags := cmd.Flags()
    39  
    40  	flags.BoolVar(&options.noTrunc, "no-trunc", false, "Don't truncate output")
    41  	flags.VarP(&options.filter, "filter", "f", "Filter output based on conditions provided")
    42  	flags.IntVar(&options.limit, "limit", registry.DefaultSearchLimit, "Max number of search results")
    43  	flags.StringVar(&options.format, "format", "", "Pretty-print search using a Go template")
    44  
    45  	return cmd
    46  }
    47  
    48  func runSearch(dockerCli command.Cli, options searchOptions) error {
    49  	indexInfo, err := registry.ParseSearchIndexInfo(options.term)
    50  	if err != nil {
    51  		return err
    52  	}
    53  
    54  	ctx := context.Background()
    55  
    56  	authConfig := command.ResolveAuthConfig(ctx, dockerCli, indexInfo)
    57  	requestPrivilege := command.RegistryAuthenticationPrivilegedFunc(dockerCli, indexInfo, "search")
    58  
    59  	encodedAuth, err := command.EncodeAuthToBase64(authConfig)
    60  	if err != nil {
    61  		return err
    62  	}
    63  
    64  	searchOptions := types.ImageSearchOptions{
    65  		RegistryAuth:  encodedAuth,
    66  		PrivilegeFunc: requestPrivilege,
    67  		Filters:       options.filter.Value(),
    68  		Limit:         options.limit,
    69  	}
    70  
    71  	clnt := dockerCli.Client()
    72  
    73  	results, err := clnt.ImageSearch(ctx, options.term, searchOptions)
    74  	if err != nil {
    75  		return err
    76  	}
    77  
    78  	sort.Slice(results, func(i, j int) bool {
    79  		return results[j].StarCount < results[i].StarCount
    80  	})
    81  	searchCtx := formatter.Context{
    82  		Output: dockerCli.Out(),
    83  		Format: NewSearchFormat(options.format),
    84  		Trunc:  !options.noTrunc,
    85  	}
    86  	return SearchWrite(searchCtx, results)
    87  }