search.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package image
  2. import (
  3. "fmt"
  4. "sort"
  5. "strings"
  6. "text/tabwriter"
  7. "golang.org/x/net/context"
  8. "github.com/docker/docker/api/client"
  9. "github.com/docker/docker/cli"
  10. "github.com/docker/docker/pkg/stringutils"
  11. "github.com/docker/docker/registry"
  12. "github.com/docker/engine-api/types"
  13. "github.com/docker/engine-api/types/filters"
  14. registrytypes "github.com/docker/engine-api/types/registry"
  15. "github.com/spf13/cobra"
  16. )
  17. type searchOptions struct {
  18. term string
  19. noTrunc bool
  20. limit int
  21. filter []string
  22. // Deprecated
  23. stars uint
  24. automated bool
  25. }
  26. // NewSearchCommand create a new `docker search` command
  27. func NewSearchCommand(dockerCli *client.DockerCli) *cobra.Command {
  28. var opts searchOptions
  29. cmd := &cobra.Command{
  30. Use: "search [OPTIONS] TERM",
  31. Short: "Search the Docker Hub for images",
  32. Args: cli.ExactArgs(1),
  33. RunE: func(cmd *cobra.Command, args []string) error {
  34. opts.term = args[0]
  35. return runSearch(dockerCli, opts)
  36. },
  37. }
  38. flags := cmd.Flags()
  39. flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Don't truncate output")
  40. flags.StringSliceVarP(&opts.filter, "filter", "f", []string{}, "Filter output based on conditions provided")
  41. flags.IntVar(&opts.limit, "limit", registry.DefaultSearchLimit, "Max number of search results")
  42. flags.BoolVar(&opts.automated, "automated", false, "Only show automated builds")
  43. flags.UintVarP(&opts.stars, "stars", "s", 0, "Only displays with at least x stars")
  44. flags.MarkDeprecated("automated", "use --filter=automated=true instead")
  45. flags.MarkDeprecated("stars", "use --filter=stars=3 instead")
  46. return cmd
  47. }
  48. func runSearch(dockerCli *client.DockerCli, opts searchOptions) error {
  49. indexInfo, err := registry.ParseSearchIndexInfo(opts.term)
  50. if err != nil {
  51. return err
  52. }
  53. ctx := context.Background()
  54. authConfig := dockerCli.ResolveAuthConfig(ctx, indexInfo)
  55. requestPrivilege := dockerCli.RegistryAuthenticationPrivilegedFunc(indexInfo, "search")
  56. encodedAuth, err := client.EncodeAuthToBase64(authConfig)
  57. if err != nil {
  58. return err
  59. }
  60. searchFilters := filters.NewArgs()
  61. for _, f := range opts.filter {
  62. var err error
  63. searchFilters, err = filters.ParseFlag(f, searchFilters)
  64. if err != nil {
  65. return err
  66. }
  67. }
  68. options := types.ImageSearchOptions{
  69. RegistryAuth: encodedAuth,
  70. PrivilegeFunc: requestPrivilege,
  71. Filters: searchFilters,
  72. Limit: opts.limit,
  73. }
  74. clnt := dockerCli.Client()
  75. unorderedResults, err := clnt.ImageSearch(ctx, opts.term, options)
  76. if err != nil {
  77. return err
  78. }
  79. results := searchResultsByStars(unorderedResults)
  80. sort.Sort(results)
  81. w := tabwriter.NewWriter(dockerCli.Out(), 10, 1, 3, ' ', 0)
  82. fmt.Fprintf(w, "NAME\tDESCRIPTION\tSTARS\tOFFICIAL\tAUTOMATED\n")
  83. for _, res := range results {
  84. // --automated and -s, --stars are deprecated since Docker 1.12
  85. if (opts.automated && !res.IsAutomated) || (int(opts.stars) > res.StarCount) {
  86. continue
  87. }
  88. desc := strings.Replace(res.Description, "\n", " ", -1)
  89. desc = strings.Replace(desc, "\r", " ", -1)
  90. if !opts.noTrunc && len(desc) > 45 {
  91. desc = stringutils.Truncate(desc, 42) + "..."
  92. }
  93. fmt.Fprintf(w, "%s\t%s\t%d\t", res.Name, desc, res.StarCount)
  94. if res.IsOfficial {
  95. fmt.Fprint(w, "[OK]")
  96. }
  97. fmt.Fprint(w, "\t")
  98. if res.IsAutomated {
  99. fmt.Fprint(w, "[OK]")
  100. }
  101. fmt.Fprint(w, "\n")
  102. }
  103. w.Flush()
  104. return nil
  105. }
  106. // SearchResultsByStars sorts search results in descending order by number of stars.
  107. type searchResultsByStars []registrytypes.SearchResult
  108. func (r searchResultsByStars) Len() int { return len(r) }
  109. func (r searchResultsByStars) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
  110. func (r searchResultsByStars) Less(i, j int) bool { return r[j].StarCount < r[i].StarCount }