2016-05-21 10:00:28 -04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
|
|
|
registrytypes "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.
|
2016-06-01 16:38:14 -04:00
|
|
|
func (daemon *Daemon) SearchRegistryForImages(ctx context.Context, filtersArgs string, term string, limit int,
|
2016-05-21 10:00:28 -04:00
|
|
|
authConfig *types.AuthConfig,
|
|
|
|
headers map[string][]string) (*registrytypes.SearchResults, error) {
|
|
|
|
|
|
|
|
searchFilters, err := filters.FromParam(filtersArgs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := searchFilters.Validate(acceptedSearchFilterTags); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var isAutomated, isOfficial bool
|
|
|
|
var hasStarFilter = 0
|
|
|
|
if searchFilters.Include("is-automated") {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
if searchFilters.Include("is-official") {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
if searchFilters.Include("stars") {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-01 16:38:14 -04:00
|
|
|
unfilteredResult, err := daemon.RegistryService.Search(ctx, term, limit, authConfig, dockerversion.DockerUserAgent(ctx), headers)
|
2016-05-27 05:13:12 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-05-21 10:00:28 -04:00
|
|
|
filteredResults := []registrytypes.SearchResult{}
|
|
|
|
for _, result := range unfilteredResult.Results {
|
|
|
|
if searchFilters.Include("is-automated") {
|
|
|
|
if isAutomated != result.IsAutomated {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if searchFilters.Include("is-official") {
|
|
|
|
if isOfficial != result.IsOfficial {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if searchFilters.Include("stars") {
|
|
|
|
if result.StarCount < hasStarFilter {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
filteredResults = append(filteredResults, result)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ®istrytypes.SearchResults{
|
|
|
|
Query: unfilteredResult.Query,
|
|
|
|
NumResults: len(filteredResults),
|
|
|
|
Results: filteredResults,
|
|
|
|
}, nil
|
|
|
|
}
|