/
listOfFiles.go
128 lines (95 loc) · 2.26 KB
/
listOfFiles.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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"strconv"
"github.com/FourtekIT/devnagri-cli/config"
"github.com/Jeffail/gabs"
)
func main() {
files := visit2("./en")
/*
for _, file := range files {
fmt.Println(file)
}
*/
impFiles := requiredExtensionFiles(files, "xml")
/*
for _, file := range impFiles {
fmt.Println(file)
}
*/
//DONE
fmt.Println(jsonListofFiles(impFiles))
/*
//TODO: Populate this struct on a per file basis.
var fileJSON = map[string]string{
"file[0][hash]": sha256Hash(filename),
"file[0][extension]": Extension,
"file[0][file_type]": Extension,
"file[0][location]": filename,
}
*/
var tempMap = make(map[int]string)
for i := range []int{1, 2, 3, 4, 5} {
tempMap[i], _ = strconv.FormatInt(i, 10)
//fmt.Println(i)
}
fmt.Println(tempMap)
}
func visit2(searchDir string) []string {
//searchDir := "./en"
fileList := []string{}
filepath.Walk(searchDir, func(path string, f os.FileInfo, err error) error {
fileList = append(fileList, path)
return nil
})
return fileList
/*
for _, file := range fileList {
fmt.Println(file)
}
*/
}
func visit(files *[]string) filepath.WalkFunc {
return func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Fatal(err)
}
*files = append(*files, path)
return nil
}
}
func requiredExtensionFiles(files []string, extension string) []string {
extensionRegexp := ".*." + extension + "$"
var reqFiles []string
for _, file := range files {
matches, _ := regexp.MatchString(extensionRegexp, file)
if matches == true {
reqFiles = append(reqFiles, file)
}
}
return reqFiles
}
func jsonListofFiles(files []string) string {
// TODO Change the following to be initialized form the calling location
var allFiles []string
RootDir := config.FetchAndValidate("RootFolder") // returns string
//RootDir := "./en"
Extension := config.FetchAndValidate("Extension")
//Extension := "pdf"
err := filepath.Walk(RootDir, visit(&allFiles))
if err != nil {
panic(err)
}
reqFiles := requiredExtensionFiles(allFiles, Extension)
//fmt.Println(reqFiles)
jsonObj := gabs.New()
jsonObj.Set(reqFiles)
//fmt.Println(jsonObj.String())
// Return the list of files as a json structure
return jsonObj.String()
}