11package txlib
22
33import (
4- "io/ioutil"
54 "os"
65 "strings"
76)
87
98const PathSeparator = string (os .PathSeparator )
109
1110/*
12- Recursively search under the directory 'root' for files that match the
13- 'fileFilter'. The original file filter must have exactly one instance of
14- "<lang>" in it.
11+ Recursively search under the directory 'root' for files that match the 'fileFilter'.
1512
1613If nothing is found, an empty map will be returned.
1714
18- If 'fileFilter' is empty, which means that we are in the last step of the
19- recursion, 'root' is returned if it exists in the filesystem and is a file.
15+ If 'fileFilter' is empty, which means that we are in the last step of the recursion,
16+ 'root' is returned if it exists in the filesystem and is a file.
2017
21- If, in the current iteration, the file filter does not have "<lang>" (which
22- means that the "<lang>" part is now in the 'root'), then the matching file, if
23- found, will be returned under the "" key ({"": "/path/to/file.txt"}).
18+ If, in the current iteration, the file filter does not have "<lang>" (which means that
19+ the "<lang>" part is now in the 'root'), then the matching file, if found, will be
20+ returned under the "" key ({"": "/path/to/file.txt"}).
2421
25- If the first item in 'fileFilter' does have a "<lang>" in it, then the contents
26- of the 'root' directory (it must be a directory) will be matched against the
27- pattern, the function will be called recursively for a new root that contains
28- the matched path and the result of the recursive function will be added to the
29- result of the current function with the matched language as the key.
22+ If the first item in 'fileFilter' does have a "<lang>" in it, then the contents of the
23+ 'root' directory (it must be a directory) will be matched against the pattern, the
24+ function will be called recursively for a new root that contains the matched path and
25+ the result of the recursive function will be added to the result of the current function
26+ with the matched language as the key.
3027
3128Examples:
3229
@@ -35,19 +32,19 @@ Examples:
3532 |
3633 + file.txt
3734
38- Then the invocation of:
35+ Then the invocation of:
3936
4037 searchFileFilter("/path/to/root/file.txt", "")
4138
42- will check that 'root' does exist and is not a directory and return:
39+ will check that 'root' does exist and is not a directory and return:
4340
4441 map[string]string{"": "/path/to/root/file.txt"}
4542
46- The invocation of:
43+ The invocation of:
4744
4845 searchFileFilter("/path/to/root", "file.txt")
4946
50- will recursively call the previous invocation and return its result:
47+ will recursively call the previous invocation and return its result:
5148
5249 map[string]string{"": "/path/to/root/file.txt"}
5350
@@ -59,26 +56,26 @@ will recursively call the previous invocation and return its result:
5956 |
6057 + fr.txt
6158
62- The invocation of:
59+ The invocation of:
6360
6461 searchFileFilter("/path/to/root/en.txt", "")
6562
66- as before, will return:
63+ as before, will return:
6764
6865 map[string]string{"": "/path/to/root/en.txt"}
6966
70- But, the invocation of:
67+ But, the invocation of:
7168
7269 searchFileFilter("/path/to/root", "<lang>.txt")
7370
74- will inspect the contents of 'root', match the 2 files against the pattern,
75- make 2 invocations similar to the first (one with "en" in 'root' and one with
76- "fr") and return their results using the matched language codes as keys. So:
71+ will inspect the contents of 'root', match the 2 files against the pattern, make 2
72+ invocations similar to the first (one with "en" in 'root' and one with "fr") and
73+ return their results using the matched language codes as keys. So:
7774
7875 map[string]string{"en": "/path/to/root/en.txt",
7976 "fr": "/path/to/root/fr.txt"}
8077
81- 3. Finally, assuming the filesystem looks like this:
78+ 3. Assuming the filesystem looks like this:
8279
8380 /path/to/root/
8481 |
@@ -90,7 +87,7 @@ make 2 invocations similar to the first (one with "en" in 'root' and one with
9087 |
9188 + file.txt
9289
93- The following calls and results will happen:
90+ The following calls and results will happen:
9491
9592 searchFileFilter("/path/to/root/en/file.txt", "")
9693 // map[string]string{"": "/path/to/root/en/file.txt"}
@@ -102,12 +99,12 @@ The following calls and results will happen:
10299 // map[string]string{"en": "/path/to/root/en/file.txt",
103100 "fr": "/path/to/root/fr/file.txt"}
104101*/
105- func searchFileFilter (root string , fileFilter string ) map [string ]string {
102+
103+ func searchFileFilter (root , fileFilter string ) map [string ]string {
106104 result := make (map [string ]string )
107105
108106 fileFilter = normaliseFileFilter (fileFilter )
109107
110- fileFilterSlice := strings .Split (fileFilter , PathSeparator )
111108 if len (fileFilter ) == 0 {
112109 fileInfo , err := os .Stat (root )
113110 if err != nil || fileInfo .IsDir () {
@@ -116,21 +113,18 @@ func searchFileFilter(root string, fileFilter string) map[string]string {
116113 result ["" ] = root
117114 return result
118115 }
119-
116+ fileFilterSlice := strings . Split ( fileFilter , PathSeparator )
120117 if ! strings .Contains (fileFilterSlice [0 ], "<lang>" ) {
121118 // Recursively go deeper
122- newRoot := strings .Join ([]string {root , fileFilterSlice [0 ]},
123- PathSeparator )
119+ newRoot := strings .Join ([]string {root , fileFilterSlice [0 ]}, PathSeparator )
124120 newFileFilter := strings .Join (fileFilterSlice [1 :], PathSeparator )
125121 return searchFileFilter (newRoot , newFileFilter )
126122 } else {
127- // Sometime before we checked that the original 'fileFilterSlice' had
128- // exactly one "<lang>" in it, so 'parts' is guaranteed to be of size 2
129123 parts := strings .Split (fileFilterSlice [0 ], "<lang>" )
130124 left := parts [0 ]
131125 right := parts [1 ]
132126
133- fileInfos , err := ioutil .ReadDir (root )
127+ fileInfos , err := os .ReadDir (root )
134128 if err != nil {
135129 return result
136130 }
@@ -149,6 +143,8 @@ func searchFileFilter(root string, fileFilter string) map[string]string {
149143
150144 newRoot := strings .Join ([]string {root , name }, PathSeparator )
151145 newFileFilter := strings .Join (fileFilterSlice [1 :], PathSeparator )
146+ // IT doesn't make sense to capture 'en/fr' with '<lang>/<lang>'
147+ newFileFilter = strings .ReplaceAll (newFileFilter , "<lang>" , languageCode )
152148 answer := searchFileFilter (newRoot , newFileFilter )
153149
154150 path , exists := answer ["" ]
0 commit comments