search.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package client
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/url"
  6. "sort"
  7. "strings"
  8. "text/tabwriter"
  9. Cli "github.com/docker/docker/cli"
  10. flag "github.com/docker/docker/pkg/mflag"
  11. "github.com/docker/docker/pkg/parsers"
  12. "github.com/docker/docker/pkg/stringutils"
  13. "github.com/docker/docker/registry"
  14. )
  15. // ByStars sorts search results in ascending order by number of stars.
  16. type ByStars []registry.SearchResult
  17. func (r ByStars) Len() int { return len(r) }
  18. func (r ByStars) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
  19. func (r ByStars) Less(i, j int) bool { return r[i].StarCount < r[j].StarCount }
  20. // CmdSearch searches the Docker Hub for images.
  21. //
  22. // Usage: docker search [OPTIONS] TERM
  23. func (cli *DockerCli) CmdSearch(args ...string) error {
  24. cmd := Cli.Subcmd("search", []string{"TERM"}, "Search the Docker Hub for images", true)
  25. noTrunc := cmd.Bool([]string{"#notrunc", "-no-trunc"}, false, "Don't truncate output")
  26. trusted := cmd.Bool([]string{"#t", "#trusted", "#-trusted"}, false, "Only show trusted builds")
  27. automated := cmd.Bool([]string{"-automated"}, false, "Only show automated builds")
  28. stars := cmd.Uint([]string{"s", "#stars", "-stars"}, 0, "Only displays with at least x stars")
  29. cmd.Require(flag.Exact, 1)
  30. cmd.ParseFlags(args, true)
  31. name := cmd.Arg(0)
  32. v := url.Values{}
  33. v.Set("term", name)
  34. // Resolve the Repository name from fqn to hostname + name
  35. taglessRemote, _ := parsers.ParseRepositoryTag(name)
  36. indexInfo, err := registry.ParseIndexInfo(taglessRemote)
  37. if err != nil {
  38. return err
  39. }
  40. rdr, _, err := cli.clientRequestAttemptLogin("GET", "/images/search?"+v.Encode(), nil, nil, indexInfo, "search")
  41. if err != nil {
  42. return err
  43. }
  44. defer rdr.Close()
  45. results := ByStars{}
  46. if err := json.NewDecoder(rdr).Decode(&results); err != nil {
  47. return err
  48. }
  49. sort.Sort(sort.Reverse(results))
  50. w := tabwriter.NewWriter(cli.out, 10, 1, 3, ' ', 0)
  51. fmt.Fprintf(w, "NAME\tDESCRIPTION\tSTARS\tOFFICIAL\tAUTOMATED\n")
  52. for _, res := range results {
  53. if (*automated && !res.IsAutomated) || (int(*stars) > res.StarCount) || (*trusted && !res.IsTrusted) {
  54. continue
  55. }
  56. desc := strings.Replace(res.Description, "\n", " ", -1)
  57. desc = strings.Replace(desc, "\r", " ", -1)
  58. if !*noTrunc && len(desc) > 45 {
  59. desc = stringutils.Truncate(desc, 42) + "..."
  60. }
  61. fmt.Fprintf(w, "%s\t%s\t%d\t", res.Name, desc, res.StarCount)
  62. if res.IsOfficial {
  63. fmt.Fprint(w, "[OK]")
  64. }
  65. fmt.Fprint(w, "\t")
  66. if res.IsAutomated || res.IsTrusted {
  67. fmt.Fprint(w, "[OK]")
  68. }
  69. fmt.Fprint(w, "\n")
  70. }
  71. w.Flush()
  72. return nil
  73. }