2018-02-07 15:52:47 -05:00
|
|
|
package images // import "github.com/docker/docker/daemon/images"
|
2016-05-21 10:00:28 -04:00
|
|
|
|
|
|
|
import (
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
2016-05-21 10:00:28 -04:00
|
|
|
"strconv"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types/filters"
|
2022-03-03 04:28:39 -05:00
|
|
|
"github.com/docker/docker/api/types/registry"
|
2016-05-21 10:00:28 -04:00
|
|
|
"github.com/docker/docker/dockerversion"
|
|
|
|
)
|
|
|
|
|
|
|
|
var acceptedSearchFilterTags = map[string]bool{
|
|
|
|
"is-automated": true,
|
|
|
|
"is-official": true,
|
|
|
|
"stars": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
// SearchRegistryForImages queries the registry for images matching
|
|
|
|
// term. authConfig is used to login.
|
2018-02-02 17:18:46 -05:00
|
|
|
//
|
|
|
|
// TODO: this could be implemented in a registry service instead of the image
|
|
|
|
// service.
|
2022-03-02 10:03:38 -05:00
|
|
|
func (i *ImageService) SearchRegistryForImages(ctx context.Context, searchFilters filters.Args, term string, limit int,
|
2022-03-03 04:28:39 -05:00
|
|
|
authConfig *registry.AuthConfig,
|
|
|
|
headers map[string][]string) (*registry.SearchResults, error) {
|
2016-05-21 10:00:28 -04:00
|
|
|
if err := searchFilters.Validate(acceptedSearchFilterTags); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var isAutomated, isOfficial bool
|
|
|
|
var hasStarFilter = 0
|
2017-09-26 07:39:56 -04:00
|
|
|
if searchFilters.Contains("is-automated") {
|
2016-05-21 10:00:28 -04:00
|
|
|
if searchFilters.UniqueExactMatch("is-automated", "true") {
|
|
|
|
isAutomated = true
|
|
|
|
} else if !searchFilters.UniqueExactMatch("is-automated", "false") {
|
2017-07-19 10:20:13 -04:00
|
|
|
return nil, invalidFilter{"is-automated", searchFilters.Get("is-automated")}
|
2016-05-21 10:00:28 -04:00
|
|
|
}
|
|
|
|
}
|
2017-09-26 07:39:56 -04:00
|
|
|
if searchFilters.Contains("is-official") {
|
2016-05-21 10:00:28 -04:00
|
|
|
if searchFilters.UniqueExactMatch("is-official", "true") {
|
|
|
|
isOfficial = true
|
|
|
|
} else if !searchFilters.UniqueExactMatch("is-official", "false") {
|
2017-07-19 10:20:13 -04:00
|
|
|
return nil, invalidFilter{"is-official", searchFilters.Get("is-official")}
|
2016-05-21 10:00:28 -04:00
|
|
|
}
|
|
|
|
}
|
2017-09-26 07:39:56 -04:00
|
|
|
if searchFilters.Contains("stars") {
|
2016-05-21 10:00:28 -04:00
|
|
|
hasStars := searchFilters.Get("stars")
|
|
|
|
for _, hasStar := range hasStars {
|
|
|
|
iHasStar, err := strconv.Atoi(hasStar)
|
|
|
|
if err != nil {
|
2017-07-19 10:20:13 -04:00
|
|
|
return nil, invalidFilter{"stars", hasStar}
|
2016-05-21 10:00:28 -04:00
|
|
|
}
|
|
|
|
if iHasStar > hasStarFilter {
|
|
|
|
hasStarFilter = iHasStar
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-02 17:18:46 -05:00
|
|
|
unfilteredResult, err := i.registryService.Search(ctx, term, limit, authConfig, dockerversion.DockerUserAgent(ctx), headers)
|
2016-05-27 05:13:12 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-03 04:28:39 -05:00
|
|
|
filteredResults := []registry.SearchResult{}
|
2016-05-21 10:00:28 -04:00
|
|
|
for _, result := range unfilteredResult.Results {
|
2017-09-26 07:39:56 -04:00
|
|
|
if searchFilters.Contains("is-automated") {
|
2016-05-21 10:00:28 -04:00
|
|
|
if isAutomated != result.IsAutomated {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2017-09-26 07:39:56 -04:00
|
|
|
if searchFilters.Contains("is-official") {
|
2016-05-21 10:00:28 -04:00
|
|
|
if isOfficial != result.IsOfficial {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2017-09-26 07:39:56 -04:00
|
|
|
if searchFilters.Contains("stars") {
|
2016-05-21 10:00:28 -04:00
|
|
|
if result.StarCount < hasStarFilter {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
filteredResults = append(filteredResults, result)
|
|
|
|
}
|
|
|
|
|
2022-03-03 04:28:39 -05:00
|
|
|
return ®istry.SearchResults{
|
2016-05-21 10:00:28 -04:00
|
|
|
Query: unfilteredResult.Query,
|
|
|
|
NumResults: len(filteredResults),
|
|
|
|
Results: filteredResults,
|
|
|
|
}, nil
|
|
|
|
}
|