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"
|
|
|
|
|
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
2015-01-12 14:56:01 -05:00
|
|
|
"github.com/docker/docker/pkg/parsers"
|
|
|
|
"github.com/docker/docker/registry"
|
2015-03-24 23:57:23 -04:00
|
|
|
"github.com/docker/docker/utils"
|
|
|
|
)
|
|
|
|
|
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 {
|
|
|
|
cmd := cli.Subcmd("search", "TERM", "Search the Docker Hub for images", true)
|
|
|
|
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)
|
|
|
|
repoInfo, err := registry.ParseRepositoryInfo(taglessRemote)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2015-01-12 14:56:01 -05:00
|
|
|
cli.LoadConfigFile()
|
|
|
|
|
2015-04-03 13:29:30 -04:00
|
|
|
rdr, _, err := cli.clientRequestAttemptLogin("GET", "/images/search?"+v.Encode(), nil, nil, repoInfo.Index, "search")
|
2015-03-24 23:57:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-01-12 14:56:01 -05:00
|
|
|
|
2015-04-03 13:29:30 -04:00
|
|
|
results := ByStars{}
|
|
|
|
err = json.NewDecoder(rdr).Decode(&results)
|
|
|
|
if 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 {
|
|
|
|
if ((*automated || *trusted) && (!res.IsTrusted && !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 {
|
|
|
|
desc = utils.Trunc(desc, 42) + "..."
|
|
|
|
}
|
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
|
|
|
|
}
|