2015-03-24 23:57:23 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2015-04-03 13:29:30 -04:00
|
|
|
"encoding/json"
|
2015-03-24 23:57:23 -04:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
2015-04-03 13:29:30 -04:00
|
|
|
"sort"
|
2015-03-24 23:57:23 -04:00
|
|
|
"strings"
|
|
|
|
"text/tabwriter"
|
|
|
|
|
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-01-12 14:56:01 -05:00
|
|
|
"github.com/docker/docker/pkg/parsers"
|
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-04-22 02:45:18 -04:00
|
|
|
// ByStars sorts search results in ascending order by number of stars.
|
2015-04-03 13:29:30 -04:00
|
|
|
type ByStars []registry.SearchResult
|
|
|
|
|
|
|
|
func (r ByStars) Len() int { return len(r) }
|
|
|
|
func (r ByStars) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
|
|
|
|
func (r ByStars) Less(i, j int) bool { return r[i].StarCount < r[j].StarCount }
|
|
|
|
|
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-03-24 23:57:23 -04:00
|
|
|
noTrunc := cmd.Bool([]string{"#notrunc", "-no-trunc"}, false, "Don't truncate output")
|
|
|
|
trusted := cmd.Bool([]string{"#t", "#trusted", "#-trusted"}, false, "Only show trusted builds")
|
|
|
|
automated := cmd.Bool([]string{"-automated"}, false, "Only show automated builds")
|
2015-04-02 20:55:08 -04:00
|
|
|
stars := cmd.Uint([]string{"s", "#stars", "-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-01-12 14:56:01 -05:00
|
|
|
// Resolve the Repository name from fqn to hostname + name
|
|
|
|
taglessRemote, _ := parsers.ParseRepositoryTag(name)
|
2015-09-22 07:44:40 -04:00
|
|
|
|
|
|
|
indexInfo, err := registry.ParseIndexInfo(taglessRemote)
|
2015-01-12 14:56:01 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2015-09-22 07:44:40 -04:00
|
|
|
rdr, _, err := cli.clientRequestAttemptLogin("GET", "/images/search?"+v.Encode(), nil, nil, indexInfo, "search")
|
2015-03-24 23:57:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-01-12 14:56:01 -05:00
|
|
|
|
2015-07-03 02:19:23 -04:00
|
|
|
defer rdr.Close()
|
|
|
|
|
2015-04-03 13:29:30 -04:00
|
|
|
results := ByStars{}
|
2015-04-26 12:50:25 -04:00
|
|
|
if err := json.NewDecoder(rdr).Decode(&results); err != nil {
|
2015-03-24 23:57:23 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-04-03 13:29:30 -04:00
|
|
|
|
|
|
|
sort.Sort(sort.Reverse(results))
|
|
|
|
|
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-09-23 02:03:58 -04:00
|
|
|
if (*automated && !res.IsAutomated) || (int(*stars) > res.StarCount) || (*trusted && !res.IsTrusted) {
|
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
|
|
|
|
}
|