-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
71 lines (62 loc) · 1.49 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
package main
import (
"flag"
"os"
"runtime"
"sync"
"github.com/hidehalo/encmdump/filesys"
"github.com/hidehalo/encmdump/ncm"
)
// Overwrite flag
var _OW bool
func findDumpJobs(path string, jobsCh chan string, exit *sync.WaitGroup) {
filter := func(f os.FileInfo) bool {
if f.Name() == ".DS_Store" {
return false
}
if f.Name() == ".git" {
return false
}
if !f.IsDir() && !ncm.IsNCM(f.Name()) {
return false
}
return true
}
files := filesys.ReadDir(path, filter)
nextDirs := make([]string, 0)
for _, file := range files {
if file.IsDir() {
nextDirs = append(nextDirs, path+"/"+file.Name())
} else if ncm.IsNCM(file.Name()) {
exit.Add(1)
jobsCh <- (path + "/" + file.Name())
ncm.ConsoleOut(path + "/" + file.Name() + "添加到处理队列")
}
}
for _, dirPath := range nextDirs {
findDumpJobs(dirPath, jobsCh, exit)
}
}
func processJobs(jobsCh chan string, exit *sync.WaitGroup) {
for path := range jobsCh {
ncm.Dump(path, _OW)
exit.Done()
}
}
// TODO: improve error handle
func main() {
var rootPath string
flag.BoolVar(&ncm.MUTE, "m", true, "是否静音执行")
flag.BoolVar(&_OW, "o", false, "是否覆盖已经存在的结果文件")
flag.StringVar(&rootPath, "p", "", "NCM文件所在目录")
flag.Parse()
var exit sync.WaitGroup
concurrency := runtime.NumCPU()
jobsCh := make(chan string, concurrency)
for i := 0; i < concurrency; i++ {
go processJobs(jobsCh, &exit)
}
findDumpJobs(rootPath, jobsCh, &exit)
close(jobsCh)
exit.Wait()
}