-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
113 lines (95 loc) · 1.77 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
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
package main
import (
"flag"
"go/parser"
"go/token"
"log"
"os"
"path/filepath"
"strings"
"sync"
. "github.com/diontr00/go-dochl/internal"
)
var (
// keys word to look up
fset = token.NewFileSet()
kwflag = flag.String("keys", "todo,fixme,bug,hack", "keywords to be extracted")
)
type dochl struct {
wg sync.WaitGroup
}
func (d *dochl) parseFile(path string) error {
f, err := parser.ParseFile(fset, path, nil, parser.ParseComments)
if err != nil {
NewError(path, err)
}
for _, c := range f.Comments {
for _, ci := range c.List {
NewComment(fset, ci).Parse()
}
}
return nil
}
func (d *dochl) parseDir(path string) {
files, err := os.ReadDir(path)
if err != nil {
NewError(path, err)
return
}
for _, file := range files {
if file.IsDir() {
subpath := filepath.Join(path, file.Name())
d.wg.Add(1)
go func(subpath string) {
defer d.wg.Done()
d.parseDir(subpath)
}(subpath)
}
}
f, err := parser.ParseDir(fset, path, nil, parser.ParseComments)
if err != nil {
NewError(path, err)
return
}
for _, pkg := range f {
for _, file := range pkg.Files {
for _, c := range file.Comments {
for _, ci := range c.List {
NewComment(fset, ci).Parse()
}
}
}
}
return
}
func main() {
flag.Parse()
keywords := strings.Split(*kwflag, ",")
if len(keywords) > 0 {
SetKeyWords(keywords)
}
log.SetFlags(0)
log.SetOutput(os.Stdout)
d := &dochl{wg: sync.WaitGroup{}}
if len(flag.Args()) == 0 {
d.parseDir(".")
os.Exit(0)
}
for _, path := range flag.Args() {
d.wg.Add(1)
go func(path string) {
defer d.wg.Done()
stat, err := os.Stat(path)
if err != nil {
NewError(path, err)
return
}
if stat.IsDir() {
d.parseDir(path)
} else {
d.parseFile(path)
}
}(path)
}
d.wg.Wait()
}