search.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package registry
  2. import (
  3. "fmt"
  4. "sort"
  5. "strings"
  6. "text/tabwriter"
  7. "golang.org/x/net/context"
  8. "github.com/docker/docker/api/types"
  9. registrytypes "github.com/docker/docker/api/types/registry"
  10. "github.com/docker/docker/cli"
  11. "github.com/docker/docker/cli/command"
  12. "github.com/docker/docker/opts"
  13. "github.com/docker/docker/pkg/stringutils"
  14. "github.com/docker/docker/registry"
  15. "github.com/spf13/cobra"
  16. )
  17. type searchOptions struct {
  18. term string
  19. noTrunc bool
  20. limit int
  21. filter opts.FilterOpt
  22. // Deprecated
  23. stars uint
  24. automated bool
  25. }
  26. // NewSearchCommand creates a new `docker search` command
  27. func NewSearchCommand(dockerCli *command.DockerCli) *cobra.Command {
  28. opts := searchOptions{filter: opts.NewFilterOpt()}
  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.VarP(&opts.filter, "filter", "f", "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 *command.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 := command.ResolveAuthConfig(ctx, dockerCli, indexInfo)
  55. requestPrivilege := command.RegistryAuthenticationPrivilegedFunc(dockerCli, indexInfo, "search")
  56. encodedAuth, err := command.EncodeAuthToBase64(authConfig)
  57. if err != nil {
  58. return err
  59. }
  60. options := types.ImageSearchOptions{
  61. RegistryAuth: encodedAuth,
  62. PrivilegeFunc: requestPrivilege,
  63. Filters: opts.filter.Value(),
  64. Limit: opts.limit,
  65. }
  66. clnt := dockerCli.Client()
  67. unorderedResults, err := clnt.ImageSearch(ctx, opts.term, options)
  68. if err != nil {
  69. return err
  70. }
  71. results := searchResultsByStars(unorderedResults)
  72. sort.Sort(results)
  73. w := tabwriter.NewWriter(dockerCli.Out(), 10, 1, 3, ' ', 0)
  74. fmt.Fprintf(w, "NAME\tDESCRIPTION\tSTARS\tOFFICIAL\tAUTOMATED\n")
  75. for _, res := range results {
  76. // --automated and -s, --stars are deprecated since Docker 1.12
  77. if (opts.automated && !res.IsAutomated) || (int(opts.stars) > res.StarCount) {
  78. continue
  79. }
  80. desc := strings.Replace(res.Description, "\n", " ", -1)
  81. desc = strings.Replace(desc, "\r", " ", -1)
  82. if !opts.noTrunc {
  83. desc = stringutils.Ellipsis(desc, 45)
  84. }
  85. fmt.Fprintf(w, "%s\t%s\t%d\t", res.Name, desc, res.StarCount)
  86. if res.IsOfficial {
  87. fmt.Fprint(w, "[OK]")
  88. }
  89. fmt.Fprint(w, "\t")
  90. if res.IsAutomated {
  91. fmt.Fprint(w, "[OK]")
  92. }
  93. fmt.Fprint(w, "\n")
  94. }
  95. w.Flush()
  96. return nil
  97. }
  98. // searchResultsByStars sorts search results in descending order by number of stars.
  99. type searchResultsByStars []registrytypes.SearchResult
  100. func (r searchResultsByStars) Len() int { return len(r) }
  101. func (r searchResultsByStars) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
  102. func (r searchResultsByStars) Less(i, j int) bool { return r[j].StarCount < r[i].StarCount }