github.1git.de/docker/cli@v26.1.3+incompatible/cli/command/registry/search.go (about)

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