-
Notifications
You must be signed in to change notification settings - Fork 0
/
directory.go
executable file
·74 lines (60 loc) · 1.2 KB
/
directory.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
package main
import (
"fmt"
"io/ioutil"
"strings"
)
type Dir struct {
Name string
Path string
Files []File
Dirs []Dir
}
type File struct {
Name string
Path string
}
var ignoreDirs = []string{"bower_components", ".git", ".gitignore", "LICENSE", "README.md"}
func ReadDir(name string) Dir {
p := strings.Split(name, "/")
dir := Dir{Path: name, Name: p[len(p)-1]}
c := make(chan Dir)
go populate(c, dir)
dir = <-c
close(c)
return dir
}
func populate(c chan Dir, d Dir) {
files, err := ioutil.ReadDir(d.Path)
if err != nil {
fmt.Println(err)
}
cc := make(chan Dir)
j := 0
for _, file := range files {
if isIgnoreFile(file.Name()) {
continue
}
if file.IsDir() {
dir := Dir{Name: file.Name(), Path: d.Path + "/" + file.Name()}
go populate(cc, dir)
j++
} else {
f := File{Name: file.Name(), Path: strings.TrimPrefix(d.Path, goPath) + "/" + file.Name()}
d.Files = append(d.Files, f)
}
}
for ; j > 0; j-- {
d.Dirs = append(d.Dirs, <-cc)
}
close(cc)
c <- d
}
func isIgnoreFile(name string) bool {
for _, n := range ignoreDirs {
if n == name {
return true
}
}
return false
}