-
Notifications
You must be signed in to change notification settings - Fork 5
/
file_manager.go
247 lines (192 loc) · 5.06 KB
/
file_manager.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package ghokin
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
mpath "path"
"path/filepath"
"sync"
)
// ProcessFileError is emitted when processing a file trigger an error
type ProcessFileError struct {
Message string
File string
}
// Error dumps a string error
func (p ProcessFileError) Error() string {
return fmt.Sprintf(`an error occurred with file "%s" : %s`, p.File, p.Message)
}
type aliases map[string]string
type indent struct {
backgroundAndScenario int
step int
tableAndDocString int
}
// FileManager handles transformation on feature files
type FileManager struct {
indentConf indent
aliases aliases
}
// NewFileManager creates a brand new FileManager, it requires indentation values and aliases defined
// as a shell commands in comments
func NewFileManager(backgroundAndScenarioIndent int, stepIndent int, tableAndDocStringIndent int, aliases map[string]string) FileManager {
return FileManager{
indent{
backgroundAndScenarioIndent,
stepIndent,
tableAndDocStringIndent,
},
aliases,
}
}
// Transform formats and applies shell commands on feature file
func (f FileManager) Transform(filename string) (bytes.Buffer, error) {
/* #nosec */
file, err := os.Open(filename)
if err != nil {
return bytes.Buffer{}, err
}
bom, err := skipBom(file)
if err != nil {
return bytes.Buffer{}, err
}
section, err := extractSections(file)
if err != nil {
return bytes.Buffer{}, err
}
buf, err := transform(section, f.indentConf, f.aliases)
if err != nil {
return bytes.Buffer{}, err
}
return *bytes.NewBuffer(append(bom, buf.Bytes()...)), nil
}
// TransformAndReplace formats and applies shell commands on file or folder
// and replace the content of files
func (f FileManager) TransformAndReplace(path string, extensions []string) []error {
return f.process(path, extensions, replaceFileWithContent)
}
// Check ensures file or folder is well formatted
func (f FileManager) Check(path string, extensions []string) []error {
return f.process(path, extensions, check)
}
func (f FileManager) process(path string, extensions []string, processFile func(file string, content []byte) error) []error {
errors := []error{}
fi, err := os.Stat(path)
if err != nil {
return append(errors, err)
}
switch mode := fi.Mode(); {
case mode.IsDir():
errors = append(errors, f.processPath(path, extensions, processFile)...)
case mode.IsRegular():
b, err := f.Transform(path)
if err != nil {
return append(errors, err)
}
if err := processFile(path, b.Bytes()); err != nil {
errors = append(errors, err)
}
}
return errors
}
func (f FileManager) processPath(path string, extensions []string, processFile func(file string, content []byte) error) []error {
errors := []error{}
fc := make(chan string)
wg := sync.WaitGroup{}
var mu sync.Mutex
files, err := findFeatureFiles(path, extensions)
if err != nil {
return []error{err}
}
if len(files) == 0 {
return []error{}
}
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
for file := range fc {
b, err := f.Transform(file)
if err != nil {
mu.Lock()
errors = append(errors, ProcessFileError{Message: err.Error(), File: file})
mu.Unlock()
continue
}
if err := processFile(file, b.Bytes()); err != nil {
mu.Lock()
errors = append(errors, err)
mu.Unlock()
}
}
wg.Done()
}()
}
for _, file := range files {
fc <- file
}
close(fc)
wg.Wait()
mu.Lock()
defer mu.Unlock()
return errors
}
func replaceFileWithContent(file string, content []byte) error {
f, err := os.Create(file)
if err != nil {
return ProcessFileError{Message: err.Error(), File: file}
}
_, err = f.Write(content)
if err != nil {
return ProcessFileError{Message: err.Error(), File: file}
}
return nil
}
func check(file string, content []byte) error {
currentContent, err := ioutil.ReadFile(file) // #nosec
if err != nil {
return ProcessFileError{Message: err.Error(), File: file}
}
if !bytes.Equal(currentContent, content) {
return ProcessFileError{Message: "file is not properly formatted", File: file}
}
return nil
}
func findFeatureFiles(rootPath string, extensions []string) ([]string, error) {
files := []string{}
if err := filepath.Walk(rootPath, func(p string, info os.FileInfo, err error) error {
if err != nil {
return err
}
for _, extension := range extensions {
if !info.IsDir() && mpath.Ext(p) == "."+extension {
files = append(files, p)
break
}
}
return nil
}); err != nil {
return []string{}, err
}
return files, nil
}
// skipBom moves file pointer after BOM if one is found and returns it
func skipBom(file *os.File) ([]byte, error) {
bom := []byte{'\xef', '\xbb', '\xbf'}
if _, err := file.Seek(0, io.SeekStart); err != nil {
return []byte{}, err
}
buffer := make([]byte, len(bom))
if n, err := file.Read(buffer); err != nil || n < len(bom) {
if _, serr := file.Seek(0, io.SeekStart); serr != nil {
return []byte{}, serr
}
return []byte{}, err
}
if bytes.Equal(bom, buffer) {
return bom, nil
}
_, err := file.Seek(0, io.SeekStart)
return []byte{}, err
}