search.go 2.6 KB

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