search.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package client
  2. import (
  3. "fmt"
  4. "net/url"
  5. "sort"
  6. "strings"
  7. "text/tabwriter"
  8. "golang.org/x/net/context"
  9. Cli "github.com/docker/docker/cli"
  10. "github.com/docker/docker/opts"
  11. flag "github.com/docker/docker/pkg/mflag"
  12. "github.com/docker/docker/pkg/stringutils"
  13. "github.com/docker/docker/registry"
  14. "github.com/docker/engine-api/types"
  15. "github.com/docker/engine-api/types/filters"
  16. registrytypes "github.com/docker/engine-api/types/registry"
  17. )
  18. // CmdSearch searches the Docker Hub for images.
  19. //
  20. // Usage: docker search [OPTIONS] TERM
  21. func (cli *DockerCli) CmdSearch(args ...string) error {
  22. var (
  23. err error
  24. filterArgs = filters.NewArgs()
  25. flFilter = opts.NewListOpts(nil)
  26. )
  27. cmd := Cli.Subcmd("search", []string{"TERM"}, Cli.DockerCommands["search"].Description, true)
  28. noTrunc := cmd.Bool([]string{"-no-trunc"}, false, "Don't truncate output")
  29. cmd.Var(&flFilter, []string{"f", "-filter"}, "Filter output based on conditions provided")
  30. // Deprecated since Docker 1.12 in favor of "--filter"
  31. automated := cmd.Bool([]string{"#-automated"}, false, "Only show automated builds - DEPRECATED")
  32. stars := cmd.Uint([]string{"s", "#-stars"}, 0, "Only displays with at least x stars - DEPRECATED")
  33. cmd.Require(flag.Exact, 1)
  34. cmd.ParseFlags(args, true)
  35. for _, f := range flFilter.GetAll() {
  36. if filterArgs, err = filters.ParseFlag(f, filterArgs); err != nil {
  37. return err
  38. }
  39. }
  40. name := cmd.Arg(0)
  41. v := url.Values{}
  42. v.Set("term", name)
  43. indexInfo, err := registry.ParseSearchIndexInfo(name)
  44. if err != nil {
  45. return err
  46. }
  47. authConfig := cli.resolveAuthConfig(indexInfo)
  48. requestPrivilege := cli.registryAuthenticationPrivilegedFunc(indexInfo, "search")
  49. encodedAuth, err := encodeAuthToBase64(authConfig)
  50. if err != nil {
  51. return err
  52. }
  53. options := types.ImageSearchOptions{
  54. RegistryAuth: encodedAuth,
  55. PrivilegeFunc: requestPrivilege,
  56. Filters: filterArgs,
  57. }
  58. unorderedResults, err := cli.client.ImageSearch(context.Background(), name, options)
  59. if err != nil {
  60. return err
  61. }
  62. results := searchResultsByStars(unorderedResults)
  63. sort.Sort(results)
  64. w := tabwriter.NewWriter(cli.out, 10, 1, 3, ' ', 0)
  65. fmt.Fprintf(w, "NAME\tDESCRIPTION\tSTARS\tOFFICIAL\tAUTOMATED\n")
  66. for _, res := range results {
  67. // --automated and -s, --stars are deprecated since Docker 1.12
  68. if (*automated && !res.IsAutomated) || (int(*stars) > res.StarCount) {
  69. continue
  70. }
  71. desc := strings.Replace(res.Description, "\n", " ", -1)
  72. desc = strings.Replace(desc, "\r", " ", -1)
  73. if !*noTrunc && len(desc) > 45 {
  74. desc = stringutils.Truncate(desc, 42) + "..."
  75. }
  76. fmt.Fprintf(w, "%s\t%s\t%d\t", res.Name, desc, res.StarCount)
  77. if res.IsOfficial {
  78. fmt.Fprint(w, "[OK]")
  79. }
  80. fmt.Fprint(w, "\t")
  81. if res.IsAutomated {
  82. fmt.Fprint(w, "[OK]")
  83. }
  84. fmt.Fprint(w, "\n")
  85. }
  86. w.Flush()
  87. return nil
  88. }
  89. // SearchResultsByStars sorts search results in descending order by number of stars.
  90. type searchResultsByStars []registrytypes.SearchResult
  91. func (r searchResultsByStars) Len() int { return len(r) }
  92. func (r searchResultsByStars) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
  93. func (r searchResultsByStars) Less(i, j int) bool { return r[j].StarCount < r[i].StarCount }