github.com/LazyboyChen7/engine@v17.12.1-ce-rc2+incompatible/daemon/search.go (about) 1 package daemon 2 3 import ( 4 "strconv" 5 6 "golang.org/x/net/context" 7 8 "github.com/docker/docker/api/types" 9 "github.com/docker/docker/api/types/filters" 10 registrytypes "github.com/docker/docker/api/types/registry" 11 "github.com/docker/docker/dockerversion" 12 ) 13 14 var acceptedSearchFilterTags = map[string]bool{ 15 "is-automated": true, 16 "is-official": true, 17 "stars": true, 18 } 19 20 // SearchRegistryForImages queries the registry for images matching 21 // term. authConfig is used to login. 22 func (daemon *Daemon) SearchRegistryForImages(ctx context.Context, filtersArgs string, term string, limit int, 23 authConfig *types.AuthConfig, 24 headers map[string][]string) (*registrytypes.SearchResults, error) { 25 26 searchFilters, err := filters.FromJSON(filtersArgs) 27 if err != nil { 28 return nil, err 29 } 30 if err := searchFilters.Validate(acceptedSearchFilterTags); err != nil { 31 return nil, err 32 } 33 34 var isAutomated, isOfficial bool 35 var hasStarFilter = 0 36 if searchFilters.Contains("is-automated") { 37 if searchFilters.UniqueExactMatch("is-automated", "true") { 38 isAutomated = true 39 } else if !searchFilters.UniqueExactMatch("is-automated", "false") { 40 return nil, invalidFilter{"is-automated", searchFilters.Get("is-automated")} 41 } 42 } 43 if searchFilters.Contains("is-official") { 44 if searchFilters.UniqueExactMatch("is-official", "true") { 45 isOfficial = true 46 } else if !searchFilters.UniqueExactMatch("is-official", "false") { 47 return nil, invalidFilter{"is-official", searchFilters.Get("is-official")} 48 } 49 } 50 if searchFilters.Contains("stars") { 51 hasStars := searchFilters.Get("stars") 52 for _, hasStar := range hasStars { 53 iHasStar, err := strconv.Atoi(hasStar) 54 if err != nil { 55 return nil, invalidFilter{"stars", hasStar} 56 } 57 if iHasStar > hasStarFilter { 58 hasStarFilter = iHasStar 59 } 60 } 61 } 62 63 unfilteredResult, err := daemon.RegistryService.Search(ctx, term, limit, authConfig, dockerversion.DockerUserAgent(ctx), headers) 64 if err != nil { 65 return nil, err 66 } 67 68 filteredResults := []registrytypes.SearchResult{} 69 for _, result := range unfilteredResult.Results { 70 if searchFilters.Contains("is-automated") { 71 if isAutomated != result.IsAutomated { 72 continue 73 } 74 } 75 if searchFilters.Contains("is-official") { 76 if isOfficial != result.IsOfficial { 77 continue 78 } 79 } 80 if searchFilters.Contains("stars") { 81 if result.StarCount < hasStarFilter { 82 continue 83 } 84 } 85 filteredResults = append(filteredResults, result) 86 } 87 88 return ®istrytypes.SearchResults{ 89 Query: unfilteredResult.Query, 90 NumResults: len(filteredResults), 91 Results: filteredResults, 92 }, nil 93 }