This repository has been archived by the owner on Jul 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
159 lines (133 loc) · 3.07 KB
/
store.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 vfs
import (
"bufio"
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
)
// Store ...
type Store interface {
Get(key string) (string, error)
}
type store struct {
files map[string]string
}
// Get takes a schema string and searches the *Schemas and returns the json
// schema if found. It returns an error if the schema is not located.
func (s *store) Get(file string) (string, error) {
f, ok := s.files[file]
if !ok {
return "", fmt.Errorf("file: '%s' could not be found", file)
}
return f, nil
}
// LoadFolders ...
func LoadFolders(folders ...string) (Store, error) {
s := &store{files: make(map[string]string)}
for _, folder := range folders {
fdir, err := os.Open(folder)
if err != nil {
return nil, err
}
defer fdir.Close()
finfo, err := fdir.Stat()
if err != nil {
return nil, err
}
err = loadFolder(fdir, finfo, s)
if err != nil {
return nil, err
}
}
return s, nil
}
// LoadFolder loads the file(s) in a given folder from the given string that
// is passed. If the path supplied is a directory it will walk through the
// entire directory and load the json files into the *Store. If the file is
// not a directory it will simply load that single file into the Store. If an
// error occurs anytime during the process an error is returned.
func LoadFolder(folder string) (Store, error) {
fdir, err := os.Open(folder)
if err != nil {
return nil, err
}
defer fdir.Close()
finfo, err := fdir.Stat()
if err != nil {
return nil, err
}
s := &store{files: make(map[string]string)}
err = loadFolder(fdir, finfo, s)
if err != nil {
return nil, err
}
return s, nil
}
func loadFolder(fdir *os.File, finfo os.FileInfo, s *store) error {
switch mode := finfo.Mode(); {
case mode.IsDir():
files, err := lsFiles(fdir.Name())
if err != nil {
return err
}
for _, f := range files {
file, err := os.Open(f)
if err != nil {
return err
}
err = loadFile(s, file)
defer file.Close()
if err != nil {
return err
}
}
case mode.IsRegular():
err := loadFile(s, fdir)
if err != nil {
return err
}
}
return nil
}
// loadFile ...
func loadFile(s *store, file *os.File) error {
base := filepath.Base(file.Name())
// store the file in the files using the base name only (not
// path/filename)
s.files[base] = scanFile(file)
return nil
}
// lsFiles takes a searchDir string and RECURSIVELY collects the file names
// inside the directory.
func lsFiles(searchDir string) ([]string, error) {
fileList := []string{}
_ = filepath.Walk(searchDir, func(path string, f os.FileInfo, err error) error {
if !f.IsDir() {
fileList = append(fileList, path)
}
return nil
})
return fileList, nil
}
// scanFile ...
func scanFile(file *os.File) string {
reader := bufio.NewReader(file)
buffer := bytes.NewBuffer(make([]byte, 0))
var err error
var eol bool
var chunk []byte
var strArray []string
for {
if chunk, eol, err = reader.ReadLine(); err != nil {
break
}
buffer.Write(chunk)
if !eol {
strArray = append(strArray, buffer.String())
buffer.Reset()
}
}
return strings.Join(strArray[:], "\n")
}