From 1189bca5a4b887e10c200ee3049c70584b9c67ed Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Tue, 18 Mar 2014 18:58:09 +0400 Subject: [PATCH] Add central place to create all collections from. --- cmd/context.go | 3 ++ debian/collections.go | 65 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 debian/collections.go diff --git a/cmd/context.go b/cmd/context.go index ff5f2923..a69c3be8 100644 --- a/cmd/context.go +++ b/cmd/context.go @@ -25,6 +25,7 @@ var context struct { database database.Storage packagePool aptly.PackagePool publishedStorage aptly.PublishedStorage + collectionFactory *debian.CollectionFactory dependencyOptions int architecturesList []string // Debug features @@ -67,6 +68,8 @@ func InitContext(cmd *commander.Command) error { return fmt.Errorf("can't open database: %s", err) } + context.collectionFactory = debian.NewCollectionFactory(context.database) + context.packagePool = files.NewPackagePool(utils.Config.RootDir) context.publishedStorage = files.NewPublishedStorage(utils.Config.RootDir) diff --git a/debian/collections.go b/debian/collections.go new file mode 100644 index 00000000..ca161f2a --- /dev/null +++ b/debian/collections.go @@ -0,0 +1,65 @@ +package debian + +import ( + "github.com/smira/aptly/database" +) + +// CollectionFactory is a single place to generate all desired collections +type CollectionFactory struct { + db database.Storage + packages *PackageCollection + remoteRepos *RemoteRepoCollection + snapshots *SnapshotCollection + localRepos *LocalRepoCollection + publishedRepos *PublishedRepoCollection +} + +// NewCollectionFactory creates new factory +func NewCollectionFactory(db database.Storage) *CollectionFactory { + return &CollectionFactory{db: db} +} + +// PackageCollection returns (or creates) new PackageCollection +func (factory *CollectionFactory) PackageCollection() *PackageCollection { + if factory.packages == nil { + factory.packages = NewPackageCollection(factory.db) + } + + return factory.packages +} + +// RemoteRepoCollection returns (or creates) new RemoteRepoCollection +func (factory *CollectionFactory) RemoteRepoCollection() *RemoteRepoCollection { + if factory.remoteRepos == nil { + factory.remoteRepos = NewRemoteRepoCollection(factory.db) + } + + return factory.remoteRepos +} + +// SnapshotCollection returns (or creates) new SnapshotCollection +func (factory *CollectionFactory) SnapshotCollection() *SnapshotCollection { + if factory.snapshots == nil { + factory.snapshots = NewSnapshotCollection(factory.db) + } + + return factory.snapshots +} + +// LocalRepoCollection returns (or creates) new LocalRepoCollection +func (factory *CollectionFactory) LocalRepoCollection() *LocalRepoCollection { + if factory.localRepos == nil { + factory.localRepos = NewLocalRepoCollection(factory.db) + } + + return factory.localRepos +} + +// PublishedRepoCollection returns (or creates) new PublishedRepoCollection +func (factory *CollectionFactory) PublishedRepoCollection() *PublishedRepoCollection { + if factory.publishedRepos == nil { + factory.publishedRepos = NewPublishedRepoCollection(factory.db) + } + + return factory.publishedRepos +}