-
Notifications
You must be signed in to change notification settings - Fork 0
/
container.go
75 lines (69 loc) · 1.42 KB
/
container.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
package sequelie
import (
"errors"
"os"
"strings"
"sync"
)
var store = make(map[string]*Query)
func readDirs(directories []string, options *Options) error {
var waitGroup sync.WaitGroup
var errs []error
for _, dir := range directories {
waitGroup.Add(1)
dir := dir
go func() {
if err := readDir(dir, options); err != nil {
errs = append(errs, err)
}
waitGroup.Done()
}()
}
waitGroup.Wait()
if len(errs) > 0 {
return errors.Join(errs...)
}
return nil
}
var sqlExtensionBytes = []byte(".sql")
func readDir(directory string, options *Options) error {
dir, err := os.ReadDir(directory)
if err != nil {
return err
}
if !strings.HasSuffix(directory, "/") {
directory += "/"
}
var waitGroup sync.WaitGroup
var errs []error
for _, file := range dir {
file := file
if file.IsDir() {
waitGroup.Add(1)
go func() {
if err := readDir(directory+file.Name(), options); err != nil {
errs = append(errs, err)
}
waitGroup.Done()
}()
continue
}
if insensitiveHasSuffix([]byte(file.Name()), sqlExtensionBytes) {
waitGroup.Add(1)
go func() {
if err := reader.read(directory+file.Name(), store, options); err != nil {
errs = append(errs, err)
}
waitGroup.Done()
}()
}
}
waitGroup.Wait()
if len(errs) > 0 {
return errors.Join(errs...)
}
return nil
}
func readFile(file string, options *Options) error {
return reader.read(file, store, options)
}