forked from golang-migrate/migrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
go-bindata.go
118 lines (98 loc) · 2.74 KB
/
go-bindata.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package bindata
import (
"bytes"
"fmt"
"io"
"os"
"github.com/golang-migrate/migrate/v4/source"
)
type AssetFunc func(name string) ([]byte, error)
func Resource(names []string, afn AssetFunc) *AssetSource {
return &AssetSource{
Names: names,
AssetFunc: afn,
}
}
type AssetSource struct {
Names []string
AssetFunc AssetFunc
}
func init() {
source.Register("go-bindata", &Bindata{})
}
type Bindata struct {
path string
assetSource *AssetSource
migrations *source.Migrations
}
func (b *Bindata) Open(url string) (source.Driver, error) {
return nil, fmt.Errorf("not yet implemented")
}
var (
ErrNoAssetSource = fmt.Errorf("expects *AssetSource")
)
func WithInstance(instance interface{}) (source.Driver, error) {
if _, ok := instance.(*AssetSource); !ok {
return nil, ErrNoAssetSource
}
as := instance.(*AssetSource)
bn := &Bindata{
path: "<go-bindata>",
assetSource: as,
migrations: source.NewMigrations(),
}
for _, fi := range as.Names {
m, err := source.DefaultParse(fi)
if err != nil {
continue // ignore files that we can't parse
}
if !bn.migrations.Append(m) {
return nil, fmt.Errorf("unable to parse file %v", fi)
}
}
return bn, nil
}
func (b *Bindata) Close() error {
return nil
}
func (b *Bindata) First() (version uint, err error) {
if v, ok := b.migrations.First(); !ok {
return 0, &os.PathError{Op: "first", Path: b.path, Err: os.ErrNotExist}
} else {
return v, nil
}
}
func (b *Bindata) Prev(version uint) (prevVersion uint, err error) {
if v, ok := b.migrations.Prev(version); !ok {
return 0, &os.PathError{Op: fmt.Sprintf("prev for version %v", version), Path: b.path, Err: os.ErrNotExist}
} else {
return v, nil
}
}
func (b *Bindata) Next(version uint) (nextVersion uint, err error) {
if v, ok := b.migrations.Next(version); !ok {
return 0, &os.PathError{Op: fmt.Sprintf("next for version %v", version), Path: b.path, Err: os.ErrNotExist}
} else {
return v, nil
}
}
func (b *Bindata) ReadUp(version uint) (r io.ReadCloser, identifier string, err error) {
if m, ok := b.migrations.Up(version); ok {
body, err := b.assetSource.AssetFunc(m.Raw)
if err != nil {
return nil, "", err
}
return io.NopCloser(bytes.NewReader(body)), m.Identifier, nil
}
return nil, "", &os.PathError{Op: fmt.Sprintf("read version %v", version), Path: b.path, Err: os.ErrNotExist}
}
func (b *Bindata) ReadDown(version uint) (r io.ReadCloser, identifier string, err error) {
if m, ok := b.migrations.Down(version); ok {
body, err := b.assetSource.AssetFunc(m.Raw)
if err != nil {
return nil, "", err
}
return io.NopCloser(bytes.NewReader(body)), m.Identifier, nil
}
return nil, "", &os.PathError{Op: fmt.Sprintf("read version %v", version), Path: b.path, Err: os.ErrNotExist}
}