diff options
author | Michael Muré <batolettre@gmail.com> | 2018-09-02 15:45:14 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2018-09-02 15:45:14 +0200 |
commit | 90a45b4c0979dd744aa5a28c84ececf243f027d2 (patch) | |
tree | bc2bea4bd1452b00685a7ad967027f78e626eba7 /cache/multi_repo_cache.go | |
parent | 6ff02ed84254275f2d19569a1a5f14efc1d47f31 (diff) | |
download | git-bug-90a45b4c0979dd744aa5a28c84ececf243f027d2.tar.gz |
cache: rename RootCache into MultiRepoCache
Underline the fact that it's fine to use RepoCache alone
Diffstat (limited to 'cache/multi_repo_cache.go')
-rw-r--r-- | cache/multi_repo_cache.go | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/cache/multi_repo_cache.go b/cache/multi_repo_cache.go new file mode 100644 index 00000000..ad43edab --- /dev/null +++ b/cache/multi_repo_cache.go @@ -0,0 +1,75 @@ +package cache + +import ( + "fmt" + + "github.com/MichaelMure/git-bug/repository" +) + +const lockfile = "lock" +const excerptsFile = "excerpts" + +type MultiRepoCache struct { + repos map[string]*RepoCache +} + +func NewMultiRepoCache() MultiRepoCache { + return MultiRepoCache{ + repos: make(map[string]*RepoCache), + } +} + +// RegisterRepository register a named repository. Use this for multi-repo setup +func (c *MultiRepoCache) RegisterRepository(ref string, repo repository.Repo) error { + r, err := NewRepoCache(repo) + if err != nil { + return err + } + + c.repos[ref] = r + return nil +} + +// RegisterDefaultRepository register a unnamed repository. Use this for mono-repo setup +func (c *MultiRepoCache) RegisterDefaultRepository(repo repository.Repo) error { + r, err := NewRepoCache(repo) + if err != nil { + return err + } + + c.repos[""] = r + return nil +} + +// ResolveRepo retrieve a repository by name +func (c *MultiRepoCache) DefaultRepo() (*RepoCache, error) { + if len(c.repos) != 1 { + return nil, fmt.Errorf("repository is not unique") + } + + for _, r := range c.repos { + return r, nil + } + + panic("unreachable") +} + +// DefaultRepo retrieve the default repository +func (c *MultiRepoCache) ResolveRepo(ref string) (*RepoCache, error) { + r, ok := c.repos[ref] + if !ok { + return nil, fmt.Errorf("unknown repo") + } + return r, nil +} + +// Close will do anything that is needed to close the cache properly +func (c *MultiRepoCache) Close() error { + for _, cachedRepo := range c.repos { + err := cachedRepo.Close() + if err != nil { + return err + } + } + return nil +} |