-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource_iofs.go
159 lines (143 loc) · 3.49 KB
/
source_iofs.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package lightmigrate
import (
"errors"
"io"
"io/fs"
"path"
"strconv"
)
type fsSource struct {
migrations *migrations
fsys fs.FS
path string
}
// NewFsSource returns a new MigrationSource from io/fs#FS and a relative path.
func NewFsSource(fsys fs.FS, basePath string) (MigrationSource, error) {
f := &fsSource{
migrations: newMigrations(),
fsys: fsys,
path: basePath,
}
err := f.init()
if err != nil {
return nil, err
}
return f, nil
}
// init prepares not initialized IoFS instance to read migrations from an
// io/fs#FS instance and a relative path.
func (f *fsSource) init() error {
entries, err := fs.ReadDir(f.fsys, f.path)
if err != nil {
return err
}
for _, e := range entries {
if e.IsDir() {
continue
}
m, err := parseFileName(e.Name())
if err != nil {
continue
}
file, err := e.Info()
if err != nil {
return err
}
if !f.migrations.Append(m) {
return ErrDuplicateMigration{
migration: *m,
FileInfo: file,
}
}
}
return nil
}
// open a given file path in the filesystem.
func (f *fsSource) open(path string) (fs.File, error) {
file, err := f.fsys.Open(path)
if err == nil {
return file, nil
}
// Some non-standard file systems may return errors that don't include the path, that
// makes debugging harder.
if !errors.As(err, new(*fs.PathError)) {
err = &fs.PathError{
Op: "open",
Path: path,
Err: err,
}
}
return nil, err
}
// Close is part of source.Driver interface implementation.
// Closes the file system if possible.
func (f *fsSource) Close() error {
c, ok := f.fsys.(io.Closer)
if !ok {
return nil
}
return c.Close()
}
// First is part of source.Driver interface implementation.
func (f *fsSource) First() (version uint64, err error) {
if version, ok := f.migrations.First(); ok {
return version, nil
}
return 0, &fs.PathError{
Op: "first",
Path: f.path,
Err: fs.ErrNotExist,
}
}
// Prev is part of source.Driver interface implementation.
func (f *fsSource) Prev(version uint64) (prevVersion uint64, err error) {
if version, ok := f.migrations.Prev(version); ok {
return version, nil
}
return 0, &fs.PathError{
Op: "prev for version " + strconv.FormatUint(version, 10),
Path: f.path,
Err: fs.ErrNotExist,
}
}
// Next is part of source.Driver interface implementation.
func (f *fsSource) Next(version uint64) (nextVersion uint64, err error) {
if version, ok := f.migrations.Next(version); ok {
return version, nil
}
return 0, &fs.PathError{
Op: "next for version " + strconv.FormatUint(version, 10),
Path: f.path,
Err: fs.ErrNotExist,
}
}
// ReadUp is part of source.Driver interface implementation.
func (f *fsSource) ReadUp(version uint64) (r io.ReadCloser, identifier string, err error) {
if m, ok := f.migrations.Up(version); ok {
body, err := f.open(path.Join(f.path, m.Raw))
if err != nil {
return nil, "", err
}
return body, m.Identifier, nil
}
return nil, "", &fs.PathError{
Op: "read up for version " + strconv.FormatUint(version, 10),
Path: f.path,
Err: fs.ErrNotExist,
}
}
// ReadDown is part of source.Driver interface implementation.
func (f *fsSource) ReadDown(version uint64) (r io.ReadCloser, identifier string, err error) {
if m, ok := f.migrations.Down(version); ok {
body, err := f.open(path.Join(f.path, m.Raw))
if err != nil {
return nil, "", err
}
return body, m.Identifier, nil
}
return nil, "", &fs.PathError{
Op: "read down for version " + strconv.FormatUint(version, 10),
Path: f.path,
Err: fs.ErrNotExist,
}
}