-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
107 lines (100 loc) · 2.26 KB
/
util.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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"regexp"
)
func checkErr(err error) {
if err != nil {
log.Println(err)
os.Exit(1)
}
}
func createFolderStructure() {
_ = os.Mkdir("images/green", os.ModePerm)
_ = os.Mkdir("images/ocean", os.ModePerm)
_ = os.Mkdir("images/yellow", os.ModePerm)
_ = os.Mkdir("images/orange", os.ModePerm)
_ = os.Mkdir("output/translated", os.ModePerm)
_ = os.Mkdir("output/warped", os.ModePerm)
_ = os.Mkdir("output/shapefiles", os.ModePerm)
_ = os.Mkdir("result/shapefile", os.ModePerm)
_ = os.Mkdir("result/json", os.ModePerm)
_ = os.Mkdir("result/kml", os.ModePerm)
}
func clear() {
log.Println("Removing old files...")
err := removeContents("images/green")
checkErr(err)
err = removeContents("images/ocean")
checkErr(err)
err = removeContents("images/yellow")
checkErr(err)
err = removeContents("images/orange")
checkErr(err)
err = removeContents("output/translated")
checkErr(err)
err = removeContents("output/warped")
checkErr(err)
err = removeContents("output/shapefiles")
checkErr(err)
err = removeContents("result/shapefile")
checkErr(err)
err = removeContents("result/json")
checkErr(err)
err = removeContents("result/kml")
checkErr(err)
}
func getFileList(dir string) []string {
fileList := []string{}
err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
if !f.IsDir() {
fileList = append(fileList, path)
}
return nil
})
if err != nil {
log.Println(err)
}
return fileList
}
func getFileListByPrefixAndExt(dir, prefix, ext string) []string {
fileList := []string{}
err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
if !f.IsDir() {
if filepath.Ext(f.Name()) != ext {
return nil
}
r, err := regexp.MatchString(prefix, f.Name())
if err == nil && r {
fname := fmt.Sprintf("%s/%s", dir, f.Name())
fileList = append(fileList, fname)
}
}
return nil
})
if err != nil {
log.Println(err)
}
return fileList
}
func removeContents(dir string) error {
d, err := os.Open(dir)
if err != nil {
return err
}
defer d.Close()
names, err := d.Readdirnames(-1)
if err != nil {
return err
}
for _, name := range names {
err = os.RemoveAll(filepath.Join(dir, name))
if err != nil {
return err
}
}
return nil
}