-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.go
144 lines (125 loc) · 2.87 KB
/
utils.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* util functions
*/
package _internal
import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"time"
)
var (
WatchFiletypes = []string{".go"} // 遍历文件夹的时候需要忽略的文件夹名字
UnWatchRegExps = []string{}
emptyPaths = []string{} // 默认空路径列表
)
// check file exist
func fileExist(fpath string) bool {
_, err := os.Stat(fpath)
return err == nil || os.IsExist(err)
}
func getFileModTime(path string) int64 {
path = strings.Replace(path, "\\", "/", -1)
f, err := os.Open(path)
if err != nil {
return time.Now().Unix()
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return time.Now().Unix()
}
return fi.ModTime().Unix()
}
// 检查文件夹是否需要排除
func checkPathExcluded(fpath string, excluedPaths []string) bool {
var (
err error
absPath string
absFPath string
)
// 获取绝对路径
if absFPath, err = filepath.Abs(fpath); err != nil {
return false
}
for _, exclPath := range excluedPaths {
// 文件夹名字 和 排除名字一致
if fpath == exclPath || absFPath == exclPath {
return true
}
if absPath, err = filepath.Abs(exclPath); err != nil {
continue
}
// println(absPath, absath)
if strings.HasPrefix(absFPath, absPath) {
return true
}
}
return false
}
// 根据后缀检测
func checkFileIncluded(name string, ftyps []string) bool {
for _, wft := range ftyps {
if strings.HasSuffix(name, wft) {
return true
}
}
return false
}
// 检查文件名字是否匹配正则
func checkFileRegexpExcluded(filename string, regexps []string) bool {
for _, reg := range regexps {
if m, err := regexp.Match(reg, []byte(filename)); err != nil {
// panic(err)
continue
} else {
// cond: err == nil
if m {
return true
}
}
}
return false
}
// 增加监听的文件类型
func AppendWatchFiletypes(names ...string) {
WatchFiletypes = append(WatchFiletypes, names...)
}
// 增加不需要监视的正则匹配表达式
func AppendUnWatchRegexps(exps ...string) {
UnWatchRegExps = append(UnWatchRegExps, exps...)
}
/*
* 递归遍历文件夹,返回所有路径,不包括需要排除的文件夹及子文件夹
* ? 返回的内容里面有重复的路径?
*/
func WalkDirectoryRecursive(dir string, excluedPaths []string, paths *[]string) {
var (
fInfos []os.FileInfo
err error
)
if fInfos, err = ioutil.ReadDir(dir); err != nil {
return
}
for _, finfo := range fInfos {
tmpDir := filepath.Join(dir, finfo.Name())
if finfo.IsDir() && checkPathExcluded(tmpDir, excluedPaths) {
continue
}
if finfo.IsDir() && finfo.Name()[0] != '.' {
WalkDirectoryRecursive(tmpDir, excluedPaths, paths)
*paths = append(*paths, tmpDir)
}
}
}
// 计算时间戳的间隔
func UnixTimeDuration(t1, t2 int64) time.Duration {
tt1 := time.Unix(t1, 0)
tt2 := time.Unix(t2, 0)
if t1 > t2 {
return tt1.Sub(tt2)
}
return tt2.Sub(tt1)
}