register: frame out basic driver registration

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
Vincent Batts 2017-04-20 14:46:38 -04:00
parent 71d8e2cb3a
commit 867595505e
Signed by: vbatts
GPG key ID: 10937E57733F1362
3 changed files with 52 additions and 3 deletions

View file

@ -2,5 +2,6 @@ package driver
// Driver is the vfs level driver to operate on filesystem trees (i.e.
// copy-on-write filesystems, etc)
type Driver struct {
type Driver interface {
Init(rootPath string) error
}

View file

@ -1,15 +1,42 @@
package storage
import "git.thisco.de/vbatts/image-storage-interfaces/driver"
import (
"sort"
"sync"
"git.thisco.de/vbatts/image-storage-interfaces/driver"
)
var (
storageDriversMu sync.RWMutex
storageDrivers = make(map[string]driver.Driver)
)
// Drivers returns a sorted list of the registered drivers
func Drivers() []string {
return nil
storageDriversMu.Lock()
defer storageDriversMu.Unlock()
var list []string
for name, _ := range storageDrivers {
list = append(list, name)
}
sort.Strings(list)
return list
}
// Register makes a database driver available by the provided name. If Register
// is called twice with the same name or if driver is nil, it panics.
func Register(name string, driver driver.Driver) {
storageDriversMu.Lock()
defer storageDriversMu.Unlock()
if driver == nil {
panic("storage: Register driver is nil")
}
if _, dup := storageDrivers[name]; dup {
panic("storage: Register called twice for driver " + name)
}
storageDrivers[name] = driver
}
// OpenHandle a storage handle using the specified storage driver name and a

21
register_test.go Normal file
View file

@ -0,0 +1,21 @@
package storage
import "testing"
type memDriver struct {
}
func (md memDriver) Init(rootPath string) error {
return nil
}
func TestRegister(t *testing.T) {
originalLen := len(Drivers())
// TODO assert this does not panic? (and later assert that one _does_ panic?)
Register("mem", memDriver{})
if nowLen := len(Drivers()); nowLen != originalLen+1 {
t.Errorf("expected registered driver to be reflected, but was not. original: %d; now: %d", originalLen, nowLen)
}
}