-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocessor.go
75 lines (65 loc) · 1.63 KB
/
processor.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
package main
import (
"fmt"
"go-cleaner/checker"
"log"
"os"
"path"
"sort"
)
func checkAndRemove(dirPath string) error {
printPath := true
stats.FolderChecked++
entries, err := os.ReadDir(dirPath)
if err != nil {
log.Println(err)
return err
}
// check dir-s in the end
sort.Slice(entries, func(i, j int) bool { return entries[j].IsDir() })
for _, entry := range entries {
fullFilePath := path.Join(dirPath, entry.Name())
if entry.IsDir() {
checkAndRemove(fullFilePath)
// we don't need to check error here
// if internal folder is absent it means that it's been deleted or renamed
// so we just skip this check
continue
}
stats.FileChecked++
// Ignore file by WhiteList extension or Size limit
beIgored := checker.IsExtMatch(entry, config.Exts.WhiteList) ||
checker.IsSizeOver(fullFilePath, config.SizeConfig.Threshold)
if beIgored {
continue
}
isMatch := checker.IsExtMatch(entry, config.Exts.BlackList) ||
checker.IsNameMatch(entry.Name(), config.Files.BlackList) ||
checker.IsZero(fullFilePath, config.SizeConfig.CatchZero) ||
checker.IsContentContain(fullFilePath, config.Contents)
if isMatch {
catchFile(fullFilePath)
if printPath {
dumpFile.WriteString(fmt.Sprintln(dirPath))
log.Println(dirPath)
printPath = false
}
dumpFile.WriteString(fmt.Sprintf(" X: %s \n", entry.Name()))
log.Printf(" X: %s \n", entry.Name())
}
}
dumpFile.Sync()
return nil
}
func catchFile(filePath string) {
if config.RealClean {
err := os.Remove(filePath)
if err != nil {
log.Println(err)
return
}
stats.RemovedCount++
} else {
stats.FoundCount++
}
}