-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
154 lines (123 loc) · 2.85 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"regexp"
)
type Todo struct {
Prefix string
Suffix string
Id *string
Filename string
Line int
}
func (todo Todo) String() string {
// TODO(1): Todo.String doesn't print ID
if todo.Id == nil {
return fmt.Sprintf("%s:%d: %sTODO: %s\n", todo.Filename, todo.Line, todo.Prefix, todo.Suffix)
} else {
return fmt.Sprintf("%s:%d: %sTODO(%s): %s\n", todo.Filename, todo.Line, todo.Prefix, *todo.Id, todo.Suffix)
}
}
func ref_str(x string) *string {
return &x
}
func lineAsUnreportedTodo(line string) *Todo {
unreportedTodo := regexp.MustCompile("^(.*)TODO: (.*)$")
groups := unreportedTodo.FindStringSubmatch(line)
if groups != nil {
return &Todo{
Prefix: groups[1],
Suffix: groups[2],
Id: nil,
Filename: "",
Line: 0,
}
}
return nil
}
func lineAsReportedTodo(line string) *Todo {
reportedTodo := regexp.MustCompile("^(.*)TODO\\((.*)\\): (.*)$")
groups := reportedTodo.FindStringSubmatch(line)
if groups != nil {
return &Todo{
Prefix: groups[1],
Suffix: groups[3],
Id: &groups[2],
Filename: "",
Line: 0,
}
}
return nil
}
func lineAsTodo(line string) *Todo {
// TODO(7): lineAsTodo has false positive result inside of string literals
if todo := lineAsUnreportedTodo(line); todo != nil {
return todo
}
if todo := lineAsReportedTodo(line); todo != nil {
return todo
}
return nil
}
func walkTodosOfFile(path string, visit func(Todo) error) error {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
line := 1
scanner := bufio.NewScanner(file)
for scanner.Scan() {
todo := lineAsTodo(scanner.Text())
if todo != nil {
todo.Filename = path
todo.Line = line
if err := visit(*todo); err != nil {
return err
}
}
line = line + 1
}
return scanner.Err()
}
func walkTodosOfDir(dirpath string, visit func(todo Todo) error) error {
return filepath.Walk(dirpath, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
err := walkTodosOfFile(path, visit)
if err != nil {
return err
}
}
return nil
})
}
func listSubCommand() error {
return walkTodosOfDir(".", func(todo Todo) error {
fmt.Printf("%v", todo)
return nil
})
}
func reportSubCommand() error {
// TODO(5): reportSubCommand not implemented
panic("Report is not implemented.")
return nil
}
func main() {
// TODO: Error results of subcommands are not handled
if len(os.Args) > 1 {
switch os.Args[1] {
case "list":
listSubCommand()
case "report":
reportSubCommand()
default:
panic(fmt.Sprintf("`%s` Unknown Command", os.Args[1]))
}
} else {
// TODO(6): Implement a map for options instead of printing them all
fmt.Printf("fallout [option]\n\tlist: Lists all possible todos of a directory recursively\n\treport: Reports an issue to github\n")
}
}