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"
|
|
|
|
|
2015-12-06 19:29:15 -05:00
|
|
|
"github.com/docker/docker/api/types"
|
2015-05-05 00:18:28 -04:00
|
|
|
Cli "github.com/docker/docker/cli"
|
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"
|
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 {
|
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")
|
2015-03-24 23:57:23 -04:00
|
|
|
automated := cmd.Bool([]string{"-automated"}, false, "Only show automated builds")
|
2015-11-09 09:37:24 -05:00
|
|
|
stars := cmd.Uint([]string{"s", "-stars"}, 0, "Only displays with at least x stars")
|
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
|
|
|
|
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
|
|
|
|
2015-12-06 19:29:15 -05:00
|
|
|
authConfig := registry.ResolveAuthConfig(cli.configFile, indexInfo)
|
|
|
|
requestPrivilege := cli.registryAuthenticationPrivilegedFunc(indexInfo, "search")
|
|
|
|
|
|
|
|
encodedAuth, err := authConfig.EncodeToBase64()
|
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{
|
|
|
|
Term: name,
|
|
|
|
RegistryAuth: encodedAuth,
|
|
|
|
}
|
2015-07-03 02:19:23 -04:00
|
|
|
|
2015-12-06 19:29:15 -05:00
|
|
|
unorderedResults, err := cli.client.ImageSearch(options, requestPrivilege)
|
|
|
|
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 {
|
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")
|
2015-04-03 13:29:30 -04:00
|
|
|
if res.IsAutomated || res.IsTrusted {
|
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.
|
|
|
|
type searchResultsByStars []registry.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 }
|