-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoba.go
69 lines (60 loc) · 1.78 KB
/
goba.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package goba
import "errors"
// ErrNoSuchHandler occurs whenever Goba could
// not find a handler for some DatabaseType.
var ErrNoSuchHandler = errors.New("no such handler")
// Goba manages a collection of image handlers and
// acts as the main interface of the application.
type Goba struct {
handlers []ImageHandler
}
// New creates a new Goba managing the given handlers.
func New(handlers ...ImageHandler) *Goba {
return &Goba{handlers: handlers}
}
// CreateImage creates a new Image of the Database with the given type.
func (g Goba) CreateImage(typ DatabaseType) (*Image, error) {
for _, handler := range g.handlers {
if handler.Type() == typ {
return handler.CreateImage()
}
}
return nil, ErrNoSuchHandler
}
// ApplyImage applies the Image with the given
// name to the Database with the given type.
func (g Goba) ApplyImage(typ DatabaseType, name string) error {
for _, handler := range g.handlers {
if handler.Type() == typ {
return handler.ApplyImage(name)
}
}
return ErrNoSuchHandler
}
// FindImage retrieves the Image with the given type and name.
func (g Goba) FindImage(typ DatabaseType, name string) (*Image, error) {
for _, handler := range g.handlers {
if handler.Type() == typ {
return handler.FindImage(name)
}
}
return nil, ErrNoSuchHandler
}
// AllImages retrieves all images with the given type.
func (g Goba) AllImages(typ DatabaseType) ([]Image, error) {
for _, handler := range g.handlers {
if handler.Type() == typ {
return handler.AllImages()
}
}
return nil, ErrNoSuchHandler
}
// DeleteImage deletes the Image with the given type and name.
func (g Goba) DeleteImage(typ DatabaseType, name string) error {
for _, handler := range g.handlers {
if handler.Type() == typ {
return handler.DeleteImage(name)
}
}
return ErrNoSuchHandler
}