2016-08-29 09:59:41 -04:00
|
|
|
package registry
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2017-08-15 06:58:49 -04:00
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
2017-05-15 08:45:19 -04:00
|
|
|
"github.com/docker/cli/opts"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
registrytypes "github.com/docker/docker/api/types/registry"
|
|
|
|
"github.com/docker/docker/registry"
|
|
|
|
"github.com/spf13/cobra"
|
2017-05-15 08:45:19 -04:00
|
|
|
"golang.org/x/net/context"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type searchOptions struct {
|
2017-08-15 06:58:49 -04:00
|
|
|
format string
|
2016-09-08 13:11:39 -04:00
|
|
|
term string
|
|
|
|
noTrunc bool
|
|
|
|
limit int
|
2016-09-13 14:53:11 -04:00
|
|
|
filter opts.FilterOpt
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
// Deprecated
|
|
|
|
stars uint
|
|
|
|
automated bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSearchCommand creates a new `docker search` command
|
2017-05-03 17:58:52 -04:00
|
|
|
func NewSearchCommand(dockerCli command.Cli) *cobra.Command {
|
2017-05-15 08:45:19 -04:00
|
|
|
options := searchOptions{filter: opts.NewFilterOpt()}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "search [OPTIONS] TERM",
|
|
|
|
Short: "Search the Docker Hub for images",
|
|
|
|
Args: cli.ExactArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2017-05-15 08:45:19 -04:00
|
|
|
options.term = args[0]
|
|
|
|
return runSearch(dockerCli, options)
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
flags.BoolVar(&options.noTrunc, "no-trunc", false, "Don't truncate output")
|
|
|
|
flags.VarP(&options.filter, "filter", "f", "Filter output based on conditions provided")
|
|
|
|
flags.IntVar(&options.limit, "limit", registry.DefaultSearchLimit, "Max number of search results")
|
2017-08-15 06:58:49 -04:00
|
|
|
flags.StringVar(&options.format, "format", "", "Pretty-print search using a Go template")
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
flags.BoolVar(&options.automated, "automated", false, "Only show automated builds")
|
|
|
|
flags.UintVarP(&options.stars, "stars", "s", 0, "Only displays with at least x stars")
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2017-03-16 10:33:24 -04:00
|
|
|
flags.MarkDeprecated("automated", "use --filter=is-automated=true instead")
|
2016-09-08 13:11:39 -04:00
|
|
|
flags.MarkDeprecated("stars", "use --filter=stars=3 instead")
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
func runSearch(dockerCli command.Cli, options searchOptions) error {
|
|
|
|
indexInfo, err := registry.ParseSearchIndexInfo(options.term)
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2016-09-09 15:38:00 -04:00
|
|
|
authConfig := command.ResolveAuthConfig(ctx, dockerCli, indexInfo)
|
|
|
|
requestPrivilege := command.RegistryAuthenticationPrivilegedFunc(dockerCli, indexInfo, "search")
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
encodedAuth, err := command.EncodeAuthToBase64(authConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
searchOptions := types.ImageSearchOptions{
|
2016-09-08 13:11:39 -04:00
|
|
|
RegistryAuth: encodedAuth,
|
|
|
|
PrivilegeFunc: requestPrivilege,
|
2017-05-15 08:45:19 -04:00
|
|
|
Filters: options.filter.Value(),
|
|
|
|
Limit: options.limit,
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
clnt := dockerCli.Client()
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
unorderedResults, err := clnt.ImageSearch(ctx, options.term, searchOptions)
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
results := searchResultsByStars(unorderedResults)
|
|
|
|
sort.Sort(results)
|
2017-08-15 06:58:49 -04:00
|
|
|
searchCtx := formatter.Context{
|
|
|
|
Output: dockerCli.Out(),
|
|
|
|
Format: formatter.NewSearchFormat(options.format),
|
|
|
|
Trunc: !options.noTrunc,
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2017-08-15 06:58:49 -04:00
|
|
|
return formatter.SearchWrite(searchCtx, results, options.automated, int(options.stars))
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-28 12:21:47 -05:00
|
|
|
// searchResultsByStars sorts search results in descending order by number of stars.
|
2016-09-08 13:11:39 -04:00
|
|
|
type searchResultsByStars []registrytypes.SearchResult
|
|
|
|
|
|
|
|
func (r searchResultsByStars) Len() int { return len(r) }
|
|
|
|
func (r searchResultsByStars) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
|
|
|
|
func (r searchResultsByStars) Less(i, j int) bool { return r[j].StarCount < r[i].StarCount }
|