store/file: add WithDir option (#2038)

This commit is contained in:
ben-toogood
2020-10-07 12:37:28 +01:00
committed by GitHub
parent 27aa1ff2ab
commit c701f96a09
3 changed files with 60 additions and 10 deletions

View File

@@ -13,17 +13,35 @@ import (
)
// NewBlobStore returns a blob file store
func NewBlobStore() (store.BlobStore, error) {
// ensure the parent directory exists
os.MkdirAll(DefaultDir, 0700)
func NewBlobStore(opts ...store.Option) (store.BlobStore, error) {
// parse the options
var options store.Options
for _, o := range opts {
o(&options)
}
return &blobStore{}, nil
var dir string
if options.Context != nil {
if d, ok := options.Context.Value(dirKey{}).(string); ok {
dir = d
}
}
if len(dir) == 0 {
dir = DefaultDir
}
// ensure the parent directory exists
os.MkdirAll(dir, 0700)
return &blobStore{dir}, nil
}
type blobStore struct{}
type blobStore struct {
dir string
}
func (b *blobStore) db() (*bolt.DB, error) {
dbPath := filepath.Join(DefaultDir, "blob.db")
dbPath := filepath.Join(b.dir, "blob.db")
return bolt.Open(dbPath, 0700, &bolt.Options{Timeout: 5 * time.Second})
}