-
Notifications
You must be signed in to change notification settings - Fork 6
/
purse.go
108 lines (97 loc) · 2.47 KB
/
purse.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
package purse
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
)
const (
ext string = ".sql"
)
// Purse is the interface representing a collection of SQL files whose
// contents are accessed via the basic Get method.
//
// Implementations of this interface should be safe for concurrent use by
// multiple goroutines.
type Purse interface {
Get(string) (string, bool)
}
// MemoryPurse is an implementation of Purse that uses a map of strings to
// represent the contents of SQL files found within a specified directory.
// It is safe for concurrent use by multiple goroutines.
type MemoryPurse struct {
mu sync.RWMutex
files map[string]string
}
// New loads SQL files' contents in the specified directory dir into memory.
// A file's contents can be accessed with the Get method.
//
// New returns an error if the directory does not exist or is not a directory.
func New(dir string) (*MemoryPurse, error) {
dir = filepath.Clean(dir) + string(os.PathSeparator)
f, err := os.Open(dir)
if err != nil {
return nil, err
}
defer f.Close()
fis, err := f.Readdir(-1)
if err != nil {
return nil, err
}
p := &MemoryPurse{files: make(map[string]string, 0)}
for _, fi := range fis {
if !fi.IsDir() {
if filepath.Ext(fi.Name()) == ext {
err = addToPurse(p, fi.Name(), filepath.Join(dir, fi.Name()))
}
} else {
err = filepath.Walk(filepath.Join(dir, fi.Name()), func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() || filepath.Ext(info.Name()) != ext {
return nil
}
return addToPurse(p, strings.TrimPrefix(path, dir), path)
})
}
if err != nil {
return nil, err
}
}
return p, nil
}
// Get returns a SQL file's contents as a string.
// If the file does not exist or does exists but had a read error,
// then v == "" and ok == false.
func (p *MemoryPurse) Get(filename string) (v string, ok bool) {
p.mu.RLock()
v, ok = p.files[filename]
p.mu.RUnlock()
return
}
// Files returns a slice of filenames for all loaded SQL files.
func (p *MemoryPurse) Files() []string {
fs := make([]string, len(p.files))
i := 0
for k, _ := range p.files {
fs[i] = k
i++
}
return fs
}
// addToPurse adds a file to the specified purse with the given name.
func addToPurse(purse *MemoryPurse, name, path string) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
b, err := ioutil.ReadAll(f)
if err != nil {
return err
}
purse.files[name] = string(b)
return nil
}