Refactoring: use CollectionFactory instead of manual collection creation.

This commit is contained in:
Andrey Smirnov
2014-03-25 14:59:26 +04:00
parent a0497058ee
commit 1a60ac6aa0
24 changed files with 101 additions and 165 deletions

View File

@@ -19,10 +19,8 @@ func ListPackagesRefList(reflist *debian.PackageRefList) (err error) {
return
}
packageCollection := debian.NewPackageCollection(context.database)
err = reflist.ForEach(func(key []byte) error {
p, err2 := packageCollection.ByKey(key)
p, err2 := context.collectionFactory.PackageCollection().ByKey(key)
if err2 != nil {
return err2
}

View File

@@ -22,9 +22,8 @@ func aptlyDbCleanup(cmd *commander.Command, args []string) error {
existingPackageRefs := debian.NewPackageRefList()
context.progress.Printf("Loading mirrors, local repos and snapshots...\n")
repoCollection := debian.NewRemoteRepoCollection(context.database)
err = repoCollection.ForEach(func(repo *debian.RemoteRepo) error {
err := repoCollection.LoadComplete(repo)
err = context.collectionFactory.RemoteRepoCollection().ForEach(func(repo *debian.RemoteRepo) error {
err := context.collectionFactory.RemoteRepoCollection().LoadComplete(repo)
if err != nil {
return err
}
@@ -37,9 +36,8 @@ func aptlyDbCleanup(cmd *commander.Command, args []string) error {
return err
}
localRepoCollection := debian.NewLocalRepoCollection(context.database)
err = localRepoCollection.ForEach(func(repo *debian.LocalRepo) error {
err := localRepoCollection.LoadComplete(repo)
err = context.collectionFactory.LocalRepoCollection().ForEach(func(repo *debian.LocalRepo) error {
err := context.collectionFactory.LocalRepoCollection().LoadComplete(repo)
if err != nil {
return err
}
@@ -52,9 +50,8 @@ func aptlyDbCleanup(cmd *commander.Command, args []string) error {
return err
}
snapshotCollection := debian.NewSnapshotCollection(context.database)
err = snapshotCollection.ForEach(func(snapshot *debian.Snapshot) error {
err := snapshotCollection.LoadComplete(snapshot)
err = context.collectionFactory.SnapshotCollection().ForEach(func(snapshot *debian.Snapshot) error {
err := context.collectionFactory.SnapshotCollection().LoadComplete(snapshot)
if err != nil {
return err
}
@@ -67,8 +64,7 @@ func aptlyDbCleanup(cmd *commander.Command, args []string) error {
// ... and compare it to the list of all packages
context.progress.Printf("Loading list of all packages...\n")
packageCollection := debian.NewPackageCollection(context.database)
allPackageRefs := packageCollection.AllPackageRefs()
allPackageRefs := context.collectionFactory.PackageCollection().AllPackageRefs()
toDelete := allPackageRefs.Substract(existingPackageRefs)
@@ -77,7 +73,7 @@ func aptlyDbCleanup(cmd *commander.Command, args []string) error {
context.database.StartBatch()
err = toDelete.ForEach(func(ref []byte) error {
return packageCollection.DeleteByKey(ref)
return context.collectionFactory.PackageCollection().DeleteByKey(ref)
})
if err != nil {
return err
@@ -94,7 +90,7 @@ func aptlyDbCleanup(cmd *commander.Command, args []string) error {
context.progress.InitBar(int64(existingPackageRefs.Len()), false)
err = existingPackageRefs.ForEach(func(key []byte) error {
pkg, err2 := packageCollection.ByKey(key)
pkg, err2 := context.collectionFactory.PackageCollection().ByKey(key)
if err2 != nil {
return err2
}

View File

@@ -29,10 +29,8 @@ func aptlyGraph(cmd *commander.Command, args []string) error {
fmt.Printf("Loading mirrors...\n")
repoCollection := debian.NewRemoteRepoCollection(context.database)
err = repoCollection.ForEach(func(repo *debian.RemoteRepo) error {
err := repoCollection.LoadComplete(repo)
err = context.collectionFactory.RemoteRepoCollection().ForEach(func(repo *debian.RemoteRepo) error {
err := context.collectionFactory.RemoteRepoCollection().LoadComplete(repo)
if err != nil {
return err
}
@@ -55,10 +53,8 @@ func aptlyGraph(cmd *commander.Command, args []string) error {
fmt.Printf("Loading local repos...\n")
localRepoCollection := debian.NewLocalRepoCollection(context.database)
err = localRepoCollection.ForEach(func(repo *debian.LocalRepo) error {
err := localRepoCollection.LoadComplete(repo)
err = context.collectionFactory.LocalRepoCollection().ForEach(func(repo *debian.LocalRepo) error {
err := context.collectionFactory.LocalRepoCollection().LoadComplete(repo)
if err != nil {
return err
}
@@ -80,15 +76,13 @@ func aptlyGraph(cmd *commander.Command, args []string) error {
fmt.Printf("Loading snapshots...\n")
snapshotCollection := debian.NewSnapshotCollection(context.database)
snapshotCollection.ForEach(func(snapshot *debian.Snapshot) error {
context.collectionFactory.SnapshotCollection().ForEach(func(snapshot *debian.Snapshot) error {
existingNodes[snapshot.UUID] = true
return nil
})
err = snapshotCollection.ForEach(func(snapshot *debian.Snapshot) error {
err := snapshotCollection.LoadComplete(snapshot)
err = context.collectionFactory.SnapshotCollection().ForEach(func(snapshot *debian.Snapshot) error {
err := context.collectionFactory.SnapshotCollection().LoadComplete(snapshot)
if err != nil {
return err
}
@@ -122,9 +116,7 @@ func aptlyGraph(cmd *commander.Command, args []string) error {
fmt.Printf("Loading published repos...\n")
publishedCollection := debian.NewPublishedRepoCollection(context.database)
publishedCollection.ForEach(func(repo *debian.PublishedRepo) error {
context.collectionFactory.PublishedRepoCollection().ForEach(func(repo *debian.PublishedRepo) error {
graph.AddNode("aptly", graphvizEscape(repo.UUID), map[string]string{
"shape": "Mrecord",
"style": "filled",

View File

@@ -48,9 +48,7 @@ func aptlyMirrorCreate(cmd *commander.Command, args []string) error {
return fmt.Errorf("unable to fetch mirror: %s", err)
}
repoCollection := debian.NewRemoteRepoCollection(context.database)
err = repoCollection.Add(repo)
err = context.collectionFactory.RemoteRepoCollection().Add(repo)
if err != nil {
return fmt.Errorf("unable to add mirror: %s", err)
}

View File

@@ -4,7 +4,6 @@ import (
"fmt"
"github.com/gonuts/commander"
"github.com/gonuts/flag"
"github.com/smira/aptly/debian"
)
func aptlyMirrorDrop(cmd *commander.Command, args []string) error {
@@ -16,16 +15,14 @@ func aptlyMirrorDrop(cmd *commander.Command, args []string) error {
name := args[0]
repoCollection := debian.NewRemoteRepoCollection(context.database)
repo, err := repoCollection.ByName(name)
repo, err := context.collectionFactory.RemoteRepoCollection().ByName(name)
if err != nil {
return fmt.Errorf("unable to drop: %s", err)
}
force := cmd.Flag.Lookup("force").Value.Get().(bool)
if !force {
snapshotCollection := debian.NewSnapshotCollection(context.database)
snapshots := snapshotCollection.ByRemoteRepoSource(repo)
snapshots := context.collectionFactory.SnapshotCollection().ByRemoteRepoSource(repo)
if len(snapshots) > 0 {
fmt.Printf("Mirror `%s` was used to create following snapshots:\n", repo.Name)
@@ -37,7 +34,7 @@ func aptlyMirrorDrop(cmd *commander.Command, args []string) error {
}
}
err = repoCollection.Drop(repo)
err = context.collectionFactory.RemoteRepoCollection().Drop(repo)
if err != nil {
return fmt.Errorf("unable to drop: %s", err)
}

View File

@@ -15,13 +15,11 @@ func aptlyMirrorList(cmd *commander.Command, args []string) error {
return err
}
repoCollection := debian.NewRemoteRepoCollection(context.database)
if repoCollection.Len() > 0 {
if context.collectionFactory.RemoteRepoCollection().Len() > 0 {
fmt.Printf("List of mirrors:\n")
repos := make([]string, repoCollection.Len())
repos := make([]string, context.collectionFactory.RemoteRepoCollection().Len())
i := 0
repoCollection.ForEach(func(repo *debian.RemoteRepo) error {
context.collectionFactory.RemoteRepoCollection().ForEach(func(repo *debian.RemoteRepo) error {
repos[i] = repo.String()
i++
return nil

View File

@@ -4,7 +4,6 @@ import (
"fmt"
"github.com/gonuts/commander"
"github.com/gonuts/flag"
"github.com/smira/aptly/debian"
"github.com/smira/aptly/utils"
"strings"
)
@@ -18,13 +17,12 @@ func aptlyMirrorShow(cmd *commander.Command, args []string) error {
name := args[0]
repoCollection := debian.NewRemoteRepoCollection(context.database)
repo, err := repoCollection.ByName(name)
repo, err := context.collectionFactory.RemoteRepoCollection().ByName(name)
if err != nil {
return fmt.Errorf("unable to show: %s", err)
}
err = repoCollection.LoadComplete(repo)
err = context.collectionFactory.RemoteRepoCollection().LoadComplete(repo)
if err != nil {
return fmt.Errorf("unable to show: %s", err)
}

View File

@@ -4,7 +4,6 @@ import (
"fmt"
"github.com/gonuts/commander"
"github.com/gonuts/flag"
"github.com/smira/aptly/debian"
)
func aptlyMirrorUpdate(cmd *commander.Command, args []string) error {
@@ -16,13 +15,12 @@ func aptlyMirrorUpdate(cmd *commander.Command, args []string) error {
name := args[0]
repoCollection := debian.NewRemoteRepoCollection(context.database)
repo, err := repoCollection.ByName(name)
repo, err := context.collectionFactory.RemoteRepoCollection().ByName(name)
if err != nil {
return fmt.Errorf("unable to update: %s", err)
}
err = repoCollection.LoadComplete(repo)
err = context.collectionFactory.RemoteRepoCollection().LoadComplete(repo)
if err != nil {
return fmt.Errorf("unable to update: %s", err)
}
@@ -39,14 +37,12 @@ func aptlyMirrorUpdate(cmd *commander.Command, args []string) error {
return fmt.Errorf("unable to update: %s", err)
}
packageCollection := debian.NewPackageCollection(context.database)
err = repo.Download(context.progress, context.downloader, packageCollection, context.packagePool, ignoreMismatch)
err = repo.Download(context.progress, context.downloader, context.collectionFactory, context.packagePool, ignoreMismatch)
if err != nil {
return fmt.Errorf("unable to update: %s", err)
}
err = repoCollection.Update(repo)
err = context.collectionFactory.RemoteRepoCollection().Update(repo)
if err != nil {
return fmt.Errorf("unable to update: %s", err)
}

View File

@@ -4,7 +4,6 @@ import (
"fmt"
"github.com/gonuts/commander"
"github.com/gonuts/flag"
"github.com/smira/aptly/debian"
)
func aptlyPublishDrop(cmd *commander.Command, args []string) error {
@@ -21,9 +20,7 @@ func aptlyPublishDrop(cmd *commander.Command, args []string) error {
prefix = args[1]
}
publishedCollecton := debian.NewPublishedRepoCollection(context.database)
err = publishedCollecton.Remove(context.publishedStorage, prefix, distribution)
err = context.collectionFactory.PublishedRepoCollection().Remove(context.publishedStorage, prefix, distribution)
if err != nil {
return fmt.Errorf("unable to remove: %s", err)
}

View File

@@ -23,21 +23,19 @@ func aptlyRepoAdd(cmd *commander.Command, args []string) error {
verifier := &utils.GpgVerifier{}
localRepoCollection := debian.NewLocalRepoCollection(context.database)
repo, err := localRepoCollection.ByName(name)
repo, err := context.collectionFactory.LocalRepoCollection().ByName(name)
if err != nil {
return fmt.Errorf("unable to add: %s", err)
}
err = localRepoCollection.LoadComplete(repo)
err = context.collectionFactory.LocalRepoCollection().LoadComplete(repo)
if err != nil {
return fmt.Errorf("unable to add: %s", err)
}
context.progress.Printf("Loading packages...\n")
packageCollection := debian.NewPackageCollection(context.database)
list, err := debian.NewPackageListFromRefList(repo.RefList(), packageCollection, context.progress)
list, err := debian.NewPackageListFromRefList(repo.RefList(), context.collectionFactory.PackageCollection(), context.progress)
if err != nil {
return fmt.Errorf("unable to load packages: %s", err)
}
@@ -144,7 +142,7 @@ func aptlyRepoAdd(cmd *commander.Command, args []string) error {
continue
}
err = packageCollection.Update(p)
err = context.collectionFactory.PackageCollection().Update(p)
if err != nil {
context.progress.ColoredPrintf("@y[!]@| @!Unable to save package %s: %s@|", p, err)
continue
@@ -162,7 +160,7 @@ func aptlyRepoAdd(cmd *commander.Command, args []string) error {
repo.UpdateRefList(debian.NewPackageRefListFromPackageList(list))
err = localRepoCollection.Update(repo)
err = context.collectionFactory.LocalRepoCollection().Update(repo)
if err != nil {
return fmt.Errorf("unable to save: %s", err)
}

View File

@@ -16,9 +16,7 @@ func aptlyRepoCreate(cmd *commander.Command, args []string) error {
repo := debian.NewLocalRepo(args[0], cmd.Flag.Lookup("comment").Value.String())
localRepoCollection := debian.NewLocalRepoCollection(context.database)
err = localRepoCollection.Add(repo)
err = context.collectionFactory.LocalRepoCollection().Add(repo)
if err != nil {
return fmt.Errorf("unable to add local repo: %s", err)
}

View File

@@ -15,14 +15,12 @@ func aptlyRepoList(cmd *commander.Command, args []string) error {
return err
}
localRepoCollection := debian.NewLocalRepoCollection(context.database)
if localRepoCollection.Len() > 0 {
if context.collectionFactory.LocalRepoCollection().Len() > 0 {
fmt.Printf("List of mirrors:\n")
repos := make([]string, localRepoCollection.Len())
repos := make([]string, context.collectionFactory.LocalRepoCollection().Len())
i := 0
localRepoCollection.ForEach(func(repo *debian.LocalRepo) error {
err := localRepoCollection.LoadComplete(repo)
context.collectionFactory.LocalRepoCollection().ForEach(func(repo *debian.LocalRepo) error {
err := context.collectionFactory.LocalRepoCollection().LoadComplete(repo)
if err != nil {
return err
}

View File

@@ -17,14 +17,12 @@ func aptlyRepoMoveCopyImport(cmd *commander.Command, args []string) error {
command := cmd.Name()
localRepoCollection := debian.NewLocalRepoCollection(context.database)
dstRepo, err := localRepoCollection.ByName(args[1])
dstRepo, err := context.collectionFactory.LocalRepoCollection().ByName(args[1])
if err != nil {
return fmt.Errorf("unable to %s: %s", command, err)
}
err = localRepoCollection.LoadComplete(dstRepo)
err = context.collectionFactory.LocalRepoCollection().LoadComplete(dstRepo)
if err != nil {
return fmt.Errorf("unable to %s: %s", command, err)
}
@@ -35,7 +33,7 @@ func aptlyRepoMoveCopyImport(cmd *commander.Command, args []string) error {
)
if command == "copy" || command == "move" {
srcRepo, err = localRepoCollection.ByName(args[0])
srcRepo, err = context.collectionFactory.LocalRepoCollection().ByName(args[0])
if err != nil {
return fmt.Errorf("unable to %s: %s", command, err)
}
@@ -44,7 +42,7 @@ func aptlyRepoMoveCopyImport(cmd *commander.Command, args []string) error {
return fmt.Errorf("unable to %s: source and destination are the same", command)
}
err = localRepoCollection.LoadComplete(srcRepo)
err = context.collectionFactory.LocalRepoCollection().LoadComplete(srcRepo)
if err != nil {
return fmt.Errorf("unable to %s: %s", command, err)
}
@@ -52,14 +50,13 @@ func aptlyRepoMoveCopyImport(cmd *commander.Command, args []string) error {
srcRefList = srcRepo.RefList()
} else if command == "import" {
var srcRemoteRepo *debian.RemoteRepo
repoCollection := debian.NewRemoteRepoCollection(context.database)
srcRemoteRepo, err = repoCollection.ByName(args[0])
srcRemoteRepo, err = context.collectionFactory.RemoteRepoCollection().ByName(args[0])
if err != nil {
return fmt.Errorf("unable to %s: %s", command, err)
}
err = repoCollection.LoadComplete(srcRemoteRepo)
err = context.collectionFactory.RemoteRepoCollection().LoadComplete(srcRemoteRepo)
if err != nil {
return fmt.Errorf("unable to %s: %s", command, err)
}
@@ -75,13 +72,12 @@ func aptlyRepoMoveCopyImport(cmd *commander.Command, args []string) error {
context.progress.Printf("Loading packages...\n")
packageCollection := debian.NewPackageCollection(context.database)
dstList, err := debian.NewPackageListFromRefList(dstRepo.RefList(), packageCollection, context.progress)
dstList, err := debian.NewPackageListFromRefList(dstRepo.RefList(), context.collectionFactory.PackageCollection(), context.progress)
if err != nil {
return fmt.Errorf("unable to load packages: %s", err)
}
srcList, err := debian.NewPackageListFromRefList(srcRefList, packageCollection, context.progress)
srcList, err := debian.NewPackageListFromRefList(srcRefList, context.collectionFactory.PackageCollection(), context.progress)
if err != nil {
return fmt.Errorf("unable to load packages: %s", err)
}
@@ -145,7 +141,7 @@ func aptlyRepoMoveCopyImport(cmd *commander.Command, args []string) error {
} else {
dstRepo.UpdateRefList(debian.NewPackageRefListFromPackageList(dstList))
err = localRepoCollection.Update(dstRepo)
err = context.collectionFactory.LocalRepoCollection().Update(dstRepo)
if err != nil {
return fmt.Errorf("unable to save: %s", err)
}
@@ -153,7 +149,7 @@ func aptlyRepoMoveCopyImport(cmd *commander.Command, args []string) error {
if command == "move" {
srcRepo.UpdateRefList(debian.NewPackageRefListFromPackageList(srcList))
err = localRepoCollection.Update(srcRepo)
err = context.collectionFactory.LocalRepoCollection().Update(srcRepo)
if err != nil {
return fmt.Errorf("unable to save: %s", err)
}

View File

@@ -16,21 +16,19 @@ func aptlyRepoRemove(cmd *commander.Command, args []string) error {
name := args[0]
localRepoCollection := debian.NewLocalRepoCollection(context.database)
repo, err := localRepoCollection.ByName(name)
repo, err := context.collectionFactory.LocalRepoCollection().ByName(name)
if err != nil {
return fmt.Errorf("unable to remove: %s", err)
}
err = localRepoCollection.LoadComplete(repo)
err = context.collectionFactory.LocalRepoCollection().LoadComplete(repo)
if err != nil {
return fmt.Errorf("unable to remove: %s", err)
}
context.progress.Printf("Loading packages...\n")
packageCollection := debian.NewPackageCollection(context.database)
list, err := debian.NewPackageListFromRefList(repo.RefList(), packageCollection, context.progress)
list, err := debian.NewPackageListFromRefList(repo.RefList(), context.collectionFactory.PackageCollection(), context.progress)
if err != nil {
return fmt.Errorf("unable to load packages: %s", err)
}
@@ -52,7 +50,7 @@ func aptlyRepoRemove(cmd *commander.Command, args []string) error {
} else {
repo.UpdateRefList(debian.NewPackageRefListFromPackageList(list))
err = localRepoCollection.Update(repo)
err = context.collectionFactory.LocalRepoCollection().Update(repo)
if err != nil {
return fmt.Errorf("unable to save: %s", err)
}

View File

@@ -4,7 +4,6 @@ import (
"fmt"
"github.com/gonuts/commander"
"github.com/gonuts/flag"
"github.com/smira/aptly/debian"
)
func aptlyRepoShow(cmd *commander.Command, args []string) error {
@@ -16,13 +15,12 @@ func aptlyRepoShow(cmd *commander.Command, args []string) error {
name := args[0]
localRepoCollection := debian.NewLocalRepoCollection(context.database)
repo, err := localRepoCollection.ByName(name)
repo, err := context.collectionFactory.LocalRepoCollection().ByName(name)
if err != nil {
return fmt.Errorf("unable to show: %s", err)
}
err = localRepoCollection.LoadComplete(repo)
err = context.collectionFactory.LocalRepoCollection().LoadComplete(repo)
if err != nil {
return fmt.Errorf("unable to show: %s", err)
}

View File

@@ -19,13 +19,12 @@ func aptlySnapshotCreate(cmd *commander.Command, args []string) error {
repoName, snapshotName := args[3], args[0]
repoCollection := debian.NewRemoteRepoCollection(context.database)
repo, err = repoCollection.ByName(repoName)
repo, err = context.collectionFactory.RemoteRepoCollection().ByName(repoName)
if err != nil {
return fmt.Errorf("unable to create snapshot: %s", err)
}
err = repoCollection.LoadComplete(repo)
err = context.collectionFactory.RemoteRepoCollection().LoadComplete(repo)
if err != nil {
return fmt.Errorf("unable to create snapshot: %s", err)
}
@@ -40,13 +39,12 @@ func aptlySnapshotCreate(cmd *commander.Command, args []string) error {
localRepoName, snapshotName := args[3], args[0]
localRepoCollection := debian.NewLocalRepoCollection(context.database)
repo, err = localRepoCollection.ByName(localRepoName)
repo, err = context.collectionFactory.LocalRepoCollection().ByName(localRepoName)
if err != nil {
return fmt.Errorf("unable to create snapshot: %s", err)
}
err = localRepoCollection.LoadComplete(repo)
err = context.collectionFactory.LocalRepoCollection().LoadComplete(repo)
if err != nil {
return fmt.Errorf("unable to create snapshot: %s", err)
}
@@ -67,9 +65,7 @@ func aptlySnapshotCreate(cmd *commander.Command, args []string) error {
return err
}
snapshotCollection := debian.NewSnapshotCollection(context.database)
err = snapshotCollection.Add(snapshot)
err = context.collectionFactory.SnapshotCollection().Add(snapshot)
if err != nil {
return fmt.Errorf("unable to add snapshot: %s", err)
}

View File

@@ -4,7 +4,6 @@ import (
"fmt"
"github.com/gonuts/commander"
"github.com/gonuts/flag"
"github.com/smira/aptly/debian"
)
func aptlySnapshotDiff(cmd *commander.Command, args []string) error {
@@ -16,33 +15,30 @@ func aptlySnapshotDiff(cmd *commander.Command, args []string) error {
onlyMatching := cmd.Flag.Lookup("only-matching").Value.Get().(bool)
snapshotCollection := debian.NewSnapshotCollection(context.database)
packageCollection := debian.NewPackageCollection(context.database)
// Load <name-a> snapshot
snapshotA, err := snapshotCollection.ByName(args[0])
snapshotA, err := context.collectionFactory.SnapshotCollection().ByName(args[0])
if err != nil {
return fmt.Errorf("unable to load snapshot A: %s", err)
}
err = snapshotCollection.LoadComplete(snapshotA)
err = context.collectionFactory.SnapshotCollection().LoadComplete(snapshotA)
if err != nil {
return fmt.Errorf("unable to load snapshot A: %s", err)
}
// Load <name-b> snapshot
snapshotB, err := snapshotCollection.ByName(args[1])
snapshotB, err := context.collectionFactory.SnapshotCollection().ByName(args[1])
if err != nil {
return fmt.Errorf("unable to load snapshot B: %s", err)
}
err = snapshotCollection.LoadComplete(snapshotB)
err = context.collectionFactory.SnapshotCollection().LoadComplete(snapshotB)
if err != nil {
return fmt.Errorf("unable to load snapshot B: %s", err)
}
// Calculate diff
diff, err := snapshotA.RefList().Diff(snapshotB.RefList(), packageCollection)
diff, err := snapshotA.RefList().Diff(snapshotB.RefList(), context.collectionFactory.PackageCollection())
if err != nil {
return fmt.Errorf("unable to calculate diff: %s", err)
}

View File

@@ -15,15 +15,13 @@ func aptlySnapshotList(cmd *commander.Command, args []string) error {
return err
}
snapshotCollection := debian.NewSnapshotCollection(context.database)
if snapshotCollection.Len() > 0 {
if context.collectionFactory.SnapshotCollection().Len() > 0 {
fmt.Printf("List of snapshots:\n")
snapshots := make([]string, snapshotCollection.Len())
snapshots := make([]string, context.collectionFactory.SnapshotCollection().Len())
i := 0
snapshotCollection.ForEach(func(snapshot *debian.Snapshot) error {
context.collectionFactory.SnapshotCollection().ForEach(func(snapshot *debian.Snapshot) error {
snapshots[i] = snapshot.String()
i++
return nil

View File

@@ -15,17 +15,15 @@ func aptlySnapshotMerge(cmd *commander.Command, args []string) error {
return err
}
snapshotCollection := debian.NewSnapshotCollection(context.database)
sources := make([]*debian.Snapshot, len(args)-1)
for i := 0; i < len(args)-1; i++ {
sources[i], err = snapshotCollection.ByName(args[i+1])
sources[i], err = context.collectionFactory.SnapshotCollection().ByName(args[i+1])
if err != nil {
return fmt.Errorf("unable to load snapshot: %s", err)
}
err = snapshotCollection.LoadComplete(sources[i])
err = context.collectionFactory.SnapshotCollection().LoadComplete(sources[i])
if err != nil {
return fmt.Errorf("unable to load snapshot: %s", err)
}
@@ -46,7 +44,7 @@ func aptlySnapshotMerge(cmd *commander.Command, args []string) error {
destination := debian.NewSnapshotFromRefList(args[0], sources, result,
fmt.Sprintf("Merged from sources: %s", strings.Join(sourceDescription, ", ")))
err = snapshotCollection.Add(destination)
err = context.collectionFactory.SnapshotCollection().Add(destination)
if err != nil {
return fmt.Errorf("unable to create snapshot: %s", err)
}

View File

@@ -19,27 +19,24 @@ func aptlySnapshotPull(cmd *commander.Command, args []string) error {
noDeps := cmd.Flag.Lookup("no-deps").Value.Get().(bool)
noRemove := cmd.Flag.Lookup("no-remove").Value.Get().(bool)
snapshotCollection := debian.NewSnapshotCollection(context.database)
packageCollection := debian.NewPackageCollection(context.database)
// Load <name> snapshot
snapshot, err := snapshotCollection.ByName(args[0])
snapshot, err := context.collectionFactory.SnapshotCollection().ByName(args[0])
if err != nil {
return fmt.Errorf("unable to pull: %s", err)
}
err = snapshotCollection.LoadComplete(snapshot)
err = context.collectionFactory.SnapshotCollection().LoadComplete(snapshot)
if err != nil {
return fmt.Errorf("unable to pull: %s", err)
}
// Load <source> snapshot
source, err := snapshotCollection.ByName(args[1])
source, err := context.collectionFactory.SnapshotCollection().ByName(args[1])
if err != nil {
return fmt.Errorf("unable to pull: %s", err)
}
err = snapshotCollection.LoadComplete(source)
err = context.collectionFactory.SnapshotCollection().LoadComplete(source)
if err != nil {
return fmt.Errorf("unable to pull: %s", err)
}
@@ -49,12 +46,12 @@ func aptlySnapshotPull(cmd *commander.Command, args []string) error {
// Convert snapshot to package list
context.progress.Printf("Loading packages (%d)...\n", snapshot.RefList().Len()+source.RefList().Len())
packageList, err := debian.NewPackageListFromRefList(snapshot.RefList(), packageCollection, context.progress)
packageList, err := debian.NewPackageListFromRefList(snapshot.RefList(), context.collectionFactory.PackageCollection(), context.progress)
if err != nil {
return fmt.Errorf("unable to load packages: %s", err)
}
sourcePackageList, err := debian.NewPackageListFromRefList(source.RefList(), packageCollection, context.progress)
sourcePackageList, err := debian.NewPackageListFromRefList(source.RefList(), context.collectionFactory.PackageCollection(), context.progress)
if err != nil {
return fmt.Errorf("unable to load packages: %s", err)
}
@@ -157,7 +154,7 @@ func aptlySnapshotPull(cmd *commander.Command, args []string) error {
destination := debian.NewSnapshotFromPackageList(args[2], []*debian.Snapshot{snapshot, source}, packageList,
fmt.Sprintf("Pulled into '%s' with '%s' as source, pull request was: '%s'", snapshot.Name, source.Name, strings.Join(args[3:], " ")))
err = snapshotCollection.Add(destination)
err = context.collectionFactory.SnapshotCollection().Add(destination)
if err != nil {
return fmt.Errorf("unable to create snapshot: %s", err)
}

View File

@@ -4,7 +4,6 @@ import (
"fmt"
"github.com/gonuts/commander"
"github.com/gonuts/flag"
"github.com/smira/aptly/debian"
)
func aptlySnapshotShow(cmd *commander.Command, args []string) error {
@@ -16,13 +15,12 @@ func aptlySnapshotShow(cmd *commander.Command, args []string) error {
name := args[0]
snapshotCollection := debian.NewSnapshotCollection(context.database)
snapshot, err := snapshotCollection.ByName(name)
snapshot, err := context.collectionFactory.SnapshotCollection().ByName(name)
if err != nil {
return fmt.Errorf("unable to show: %s", err)
}
err = snapshotCollection.LoadComplete(snapshot)
err = context.collectionFactory.SnapshotCollection().LoadComplete(snapshot)
if err != nil {
return fmt.Errorf("unable to show: %s", err)
}

View File

@@ -15,17 +15,14 @@ func aptlySnapshotVerify(cmd *commander.Command, args []string) error {
return err
}
snapshotCollection := debian.NewSnapshotCollection(context.database)
packageCollection := debian.NewPackageCollection(context.database)
snapshots := make([]*debian.Snapshot, len(args))
for i := range snapshots {
snapshots[i], err = snapshotCollection.ByName(args[i])
snapshots[i], err = context.collectionFactory.SnapshotCollection().ByName(args[i])
if err != nil {
return fmt.Errorf("unable to verify: %s", err)
}
err = snapshotCollection.LoadComplete(snapshots[i])
err = context.collectionFactory.SnapshotCollection().LoadComplete(snapshots[i])
if err != nil {
return fmt.Errorf("unable to verify: %s", err)
}
@@ -33,7 +30,7 @@ func aptlySnapshotVerify(cmd *commander.Command, args []string) error {
context.progress.Printf("Loading packages...\n")
packageList, err := debian.NewPackageListFromRefList(snapshots[0].RefList(), packageCollection, context.progress)
packageList, err := debian.NewPackageListFromRefList(snapshots[0].RefList(), context.collectionFactory.PackageCollection(), context.progress)
if err != nil {
fmt.Errorf("unable to load packages: %s", err)
}
@@ -46,7 +43,7 @@ func aptlySnapshotVerify(cmd *commander.Command, args []string) error {
var pL *debian.PackageList
for i := 1; i < len(snapshots); i++ {
pL, err = debian.NewPackageListFromRefList(snapshots[i].RefList(), packageCollection, context.progress)
pL, err = debian.NewPackageListFromRefList(snapshots[i].RefList(), context.collectionFactory.PackageCollection(), context.progress)
if err != nil {
fmt.Errorf("unable to load packages: %s", err)
}

4
debian/remote.go vendored
View File

@@ -315,7 +315,7 @@ ok:
}
// Download downloads all repo files
func (repo *RemoteRepo) Download(progress aptly.Progress, d aptly.Downloader, packageCollection *PackageCollection, packagePool aptly.PackagePool, ignoreMismatch bool) error {
func (repo *RemoteRepo) Download(progress aptly.Progress, d aptly.Downloader, collectionFactory *CollectionFactory, packagePool aptly.PackagePool, ignoreMismatch bool) error {
list := NewPackageList()
progress.Printf("Downloading & parsing package files...\n")
@@ -379,7 +379,7 @@ func (repo *RemoteRepo) Download(progress aptly.Progress, d aptly.Downloader, pa
return err
}
err = packageCollection.Update(p)
err = collectionFactory.PackageCollection().Update(p)
if err != nil {
return err
}

24
debian/remote_test.go vendored
View File

@@ -72,7 +72,7 @@ type RemoteRepoSuite struct {
downloader *http.FakeDownloader
progress aptly.Progress
db database.Storage
packageCollection *PackageCollection
collectionFactory *CollectionFactory
packagePool aptly.PackagePool
}
@@ -84,7 +84,7 @@ func (s *RemoteRepoSuite) SetUpTest(c *C) {
s.downloader = http.NewFakeDownloader().ExpectResponse("http://mirror.yandex.ru/debian/dists/squeeze/Release", exampleReleaseFile)
s.progress = console.NewProgress()
s.db, _ = database.OpenDB(c.MkDir())
s.packageCollection = NewPackageCollection(s.db)
s.collectionFactory = NewCollectionFactory(s.db)
s.packagePool = files.NewPackagePool(c.MkDir())
s.SetUpPackages()
s.progress.Start()
@@ -246,12 +246,12 @@ func (s *RemoteRepoSuite) TestDownload(c *C) {
s.downloader.ExpectResponse("http://mirror.yandex.ru/debian/dists/squeeze/main/binary-i386/Packages", examplePackagesFile)
s.downloader.ExpectResponse("http://mirror.yandex.ru/debian/pool/main/a/amanda/amanda-client_3.3.1-3~bpo60+1_amd64.deb", "xyz")
err = s.repo.Download(s.progress, s.downloader, s.packageCollection, s.packagePool, false)
err = s.repo.Download(s.progress, s.downloader, s.collectionFactory, s.packagePool, false)
c.Assert(err, IsNil)
c.Assert(s.downloader.Empty(), Equals, true)
c.Assert(s.repo.packageRefs, NotNil)
pkg, err := s.packageCollection.ByKey(s.repo.packageRefs.Refs[0])
pkg, err := s.collectionFactory.PackageCollection().ByKey(s.repo.packageRefs.Refs[0])
c.Assert(err, IsNil)
result, err := pkg.VerifyFiles(s.packagePool)
@@ -279,12 +279,12 @@ func (s *RemoteRepoSuite) TestDownloadWithSources(c *C) {
s.downloader.AnyExpectResponse("http://mirror.yandex.ru/debian/pool/main/a/access-modifier-checker/access-modifier-checker_1.0.orig.tar.gz", "abcd")
s.downloader.AnyExpectResponse("http://mirror.yandex.ru/debian/pool/main/a/access-modifier-checker/access-modifier-checker_1.0-4.debian.tar.gz", "abcde")
err = s.repo.Download(s.progress, s.downloader, s.packageCollection, s.packagePool, false)
err = s.repo.Download(s.progress, s.downloader, s.collectionFactory, s.packagePool, false)
c.Assert(err, IsNil)
c.Assert(s.downloader.Empty(), Equals, true)
c.Assert(s.repo.packageRefs, NotNil)
pkg, err := s.packageCollection.ByKey(s.repo.packageRefs.Refs[0])
pkg, err := s.collectionFactory.PackageCollection().ByKey(s.repo.packageRefs.Refs[0])
c.Assert(err, IsNil)
result, err := pkg.VerifyFiles(s.packagePool)
@@ -293,7 +293,7 @@ func (s *RemoteRepoSuite) TestDownloadWithSources(c *C) {
c.Check(pkg.Name, Equals, "amanda-client")
pkg, err = s.packageCollection.ByKey(s.repo.packageRefs.Refs[1])
pkg, err = s.collectionFactory.PackageCollection().ByKey(s.repo.packageRefs.Refs[1])
c.Assert(err, IsNil)
result, err = pkg.VerifyFiles(s.packagePool)
@@ -314,12 +314,12 @@ func (s *RemoteRepoSuite) TestDownloadFlat(c *C) {
err := s.flat.Fetch(downloader, nil)
c.Assert(err, IsNil)
err = s.flat.Download(s.progress, downloader, s.packageCollection, s.packagePool, false)
err = s.flat.Download(s.progress, downloader, s.collectionFactory, s.packagePool, false)
c.Assert(err, IsNil)
c.Assert(downloader.Empty(), Equals, true)
c.Assert(s.flat.packageRefs, NotNil)
pkg, err := s.packageCollection.ByKey(s.flat.packageRefs.Refs[0])
pkg, err := s.collectionFactory.PackageCollection().ByKey(s.flat.packageRefs.Refs[0])
c.Assert(err, IsNil)
result, err := pkg.VerifyFiles(s.packagePool)
@@ -348,12 +348,12 @@ func (s *RemoteRepoSuite) TestDownloadWithSourcesFlat(c *C) {
err := s.flat.Fetch(downloader, nil)
c.Assert(err, IsNil)
err = s.flat.Download(s.progress, downloader, s.packageCollection, s.packagePool, false)
err = s.flat.Download(s.progress, downloader, s.collectionFactory, s.packagePool, false)
c.Assert(err, IsNil)
c.Assert(downloader.Empty(), Equals, true)
c.Assert(s.flat.packageRefs, NotNil)
pkg, err := s.packageCollection.ByKey(s.flat.packageRefs.Refs[0])
pkg, err := s.collectionFactory.PackageCollection().ByKey(s.flat.packageRefs.Refs[0])
c.Assert(err, IsNil)
result, err := pkg.VerifyFiles(s.packagePool)
@@ -362,7 +362,7 @@ func (s *RemoteRepoSuite) TestDownloadWithSourcesFlat(c *C) {
c.Check(pkg.Name, Equals, "amanda-client")
pkg, err = s.packageCollection.ByKey(s.flat.packageRefs.Refs[1])
pkg, err = s.collectionFactory.PackageCollection().ByKey(s.flat.packageRefs.Refs[1])
c.Assert(err, IsNil)
result, err = pkg.VerifyFiles(s.packagePool)