2018-02-05 21:05:59 +00:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2016-09-06 18:46:37 +00:00
|
|
|
|
|
|
|
import (
|
2018-04-19 22:30:59 +00:00
|
|
|
"context"
|
2016-09-06 18:46:37 +00:00
|
|
|
"encoding/json"
|
2023-07-10 14:44:59 +00:00
|
|
|
"net/http"
|
2016-09-06 18:46:37 +00:00
|
|
|
"net/url"
|
2022-03-15 13:53:46 +00:00
|
|
|
"strconv"
|
2016-09-06 18:46:37 +00:00
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
|
|
|
"github.com/docker/docker/api/types/registry"
|
2019-02-09 18:19:22 +00:00
|
|
|
"github.com/docker/docker/errdefs"
|
2016-09-06 18:46:37 +00:00
|
|
|
)
|
|
|
|
|
2021-02-16 15:07:44 +00:00
|
|
|
// ImageSearch makes the docker host search by a term in a remote registry.
|
2016-09-06 18:46:37 +00:00
|
|
|
// The list of results is not sorted in any fashion.
|
|
|
|
func (cli *Client) ImageSearch(ctx context.Context, term string, options types.ImageSearchOptions) ([]registry.SearchResult, error) {
|
|
|
|
var results []registry.SearchResult
|
|
|
|
query := url.Values{}
|
|
|
|
query.Set("term", term)
|
2022-03-15 13:53:46 +00:00
|
|
|
if options.Limit > 0 {
|
|
|
|
query.Set("limit", strconv.Itoa(options.Limit))
|
|
|
|
}
|
2016-09-06 18:46:37 +00:00
|
|
|
|
|
|
|
if options.Filters.Len() > 0 {
|
2017-09-26 11:59:45 +00:00
|
|
|
filterJSON, err := filters.ToJSON(options.Filters)
|
2016-09-06 18:46:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return results, err
|
|
|
|
}
|
|
|
|
query.Set("filters", filterJSON)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := cli.tryImageSearch(ctx, query, options.RegistryAuth)
|
2019-02-11 12:26:12 +00:00
|
|
|
defer ensureReaderClosed(resp)
|
2019-02-09 18:19:22 +00:00
|
|
|
if errdefs.IsUnauthorized(err) && options.PrivilegeFunc != nil {
|
2016-09-06 18:46:37 +00:00
|
|
|
newAuthHeader, privilegeErr := options.PrivilegeFunc()
|
|
|
|
if privilegeErr != nil {
|
|
|
|
return results, privilegeErr
|
|
|
|
}
|
|
|
|
resp, err = cli.tryImageSearch(ctx, query, newAuthHeader)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return results, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewDecoder(resp.body).Decode(&results)
|
|
|
|
return results, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *Client) tryImageSearch(ctx context.Context, query url.Values, registryAuth string) (serverResponse, error) {
|
2023-07-10 14:44:59 +00:00
|
|
|
return cli.get(ctx, "/images/search", query, http.Header{
|
|
|
|
registry.AuthHeader: {registryAuth},
|
|
|
|
})
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|