2016-08-29 09:59:41 -04:00
|
|
|
package registry
|
2016-06-03 13:50:01 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"text/tabwriter"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
registrytypes "github.com/docker/docker/api/types/registry"
|
2016-06-03 13:50:01 -04:00
|
|
|
"github.com/docker/docker/cli"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/cli/command"
|
2016-09-13 14:53:11 -04:00
|
|
|
"github.com/docker/docker/opts"
|
2016-06-03 13:50:01 -04:00
|
|
|
"github.com/docker/docker/pkg/stringutils"
|
|
|
|
"github.com/docker/docker/registry"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type searchOptions struct {
|
|
|
|
term string
|
|
|
|
noTrunc bool
|
|
|
|
limit int
|
2016-09-13 14:53:11 -04:00
|
|
|
filter opts.FilterOpt
|
2016-06-03 13:50:01 -04:00
|
|
|
|
|
|
|
// Deprecated
|
|
|
|
stars uint
|
|
|
|
automated bool
|
|
|
|
}
|
|
|
|
|
2016-08-25 09:29:59 -04:00
|
|
|
// NewSearchCommand creates a new `docker search` command
|
2016-09-08 13:11:39 -04:00
|
|
|
func NewSearchCommand(dockerCli *command.DockerCli) *cobra.Command {
|
2016-09-13 14:53:11 -04:00
|
|
|
opts := searchOptions{filter: opts.NewFilterOpt()}
|
2016-06-03 13:50:01 -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 {
|
|
|
|
opts.term = args[0]
|
|
|
|
return runSearch(dockerCli, opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
|
|
|
|
flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Don't truncate output")
|
2016-09-13 14:53:11 -04:00
|
|
|
flags.VarP(&opts.filter, "filter", "f", "Filter output based on conditions provided")
|
2016-06-03 13:50:01 -04:00
|
|
|
flags.IntVar(&opts.limit, "limit", registry.DefaultSearchLimit, "Max number of search results")
|
|
|
|
|
|
|
|
flags.BoolVar(&opts.automated, "automated", false, "Only show automated builds")
|
|
|
|
flags.UintVarP(&opts.stars, "stars", "s", 0, "Only displays with at least x stars")
|
|
|
|
|
2016-06-10 06:04:29 -04:00
|
|
|
flags.MarkDeprecated("automated", "use --filter=automated=true instead")
|
|
|
|
flags.MarkDeprecated("stars", "use --filter=stars=3 instead")
|
2016-06-03 13:50:01 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func runSearch(dockerCli *command.DockerCli, opts searchOptions) error {
|
2016-06-03 13:50:01 -04:00
|
|
|
indexInfo, err := registry.ParseSearchIndexInfo(opts.term)
|
|
|
|
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-06-03 13:50:01 -04:00
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
encodedAuth, err := command.EncodeAuthToBase64(authConfig)
|
2016-06-03 13:50:01 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
options := types.ImageSearchOptions{
|
|
|
|
RegistryAuth: encodedAuth,
|
|
|
|
PrivilegeFunc: requestPrivilege,
|
2016-09-13 14:53:11 -04:00
|
|
|
Filters: opts.filter.Value(),
|
2016-06-03 13:50:01 -04:00
|
|
|
Limit: opts.limit,
|
|
|
|
}
|
|
|
|
|
|
|
|
clnt := dockerCli.Client()
|
|
|
|
|
|
|
|
unorderedResults, err := clnt.ImageSearch(ctx, opts.term, options)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
results := searchResultsByStars(unorderedResults)
|
|
|
|
sort.Sort(results)
|
|
|
|
|
|
|
|
w := tabwriter.NewWriter(dockerCli.Out(), 10, 1, 3, ' ', 0)
|
|
|
|
fmt.Fprintf(w, "NAME\tDESCRIPTION\tSTARS\tOFFICIAL\tAUTOMATED\n")
|
|
|
|
for _, res := range results {
|
|
|
|
// --automated and -s, --stars are deprecated since Docker 1.12
|
|
|
|
if (opts.automated && !res.IsAutomated) || (int(opts.stars) > res.StarCount) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
desc := strings.Replace(res.Description, "\n", " ", -1)
|
|
|
|
desc = strings.Replace(desc, "\r", " ", -1)
|
2016-08-10 09:54:14 -04:00
|
|
|
if !opts.noTrunc {
|
|
|
|
desc = stringutils.Ellipsis(desc, 45)
|
2016-06-03 13:50:01 -04:00
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "%s\t%s\t%d\t", res.Name, desc, res.StarCount)
|
|
|
|
if res.IsOfficial {
|
|
|
|
fmt.Fprint(w, "[OK]")
|
|
|
|
|
|
|
|
}
|
|
|
|
fmt.Fprint(w, "\t")
|
|
|
|
if res.IsAutomated {
|
|
|
|
fmt.Fprint(w, "[OK]")
|
|
|
|
}
|
|
|
|
fmt.Fprint(w, "\n")
|
|
|
|
}
|
|
|
|
w.Flush()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SearchResultsByStars sorts search results in descending order by number of stars.
|
|
|
|
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 }
|