2015-03-24 23:57:23 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
2015-04-03 13:29:30 -04:00
|
|
|
"sort"
|
2015-03-24 23:57:23 -04:00
|
|
|
"strings"
|
|
|
|
"text/tabwriter"
|
|
|
|
|
2016-03-16 15:19:22 -04:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2015-05-05 00:18:28 -04:00
|
|
|
Cli "github.com/docker/docker/cli"
|
2016-05-20 07:41:28 -04:00
|
|
|
"github.com/docker/docker/opts"
|
2015-03-24 23:57:23 -04:00
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
2015-03-29 17:17:23 -04:00
|
|
|
"github.com/docker/docker/pkg/stringutils"
|
2015-01-12 14:56:01 -05:00
|
|
|
"github.com/docker/docker/registry"
|
2016-01-04 19:05:26 -05:00
|
|
|
"github.com/docker/engine-api/types"
|
2016-05-20 07:41:28 -04:00
|
|
|
"github.com/docker/engine-api/types/filters"
|
2016-01-04 19:05:26 -05:00
|
|
|
registrytypes "github.com/docker/engine-api/types/registry"
|
2015-03-24 23:57:23 -04:00
|
|
|
)
|
|
|
|
|
2015-03-25 13:34:41 -04:00
|
|
|
// CmdSearch searches the Docker Hub for images.
|
|
|
|
//
|
|
|
|
// Usage: docker search [OPTIONS] TERM
|
2015-03-24 23:57:23 -04:00
|
|
|
func (cli *DockerCli) CmdSearch(args ...string) error {
|
2016-05-20 07:41:28 -04:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
|
|
|
|
filterArgs = filters.NewArgs()
|
|
|
|
|
|
|
|
flFilter = opts.NewListOpts(nil)
|
|
|
|
)
|
|
|
|
|
2015-10-08 08:46:21 -04:00
|
|
|
cmd := Cli.Subcmd("search", []string{"TERM"}, Cli.DockerCommands["search"].Description, true)
|
2015-11-09 09:37:24 -05:00
|
|
|
noTrunc := cmd.Bool([]string{"-no-trunc"}, false, "Don't truncate output")
|
2016-05-20 07:41:28 -04:00
|
|
|
cmd.Var(&flFilter, []string{"f", "-filter"}, "Filter output based on conditions provided")
|
2016-06-01 16:38:14 -04:00
|
|
|
flLimit := cmd.Int([]string{"-limit"}, registry.DefaultSearchLimit, "Max number of search results")
|
2016-05-20 07:41:28 -04:00
|
|
|
|
|
|
|
// Deprecated since Docker 1.12 in favor of "--filter"
|
|
|
|
automated := cmd.Bool([]string{"#-automated"}, false, "Only show automated builds - DEPRECATED")
|
|
|
|
stars := cmd.Uint([]string{"s", "#-stars"}, 0, "Only displays with at least x stars - DEPRECATED")
|
|
|
|
|
2015-03-24 23:57:23 -04:00
|
|
|
cmd.Require(flag.Exact, 1)
|
|
|
|
|
2015-03-28 21:22:46 -04:00
|
|
|
cmd.ParseFlags(args, true)
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2016-05-20 07:41:28 -04:00
|
|
|
for _, f := range flFilter.GetAll() {
|
|
|
|
if filterArgs, err = filters.ParseFlag(f, filterArgs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-12 14:56:01 -05:00
|
|
|
name := cmd.Arg(0)
|
2015-03-24 23:57:23 -04:00
|
|
|
v := url.Values{}
|
2015-01-12 14:56:01 -05:00
|
|
|
v.Set("term", name)
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2015-11-18 17:20:54 -05:00
|
|
|
indexInfo, err := registry.ParseSearchIndexInfo(name)
|
2015-01-12 14:56:01 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2016-05-21 09:57:57 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
authConfig := cli.resolveAuthConfig(ctx, indexInfo)
|
2015-12-06 19:29:15 -05:00
|
|
|
requestPrivilege := cli.registryAuthenticationPrivilegedFunc(indexInfo, "search")
|
|
|
|
|
2015-12-11 21:14:52 -05:00
|
|
|
encodedAuth, err := encodeAuthToBase64(authConfig)
|
2015-03-24 23:57:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-01-12 14:56:01 -05:00
|
|
|
|
2015-12-06 19:29:15 -05:00
|
|
|
options := types.ImageSearchOptions{
|
2016-04-13 04:33:46 -04:00
|
|
|
RegistryAuth: encodedAuth,
|
|
|
|
PrivilegeFunc: requestPrivilege,
|
2016-05-20 07:41:28 -04:00
|
|
|
Filters: filterArgs,
|
2016-06-01 16:38:14 -04:00
|
|
|
Limit: *flLimit,
|
2015-12-06 19:29:15 -05:00
|
|
|
}
|
2015-07-03 02:19:23 -04:00
|
|
|
|
2016-05-21 09:57:57 -04:00
|
|
|
unorderedResults, err := cli.client.ImageSearch(ctx, name, options)
|
2015-12-06 19:29:15 -05:00
|
|
|
if err != nil {
|
2015-03-24 23:57:23 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-04-03 13:29:30 -04:00
|
|
|
|
2015-12-06 19:29:15 -05:00
|
|
|
results := searchResultsByStars(unorderedResults)
|
|
|
|
sort.Sort(results)
|
2015-04-03 13:29:30 -04:00
|
|
|
|
2015-03-24 23:57:23 -04:00
|
|
|
w := tabwriter.NewWriter(cli.out, 10, 1, 3, ' ', 0)
|
|
|
|
fmt.Fprintf(w, "NAME\tDESCRIPTION\tSTARS\tOFFICIAL\tAUTOMATED\n")
|
2015-04-03 13:29:30 -04:00
|
|
|
for _, res := range results {
|
2016-05-20 07:41:28 -04:00
|
|
|
// --automated and -s, --stars are deprecated since Docker 1.12
|
2015-11-09 09:37:24 -05:00
|
|
|
if (*automated && !res.IsAutomated) || (int(*stars) > res.StarCount) {
|
2015-03-24 23:57:23 -04:00
|
|
|
continue
|
|
|
|
}
|
2015-04-03 13:29:30 -04:00
|
|
|
desc := strings.Replace(res.Description, "\n", " ", -1)
|
2015-03-24 23:57:23 -04:00
|
|
|
desc = strings.Replace(desc, "\r", " ", -1)
|
|
|
|
if !*noTrunc && len(desc) > 45 {
|
2015-03-29 17:17:23 -04:00
|
|
|
desc = stringutils.Truncate(desc, 42) + "..."
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
2015-04-03 13:29:30 -04:00
|
|
|
fmt.Fprintf(w, "%s\t%s\t%d\t", res.Name, desc, res.StarCount)
|
|
|
|
if res.IsOfficial {
|
2015-03-24 23:57:23 -04:00
|
|
|
fmt.Fprint(w, "[OK]")
|
|
|
|
|
|
|
|
}
|
|
|
|
fmt.Fprint(w, "\t")
|
2016-05-05 17:01:10 -04:00
|
|
|
if res.IsAutomated {
|
2015-03-24 23:57:23 -04:00
|
|
|
fmt.Fprint(w, "[OK]")
|
|
|
|
}
|
|
|
|
fmt.Fprint(w, "\n")
|
|
|
|
}
|
|
|
|
w.Flush()
|
|
|
|
return nil
|
|
|
|
}
|
2015-12-06 19:29:15 -05:00
|
|
|
|
|
|
|
// SearchResultsByStars sorts search results in descending order by number of stars.
|
2015-12-15 11:44:20 -05:00
|
|
|
type searchResultsByStars []registrytypes.SearchResult
|
2015-12-06 19:29:15 -05:00
|
|
|
|
|
|
|
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 }
|