-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_files.go
More file actions
49 lines (44 loc) · 1.09 KB
/
map_files.go
File metadata and controls
49 lines (44 loc) · 1.09 KB
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
package utils
import (
"fmt"
"io/ioutil"
"strings"
)
// Traverses along the files tree and fills the map
// Use it like:
// m := make(map[string][]string // map of ([directory_name] [ array of files if any ])
// path := "/home/user"
// if err := utils.Collect(path, m); err != nil {
// // handle an error
// }
// use the resulting map as you wish
func Collect(basePath string, m map[string][]string) error {
if _, err := ioutil.ReadDir(basePath); err != nil {
return err
}
err := traverse(&m, basePath)
if err != nil {
return err
}
return nil
}
// Traverse file tree and append to the map
func traverse(mapPtr *map[string][]string, path string) error {
files, err := ioutil.ReadDir(path)
if err != nil {
return err
}
for _, file := range files {
// I don't want any dotfiles
if !strings.HasPrefix(file.Name(), ".") {
if !file.IsDir() {
(*mapPtr)[path] = append((*mapPtr)[path], file.Name())
} else {
// File type is directory so let's call traverse recursively
_ = traverse(mapPtr,
fmt.Sprintf("%s/%s", path, file.Name()))
}
}
}
return nil
}