forked from vtrifonov/http-api-mock
/
file_persister.go
223 lines (184 loc) · 5.32 KB
/
file_persister.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package persist
import (
"io/ioutil"
"os"
"path"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"github.com/vtrifonov/http-api-mock/logging"
"github.com/vtrifonov/http-api-mock/utils"
"github.com/ryanuber/go-glob"
)
//FilePersister persists body in file
type FilePersister struct {
PersistPath string
}
func (fp FilePersister) GetName() string {
return "file"
}
func (fp FilePersister) Read(name string) (string, error) {
pathToFile, _ := fp.getFilePath(name)
logging.Printf("Reading entity: %s\n", pathToFile)
if _, err := os.Stat(pathToFile); err != nil {
logging.Printf("Error reading the entity (%s)\n", err)
return "", err
}
content, err := ioutil.ReadFile(pathToFile)
return string(content), err
}
func (fp FilePersister) ReadCollection(name string) (string, error) {
logging.Printf("Reading collection: %s\n", name)
filesInCollection := fp.getCollectionFiles(name)
contents := []string{}
allJSON := true
sort.Strings(filesInCollection)
for _, file := range filesInCollection {
if fileContent, err := ioutil.ReadFile(file); err == nil {
stringContent := string(fileContent)
if allJSON {
allJSON = utils.IsJSON(stringContent)
}
contents = append(contents, stringContent)
}
}
if allJSON {
return "[" + strings.Join(contents, ",") + "]", nil
}
return strings.Join(contents, "\n"), nil
}
func (fp FilePersister) Write(name, content string) error {
pathToFile, fileDir := fp.getFilePath(name)
fileContent := []byte(content)
err := os.MkdirAll(fileDir, 0755)
if err == nil {
err = ioutil.WriteFile(pathToFile, fileContent, 0755)
}
return err
}
func (fp FilePersister) Delete(name string) error {
pathToFile, _ := fp.getFilePath(name)
return os.Remove(pathToFile)
}
func (fp FilePersister) DeleteCollection(name string) error {
logging.Printf("Deleting collection: %s\n", name)
filesInCollection := fp.getCollectionFiles(name)
for _, file := range filesInCollection {
err := os.Remove(file)
if err != nil {
logging.Println(err)
}
}
return nil
}
func (fp FilePersister) GetSequence(name string, increase int) (int, error) {
fileName := "_sequences/" + name + ".value"
oldValue := 0
if content, err := fp.Read(fileName); err == nil {
oldValue, err = strconv.Atoi(content)
}
newValue := oldValue + increase
err := fp.Write(fileName, strconv.Itoa(newValue))
return newValue, err
}
func (fp FilePersister) GetValue(key string) (string, error) {
fileName := "_keyValues/" + key + ".value"
content, err := fp.Read(fileName)
return content, err
}
func (fp FilePersister) SetValue(key string, value string) error {
fileName := "_keyValues/" + key + ".value"
err := fp.Write(fileName, value)
return err
}
func (fp FilePersister) getFilePath(fileName string) (pathToFile string, fileDir string) {
pathToFile = path.Join(fp.PersistPath, fileName)
fileDir = path.Dir(pathToFile)
var err error
pathToFile, err = filepath.Abs(pathToFile)
if err != nil {
return "", ""
}
if !strings.HasPrefix(pathToFile, fp.PersistPath) {
logging.Printf("File path not under the persist path. FilePath: %s, PersistPath %s", pathToFile, fp.PersistPath)
return "", ""
}
return pathToFile, fileDir
}
func (fp FilePersister) getFolderPath(folderName string) (pathToFolder string) {
pathToFolder = path.Join(fp.PersistPath, folderName)
var err error
pathToFolder, err = filepath.Abs(pathToFolder)
if err != nil {
return ""
}
if !strings.HasPrefix(pathToFolder, fp.PersistPath) {
logging.Printf("Folder path not under the persist path. FilePath: %s, PersistPath %s", pathToFolder, fp.PersistPath)
return ""
}
return pathToFolder
}
func (fp FilePersister) getFilesInCollection(collectionName string) []string {
folder := collectionName
if strings.Index(folder, "/") == 0 {
folder = folder[1:]
}
filter := ""
if i := strings.Index(folder, "/"); i > 0 {
filter = folder[(i + 1):]
folder = folder[0:i]
}
fullFolderPath := fp.getFolderPath(folder)
filesList := []string{}
if fullFolderPath == "" {
return []string{}
}
_, err := os.Stat(fullFolderPath)
if err != nil && os.IsNotExist(err) {
return filesList
}
regex, regexError := regexp.Compile(filter)
filepath.Walk(fullFolderPath, func(filePath string, fileInfo os.FileInfo, err error) error {
if !fileInfo.IsDir() {
if len(filePath) > len(fullFolderPath) {
relativeFilePath := filePath[(len(fullFolderPath) + 1):]
if (filter == "") || glob.Glob(filter, relativeFilePath) || (regexError == nil && regex.MatchString(relativeFilePath)) {
filesList = append(filesList, filePath)
}
}
}
return nil
})
return filesList
}
func (fp FilePersister) getFilesList(name string) []string {
if strings.Index(name, ",") == 0 {
name = name[1:] // remove the starting comma
}
fileNames := strings.Split(name, ",")
files := []string{}
for _, fileName := range fileNames {
pathToFile, _ := fp.getFilePath(fileName)
if pathToFile != "" {
files = append(files, pathToFile)
}
}
return files
}
func (fp FilePersister) getCollectionFiles(name string) []string {
if strings.Index(name, ",") > -1 {
return fp.getFilesList(name)
}
return fp.getFilesInCollection(name)
}
//NewFilePersister creates a new FilePersister
func NewFilePersister(persistPath string) *FilePersister {
result := FilePersister{PersistPath: persistPath}
err := os.MkdirAll(result.PersistPath, 0755)
if err != nil {
panic(err)
}
return &result
}