-
Notifications
You must be signed in to change notification settings - Fork 0
/
move.go
45 lines (41 loc) · 1.01 KB
/
move.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
package resource
import (
"os"
"path/filepath"
"strings"
"github.com/sohaha/zlsgo/zfile"
"github.com/sohaha/zlsgo/zstring"
)
func (r *Resource) MoveFile() error {
d := zfile.RealPathMkdir(r.Dir, true)
return filepath.Walk(r.tmpPath, func(path string, info os.FileInfo, err error) error {
path = zfile.RealPath(path, info.IsDir())
newPath := strings.Replace(path, r.tmpPath, d, 1)
shortPath := strings.Replace(path, r.tmpPath, "/", 1)
if len(r.ignore) > 0 {
for _, v := range r.ignore {
if pathMatche(shortPath, v) {
return filepath.SkipDir
}
}
}
for k, v := range r.moveRule {
if shortPath == k {
newPath = d + strings.TrimLeft(v, "/")
delete(r.moveRule, k)
break
}
}
if r.keep != "" && zfile.FileExist(newPath) {
_ = os.Rename(newPath, newPath+"."+r.keep)
}
if info.IsDir() {
_ = os.MkdirAll(newPath, info.Mode())
return nil
}
return zfile.CopyFile(path, newPath)
})
}
func pathMatche(path, rule string) bool {
return zstring.RegexMatch(rule, path)
}