-
Notifications
You must be signed in to change notification settings - Fork 15
/
source_go116.go
64 lines (45 loc) · 1.15 KB
/
source_go116.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
//go:build go1.16
// +build go1.16
package migration
import (
"bytes"
"embed"
"fmt"
"io"
"io/fs"
"path"
)
// EmbedMigrationSource uses an embed.FS that is used to embed files natively in Go 1.16+
type EmbedMigrationSource struct {
EmbedFS embed.FS
// The path in the embed FS to use
Dir string
}
// ListMigrationFiles returns a list of embedded migration files
func (e EmbedMigrationSource) ListMigrationFiles() ([]string, error) {
var f fs.FS = e.EmbedFS
if e.Dir != "" {
var err error
f, err = fs.Sub(f, e.Dir)
if err != nil {
return nil, fmt.Errorf("error opening subdirectory in embed fs: %s", err)
}
}
files, err := fs.ReadDir(f, ".")
if err != nil {
return nil, fmt.Errorf("error reading directory from embed fs: %w", err)
}
var migrations []string
for _, file := range files {
if file.IsDir() {
continue
}
migrations = append(migrations, file.Name())
}
return migrations, nil
}
// GetMigrationFile gets an embedded migration file
func (e EmbedMigrationSource) GetMigrationFile(name string) (io.Reader, error) {
file, err := fs.ReadFile(e.EmbedFS, path.Join(e.Dir, name))
return bytes.NewReader(file), err
}