-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathembed_markdown_driver.go
69 lines (62 loc) · 1.45 KB
/
embed_markdown_driver.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 sqlmanager
import (
"embed"
"io/fs"
"log"
"path"
"strings"
)
type EmbedMarkdownDriver struct {
fs embed.FS
dir string
}
func NewMarkdownDriverWithEmbedDir(fs embed.FS, dir string) *EmbedMarkdownDriver {
return &EmbedMarkdownDriver{
fs: fs,
dir: dir,
}
}
func NewMarkdownDriverWithEmbed(fs embed.FS) *EmbedMarkdownDriver {
return NewMarkdownDriverWithEmbedDir(fs, "sql")
}
func (mdd *EmbedMarkdownDriver) DriverName() string {
return "embed"
}
func (mdd *EmbedMarkdownDriver) Load() ([]SqlTemple, error) {
var sqls []SqlTemple
err := fs.WalkDir(mdd.fs, mdd.dir, func(subpath string, d fs.DirEntry, err error) error {
if d.IsDir() {
return nil
}
ext := path.Ext(subpath)
if ext == ".md" || ext == ".markdown" {
s, err := mdd.parseMarkdown(subpath)
if err != nil {
return err
}
if len(s) != 0 {
sqls = append(sqls, s...)
}
}
return nil
})
if err != nil {
return nil, err
}
return sqls, nil
}
func (mdd *EmbedMarkdownDriver) parseMarkdown(filename string) ([]SqlTemple, error) {
buf, err := mdd.fs.ReadFile(filename)
if err != nil {
log.Printf("sqlmanager - ERROR: %s loading failed...\n", filename)
return nil, err
}
return parseMarkdown(buf, mdd.getName(filename))
}
func (mdd *EmbedMarkdownDriver) getName(filename string) string {
ext := path.Ext(filename)
base := strings.TrimSuffix(filename, ext)
base = strings.TrimPrefix(base, mdd.dir)
base = strings.TrimPrefix(base, "/")
return base
}