-
Notifications
You must be signed in to change notification settings - Fork 23
/
filefilter.go
173 lines (133 loc) · 4.76 KB
/
filefilter.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
package txlib
import (
"os"
"strings"
)
const PathSeparator = string(os.PathSeparator)
/*
Recursively search under the directory 'root' for files that match the 'fileFilter'.
If nothing is found, an empty map will be returned.
If 'fileFilter' is empty, which means that we are in the last step of the recursion,
'root' is returned if it exists in the filesystem and is a file.
If, in the current iteration, the file filter does not have "<lang>" (which means that
the "<lang>" part is now in the 'root'), then the matching file, if found, will be
returned under the "" key ({"": "/path/to/file.txt"}).
If the first item in 'fileFilter' does have a "<lang>" in it, then the contents of the
'root' directory (it must be a directory) will be matched against the pattern, the
function will be called recursively for a new root that contains the matched path and
the result of the recursive function will be added to the result of the current function
with the matched language as the key.
Examples:
1. Assuming the filesystem looks like this:
/path/to/root/
|
+ file.txt
Then the invocation of:
searchFileFilter("/path/to/root/file.txt", "")
will check that 'root' does exist and is not a directory and return:
map[string]string{"": "/path/to/root/file.txt"}
The invocation of:
searchFileFilter("/path/to/root", "file.txt")
will recursively call the previous invocation and return its result:
map[string]string{"": "/path/to/root/file.txt"}
2. Assuming the filesystem looks like this:
/path/to/root/
|
+ en.txt
|
+ fr.txt
The invocation of:
searchFileFilter("/path/to/root/en.txt", "")
as before, will return:
map[string]string{"": "/path/to/root/en.txt"}
But, the invocation of:
searchFileFilter("/path/to/root", "<lang>.txt")
will inspect the contents of 'root', match the 2 files against the pattern, make 2
invocations similar to the first (one with "en" in 'root' and one with "fr") and
return their results using the matched language codes as keys. So:
map[string]string{"en": "/path/to/root/en.txt",
"fr": "/path/to/root/fr.txt"}
3. Assuming the filesystem looks like this:
/path/to/root/
|
+ en/
| |
| + file.txt
|
+ fr/
|
+ file.txt
The following calls and results will happen:
searchFileFilter("/path/to/root/en/file.txt", "")
// map[string]string{"": "/path/to/root/en/file.txt"}
searchFileFilter("/path/to/root/en", "file.txt")
// map[string]string{"": "/path/to/root/en/file.txt"}
searchFileFilter("/path/to/root", "<lang>/file.txt")
// map[string]string{"en": "/path/to/root/en/file.txt",
"fr": "/path/to/root/fr/file.txt"}
*/
func searchFileFilter(root, fileFilter string) map[string]string {
result := make(map[string]string)
fileFilter = normaliseFileFilter(fileFilter)
if len(fileFilter) == 0 {
fileInfo, err := os.Stat(root)
if err != nil || fileInfo.IsDir() {
return result
}
result[""] = root
return result
}
fileFilterSlice := strings.Split(fileFilter, PathSeparator)
if !strings.Contains(fileFilterSlice[0], "<lang>") {
// Recursively go deeper
newRoot := strings.Join([]string{root, fileFilterSlice[0]}, PathSeparator)
newFileFilter := strings.Join(fileFilterSlice[1:], PathSeparator)
return searchFileFilter(newRoot, newFileFilter)
} else {
parts := strings.Split(fileFilterSlice[0], "<lang>")
left := parts[0]
right := parts[1]
fileInfos, err := os.ReadDir(root)
if err != nil {
return result
}
for _, fileInfo := range fileInfos {
name := fileInfo.Name()
if len(name) < len(left)+len(right) ||
// before-fr-after
// ^^^^^^^
name[:len(left)] != left ||
// before-fr-after
// ^^^^^^
name[len(name)-len(right):] != right {
continue
}
languageCode := name[len(left) : len(name)-len(right)]
newRoot := strings.Join([]string{root, name}, PathSeparator)
newFileFilter := strings.Join(fileFilterSlice[1:], PathSeparator)
// IT doesn't make sense to capture 'en/fr' with '<lang>/<lang>'
newFileFilter = strings.ReplaceAll(newFileFilter, "<lang>", languageCode)
answer := searchFileFilter(newRoot, newFileFilter)
path, exists := answer[""]
if exists {
result[languageCode] = path
}
}
return result
}
}
/**
Best effort try to figure out if we need to change the path separator
Case: Someone creates the config paths for a linux machine but
tries to use the CLI from a windows machine or the opposite
*/
func normaliseFileFilter(fileFilter string) string {
if !strings.Contains(fileFilter, PathSeparator) {
sep := "/"
if PathSeparator == "/" {
sep = "\\"
}
fileFilter = strings.Replace(fileFilter, sep, PathSeparator, -1)
}
return fileFilter
}