1package store
2
3import (
4 "context"
5 "errors"
6 "sync"
7
8 "github.com/go-git/go-billy/v5"
9)
10
11// Constructor is a function that returns a new store.
12type Constructor func(ctx context.Context, fs billy.Filesystem) (Store, error)
13
14var (
15 registry = map[string]Constructor{}
16 mtx sync.RWMutex
17
18 // ErrStoreNotFound is returned when a store is not found.
19 ErrStoreNotFound = errors.New("store not found")
20)
21
22// Register registers a store.
23func Register(name string, fn Constructor) {
24 mtx.Lock()
25 defer mtx.Unlock()
26
27 registry[name] = fn
28}
29
30// New returns a new store.
31func New(ctx context.Context, fs billy.Filesystem, name string) (Store, error) {
32 mtx.RLock()
33 fn, ok := registry[name]
34 mtx.RUnlock()
35
36 if !ok {
37 return nil, ErrStoreNotFound
38 }
39
40 return fn(ctx, fs)
41}
42
43// List returns a list of registered stores.
44func List() []string {
45 mtx.Lock()
46 defer mtx.Unlock()
47 stores := make([]string, 0)
48 for name := range registry {
49 stores = append(stores, name)
50 }
51 return stores
52}