Skip to content

Commit 132ec66

Browse files
committed
feat: add removeAll
1 parent b599207 commit 132ec66

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

fs.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"path"
6+
)
7+
8+
func IsDir(f string) bool {
9+
s, _ := os.Stat(f)
10+
return s.IsDir()
11+
}
12+
13+
func RemoveAll(f string) error {
14+
entrys, err := os.ReadDir(f)
15+
if err != nil {
16+
return err
17+
}
18+
19+
entrysLen := len(entrys)
20+
21+
chs := make(chan interface{}, entrysLen)
22+
errChan := make(chan error)
23+
24+
for _, entry := range entrys {
25+
go func(entry os.DirEntry) {
26+
p := path.Join(f, entry.Name())
27+
if entry.IsDir() {
28+
nestedErr := RemoveAll(p)
29+
if nestedErr != nil {
30+
errChan <- nestedErr
31+
}
32+
return
33+
}
34+
os.Remove(p)
35+
}(entry)
36+
}
37+
38+
for i := 0; i < entrysLen; i++ {
39+
select {
40+
case nestedErr := <-errChan:
41+
return nestedErr
42+
case <-chs:
43+
}
44+
}
45+
return nil
46+
}

main.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,25 @@ func isHelp() bool {
1717
}
1818

1919
func main() {
20-
2120
if isHelp() {
2221
fmt.Print("\n")
2322
color.Cyan("clean-cache")
2423
fmt.Print("Description: go 写的清理 node 项目缓存,超级无敌快\n\n")
2524
os.Exit(0)
2625
}
2726

27+
cache_dirs := []string{".nuxt", "cache", ".cache", "@cache", "temp", ".temp", "@temp"}
28+
29+
for _, v := range cache_dirs {
30+
if !IsDir(v) {
31+
continue
32+
}
33+
err := RemoveAll(v)
34+
if err != nil {
35+
color.Red("remove fail: %v \nroot: %v", err, v)
36+
} else {
37+
color.Green("remove success: %v", v)
38+
}
39+
}
40+
2841
}

0 commit comments

Comments
 (0)