Common piece of code to show list of packages, with searching & details if requested
(c *gin.Context, reflist *deb.PackageRefList, collectionFactory *deb.CollectionFactory)
| 234 | // Common piece of code to show list of packages, |
| 235 | // with searching & details if requested |
| 236 | func showPackages(c *gin.Context, reflist *deb.PackageRefList, collectionFactory *deb.CollectionFactory) { |
| 237 | result := []*deb.Package{} |
| 238 | |
| 239 | list, err := deb.NewPackageListFromRefList(reflist, collectionFactory.PackageCollection(), nil) |
| 240 | if err != nil { |
| 241 | AbortWithJSONError(c, 404, err) |
| 242 | return |
| 243 | } |
| 244 | |
| 245 | queryS := c.Request.URL.Query().Get("q") |
| 246 | if queryS != "" { |
| 247 | q, err := query.Parse(c.Request.URL.Query().Get("q")) |
| 248 | if err != nil { |
| 249 | AbortWithJSONError(c, 400, err) |
| 250 | return |
| 251 | } |
| 252 | |
| 253 | withDeps := c.Request.URL.Query().Get("withDeps") == "1" |
| 254 | architecturesList := []string{} |
| 255 | |
| 256 | if withDeps { |
| 257 | if len(context.ArchitecturesList()) > 0 { |
| 258 | architecturesList = context.ArchitecturesList() |
| 259 | } else { |
| 260 | architecturesList = list.Architectures(false) |
| 261 | } |
| 262 | |
| 263 | sort.Strings(architecturesList) |
| 264 | |
| 265 | if len(architecturesList) == 0 { |
| 266 | AbortWithJSONError(c, 400, fmt.Errorf("unable to determine list of architectures, please specify explicitly")) |
| 267 | return |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | list.PrepareIndex() |
| 272 | |
| 273 | list, err = list.Filter(deb.FilterOptions{ |
| 274 | Queries: []deb.PackageQuery{q}, |
| 275 | WithDependencies: withDeps, |
| 276 | Source: nil, |
| 277 | DependencyOptions: context.DependencyOptions(), |
| 278 | Architectures: architecturesList, |
| 279 | }) |
| 280 | if err != nil { |
| 281 | AbortWithJSONError(c, 500, fmt.Errorf("unable to search: %s", err)) |
| 282 | return |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | // filter packages by version |
| 287 | if c.Request.URL.Query().Get("maximumVersion") == "1" { |
| 288 | list.PrepareIndex() |
| 289 | _ = list.ForEach(func(p *deb.Package) error { |
| 290 | versionQ, err := query.Parse(fmt.Sprintf("Name (%s), $Version (<= %s)", p.Name, p.Version)) |
| 291 | if err != nil { |
| 292 | fmt.Println("filter packages by version, query string parse err: ", err) |
| 293 | _ = c.AbortWithError(500, fmt.Errorf("unable to parse %s maximum version query string: %s", p.Name, err)) |
no test coverage detected