image_search.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. package lib
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/url"
  6. "github.com/docker/docker/api/types"
  7. "github.com/docker/docker/api/types/registry"
  8. )
  9. // ImageSearch makes the docker host to search by a term in a remote registry.
  10. // The list of results is not sorted in any fashion.
  11. func (cli *Client) ImageSearch(options types.ImageSearchOptions, privilegeFunc RequestPrivilegeFunc) ([]registry.SearchResult, error) {
  12. var results []registry.SearchResult
  13. query := url.Values{}
  14. query.Set("term", options.Term)
  15. resp, err := cli.tryImageSearch(query, options.RegistryAuth)
  16. if resp.statusCode == http.StatusUnauthorized {
  17. newAuthHeader, privilegeErr := privilegeFunc()
  18. if privilegeErr != nil {
  19. return results, privilegeErr
  20. }
  21. resp, err = cli.tryImageSearch(query, newAuthHeader)
  22. }
  23. if err != nil {
  24. return results, err
  25. }
  26. defer ensureReaderClosed(resp)
  27. err = json.NewDecoder(resp.body).Decode(&results)
  28. return results, err
  29. }
  30. func (cli *Client) tryImageSearch(query url.Values, registryAuth string) (*serverResponse, error) {
  31. headers := map[string][]string{"X-Registry-Auth": {registryAuth}}
  32. return cli.get("/images/search", query, headers)
  33. }