-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.go
47 lines (43 loc) · 1009 Bytes
/
loader.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
package loader
import (
"io/ioutil"
"path"
)
// ListDirectoryContents gets slice of file paths.
// If recursive is true, it searches file recursivly.
func ListDirectoryContents(directoryName string, recursive bool) ([]string, error) {
items, err := ioutil.ReadDir(directoryName)
if err != nil {
return nil, err
}
var files []string
for _, item := range items {
if item.IsDir() {
if recursive == false {
continue
}
targetPath := path.Join(directoryName, item.Name())
childFiles, err := ListDirectoryContents(targetPath, recursive)
if err != nil {
return nil, err
}
for _, file := range childFiles {
files = append(files, file)
}
} else {
filePath := path.Join(directoryName, item.Name())
if isMarkdown(filePath) {
files = append(files, filePath)
}
}
}
return files, nil
}
func isMarkdown(fileName string) bool {
if path.Ext(fileName) == ".md" {
return true
} else if path.Ext(fileName) == ".markdown" {
return true
}
return false
}