diff --git a/api/api.go b/api/api.go index 30ba1a24..e86e8012 100644 --- a/api/api.go +++ b/api/api.go @@ -2,8 +2,12 @@ package api import ( + "fmt" "github.com/gin-gonic/gin" "github.com/smira/aptly/aptly" + "github.com/smira/aptly/deb" + "github.com/smira/aptly/query" + "sort" ) // Lock order acquisition (canonical): @@ -16,3 +20,61 @@ import ( func apiVersion(c *gin.Context) { c.JSON(200, gin.H{"Version": aptly.Version}) } + +// Common piece of code to show list of packages, +// with searching & details if requested +func showPackages(c *gin.Context, reflist *deb.PackageRefList) { + result := []*deb.Package{} + + list, err := deb.NewPackageListFromRefList(reflist, context.CollectionFactory().PackageCollection(), context.Progress()) + if err != nil { + c.Fail(404, err) + return + } + + queryS := c.Request.URL.Query().Get("q") + if queryS != "" { + q, err := query.Parse(c.Request.URL.Query().Get("q")) + if err != nil { + c.Fail(400, err) + return + } + + withDeps := c.Request.URL.Query().Get("withDeps") == "1" + architecturesList := []string{} + + if withDeps { + if len(context.ArchitecturesList()) > 0 { + architecturesList = context.ArchitecturesList() + } else { + architecturesList = list.Architectures(false) + } + + sort.Strings(architecturesList) + + if len(architecturesList) == 0 { + c.Fail(400, fmt.Errorf("unable to determine list of architectures, please specify explicitly")) + return + } + } + + list.PrepareIndex() + + list, err = list.Filter([]deb.PackageQuery{q}, withDeps, + nil, context.DependencyOptions(), architecturesList) + if err != nil { + c.Fail(500, fmt.Errorf("unable to search: %s", err)) + } + } + + if c.Request.URL.Query().Get("format") == "details" { + list.ForEach(func(p *deb.Package) error { + result = append(result, p) + return nil + }) + + c.JSON(200, result) + } else { + c.JSON(200, list.Strings()) + } +} diff --git a/api/repos.go b/api/repos.go index 44e163cb..35b2b2bb 100644 --- a/api/repos.go +++ b/api/repos.go @@ -6,11 +6,9 @@ import ( "github.com/smira/aptly/aptly" "github.com/smira/aptly/database" "github.com/smira/aptly/deb" - "github.com/smira/aptly/query" "github.com/smira/aptly/utils" "os" "path/filepath" - "sort" ) // GET /api/repos @@ -178,59 +176,7 @@ func apiReposPackagesShow(c *gin.Context) { return } - list, err := deb.NewPackageListFromRefList(repo.RefList(), context.CollectionFactory().PackageCollection(), nil) - if err != nil { - c.Fail(500, err) - return - } - - list.PrepareIndex() - - result := []*deb.Package{} - queryS := c.Request.URL.Query().Get("q") - if queryS != "" { - q, err := query.Parse(queryS) - if err != nil { - c.Fail(400, err) - return - } - - withDeps := c.Request.URL.Query().Get("withDeps") == "1" - architecturesList := []string{} - - if withDeps { - if len(context.ArchitecturesList()) > 0 { - architecturesList = context.ArchitecturesList() - } else { - architecturesList = list.Architectures(false) - } - - sort.Strings(architecturesList) - - if len(architecturesList) == 0 { - c.Fail(400, fmt.Errorf("unable to determine list of architectures, please specify explicitly")) - return - } - } - - list, err = list.Filter([]deb.PackageQuery{q}, withDeps, - nil, context.DependencyOptions(), architecturesList) - if err != nil { - c.Fail(500, err) - return - } - } - - if c.Request.URL.Query().Get("format") == "details" { - list.ForEach(func(p *deb.Package) error { - result = append(result, p) - return nil - }) - - c.JSON(200, result) - } else { - c.JSON(200, list.Strings()) - } + showPackages(c, repo.RefList()) } // Handler for both add and delete diff --git a/api/snapshot.go b/api/snapshot.go index 863ee026..366c75d2 100644 --- a/api/snapshot.go +++ b/api/snapshot.go @@ -4,8 +4,6 @@ import ( "fmt" "github.com/gin-gonic/gin" "github.com/smira/aptly/deb" - "github.com/smira/aptly/query" - "sort" ) // GET /api/snapshots @@ -403,58 +401,5 @@ func apiSnapshotsSearchPackages(c *gin.Context) { return } - reflist := snapshot.RefList() - result := []*deb.Package{} - - list, err := deb.NewPackageListFromRefList(reflist, context.CollectionFactory().PackageCollection(), context.Progress()) - if err != nil { - c.Fail(404, err) - return - } - - queryS := c.Request.URL.Query().Get("q") - if queryS != "" { - q, err := query.Parse(c.Request.URL.Query().Get("q")) - if err != nil { - c.Fail(400, err) - return - } - - withDeps := c.Request.URL.Query().Get("withDeps") == "1" - architecturesList := []string{} - - if withDeps { - if len(context.ArchitecturesList()) > 0 { - architecturesList = context.ArchitecturesList() - } else { - architecturesList = list.Architectures(false) - } - - sort.Strings(architecturesList) - - if len(architecturesList) == 0 { - c.Fail(400, fmt.Errorf("unable to determine list of architectures, please specify explicitly")) - return - } - } - - list.PrepareIndex() - - list, err = list.Filter([]deb.PackageQuery{q}, withDeps, - nil, context.DependencyOptions(), architecturesList) - if err != nil { - c.Fail(500, fmt.Errorf("unable to search: %s", err)) - } - } - - if c.Request.URL.Query().Get("format") == "details" { - list.ForEach(func(p *deb.Package) error { - result = append(result, p) - return nil - }) - - c.JSON(200, result) - } else { - c.JSON(200, list.Strings()) - } + showPackages(c, snapshot.RefList()) }