-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
86 lines (74 loc) · 1.93 KB
/
main.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
package dirtree
import (
"io/fs"
"os"
"path"
"strings"
)
// TreeNode
//Directory Tree Node
//
//目录树节点
type TreeNode struct {
//node name 节点名称
Name string
//node base name does not contain extension 节点名称 不包含扩展名
BaseName string
//node extension 节点扩展名
ExtName string
//current dir name 当前节点所在文件夹名称
CurrDirName string
//base dir 当前节点所在文件夹路径地址 不包含当前节点所在文件夹
BaseDirPath string
//is dir 是否文件夹
IsDir bool
//full path 完整路径
FullPath string
//file info os.DirEntry.Info()
FileInfo os.FileInfo
//file mode os.DirEntry.Type()
Type fs.FileMode
//array of subordinate nodes 子节点数组
Children []*TreeNode
}
// Tree
//
//Get directory tree recursively 递归获取目录树
//
// param: name dir path 目录路径地址
//
// param: matchExt match extension map nil all files 匹配扩展名Map
//
// param: sortFunc sort function nil no sort 排序函数
//
// return:
func Tree(name string, matchExt map[string]string, sortFunc func([]*TreeNode)) []*TreeNode {
treeNodes := make([]*TreeNode, 0, 0)
dirEntries, _ := os.ReadDir(name)
for _, dirEntry := range dirEntries {
treeNode := new(TreeNode)
treeNode.Name = dirEntry.Name()
treeNode.ExtName = path.Ext(treeNode.Name)
treeNode.BaseName = strings.TrimSuffix(treeNode.Name, treeNode.ExtName)
treeNode.CurrDirName = path.Base(name)
treeNode.BaseDirPath = path.Dir(name)
treeNode.FullPath = path.Join(name, treeNode.Name)
treeNode.IsDir = dirEntry.IsDir()
treeNode.Type = dirEntry.Type()
treeNode.FileInfo, _ = dirEntry.Info()
if treeNode.IsDir {
treeNode.Children = Tree(treeNode.FullPath, matchExt, sortFunc)
} else {
if matchExt != nil {
if _, ok := matchExt[treeNode.ExtName]; !ok {
continue
}
}
}
treeNodes = append(treeNodes, treeNode)
}
if sortFunc != nil {
sortFunc(treeNodes)
}
return treeNodes
}