github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/pkg/api/handlers/compat/images_search.go (about) 1 package compat 2 3 import ( 4 "net/http" 5 "strconv" 6 7 "github.com/containers/image/v5/types" 8 "github.com/containers/libpod/libpod/image" 9 "github.com/containers/libpod/pkg/api/handlers/utils" 10 "github.com/gorilla/schema" 11 "github.com/pkg/errors" 12 ) 13 14 func SearchImages(w http.ResponseWriter, r *http.Request) { 15 decoder := r.Context().Value("decoder").(*schema.Decoder) 16 query := struct { 17 Term string `json:"term"` 18 Limit int `json:"limit"` 19 Filters map[string][]string `json:"filters"` 20 }{ 21 // This is where you can override the golang default value for one of fields 22 } 23 24 if err := decoder.Decode(&query, r.URL.Query()); err != nil { 25 utils.Error(w, "Something went wrong.", http.StatusBadRequest, errors.Wrapf(err, "Failed to parse parameters for %s", r.URL.String())) 26 return 27 } 28 29 filter := image.SearchFilter{} 30 if len(query.Filters) > 0 { 31 if len(query.Filters["stars"]) > 0 { 32 stars, err := strconv.Atoi(query.Filters["stars"][0]) 33 if err != nil { 34 utils.InternalServerError(w, err) 35 return 36 } 37 filter.Stars = stars 38 } 39 if len(query.Filters["is-official"]) > 0 { 40 isOfficial, err := strconv.ParseBool(query.Filters["is-official"][0]) 41 if err != nil { 42 utils.InternalServerError(w, err) 43 return 44 } 45 filter.IsOfficial = types.NewOptionalBool(isOfficial) 46 } 47 if len(query.Filters["is-automated"]) > 0 { 48 isAutomated, err := strconv.ParseBool(query.Filters["is-automated"][0]) 49 if err != nil { 50 utils.InternalServerError(w, err) 51 return 52 } 53 filter.IsAutomated = types.NewOptionalBool(isAutomated) 54 } 55 } 56 options := image.SearchOptions{ 57 Filter: filter, 58 Limit: query.Limit, 59 } 60 61 results, err := image.SearchImages(query.Term, options) 62 if err != nil { 63 utils.BadRequest(w, "term", query.Term, err) 64 return 65 } 66 utils.WriteResponse(w, http.StatusOK, results) 67 }