Skip to content

Commit 1ad1d8c

Browse files
committed
some renaming, cleanup
1 parent 8c58656 commit 1ad1d8c

File tree

4 files changed

+62
-17
lines changed

4 files changed

+62
-17
lines changed

cmd/commands/todos.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,18 @@ var todosCmd = &cobra.Command{
2626
dir, err = filepath.Rel(cwd, args[0])
2727
handleError(err)
2828
}
29+
found := make([]*todos.TODO, 0)
2930
err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
3031
if !info.IsDir() {
3132
p, err := filepath.Rel(dir, path)
3233
handleError(err)
33-
_, err = todos.SearchFile(p)
34+
t, err := todos.SearchFile(p)
3435
handleError(err)
35-
// if len(c) > 0 {
36-
// fmt.Println(c)
37-
// }
36+
found = append(found, t...)
3837
}
3938
return nil
4039
})
4140
handleError(err)
41+
todos.WriteTodos(found, os.Stdout)
4242
},
4343
}

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ github.com/apparentlymart/go-textseg v1.0.0/go.mod h1:z96Txxhf3xSFMPmb5X/1W05FF/
1111
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
1212
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
1313
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
14-
github.com/augmentable-dev/lege v0.0.0-20191022024440-1583f8f6972a h1:CNfPUBMEyYvH1yJ/pGkLIisZ65y+EHStYPN72OIy9dk=
15-
github.com/augmentable-dev/lege v0.0.0-20191022024440-1583f8f6972a/go.mod h1:DtuvAW6+SE9e44O6eLaMJp8PFiadmk6NfXslCKYCiB0=
1614
github.com/augmentable-dev/lege v0.0.0-20191023021623-869a91bc405e h1:geKUZgzslZzV3VHxeMCwRhS4Zs/ocu2mTer4+dB3094=
1715
github.com/augmentable-dev/lege v0.0.0-20191023021623-869a91bc405e/go.mod h1:DtuvAW6+SE9e44O6eLaMJp8PFiadmk6NfXslCKYCiB0=
1816
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=

pkg/todos/report.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package todos
2+
3+
import (
4+
"io"
5+
"text/template"
6+
)
7+
8+
// DefaultTemplate is the default report template
9+
const DefaultTemplate = `
10+
{{- range . }}
11+
=== 📋 {{ .String }}
12+
--- {{ .FilePath }}:{{ .Line }}:{{ .Position }}
13+
{{- else }}
14+
no todos 🎉
15+
{{- end }}
16+
`
17+
18+
// WriteTodos renders a report of todos
19+
func WriteTodos(todos []*TODO, writer io.Writer) error {
20+
21+
t, err := template.New("todos").Parse(DefaultTemplate)
22+
if err != nil {
23+
return err
24+
}
25+
26+
t.Execute(writer, todos)
27+
28+
return nil
29+
}

pkg/todos/todos.go

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package todos
22

33
import (
44
"bytes"
5-
"fmt"
65
"io/ioutil"
76
"path/filepath"
87
"strings"
@@ -13,12 +12,12 @@ import (
1312

1413
// CStyleCommentOptions ...
1514
var CStyleCommentOptions *lege.ParseOptions = &lege.ParseOptions{
16-
BoundaryOptions: []lege.BoundaryOption{
17-
lege.BoundaryOption{
15+
Boundaries: []lege.Boundary{
16+
lege.Boundary{
1817
Starts: []string{"//"},
1918
Ends: []string{"\n"},
2019
},
21-
lege.BoundaryOption{
20+
lege.Boundary{
2221
Starts: []string{"/*"},
2322
Ends: []string{"*/"},
2423
},
@@ -27,8 +26,8 @@ var CStyleCommentOptions *lege.ParseOptions = &lege.ParseOptions{
2726

2827
// HashStyleCommentOptions ...
2928
var HashStyleCommentOptions *lege.ParseOptions = &lege.ParseOptions{
30-
BoundaryOptions: []lege.BoundaryOption{
31-
lege.BoundaryOption{
29+
Boundaries: []lege.Boundary{
30+
lege.Boundary{
3231
Starts: []string{"#"},
3332
Ends: []string{"\n"},
3433
},
@@ -51,8 +50,17 @@ var LanguageParseOptions map[Language]*lege.ParseOptions = map[Language]*lege.Pa
5150
"PHP": CStyleCommentOptions,
5251
}
5352

54-
// SearchFile ...
55-
func SearchFile(filePath string) ([]*lege.Collection, error) {
53+
// ToDo represents a ToDo item
54+
type ToDo struct {
55+
FilePath string
56+
Line int
57+
Position int
58+
String string
59+
}
60+
61+
// SearchFile searches a file for comments. It infers the language
62+
func SearchFile(filePath string) ([]*ToDo, error) {
63+
todos := make([]*ToDo, 0)
5664
src, err := ioutil.ReadFile(filePath)
5765
if err != nil {
5866
return nil, err
@@ -61,7 +69,10 @@ func SearchFile(filePath string) ([]*lege.Collection, error) {
6169
if enry.IsVendor(filePath) {
6270
return nil, nil
6371
}
64-
options := LanguageParseOptions[lang]
72+
options, ok := LanguageParseOptions[lang]
73+
if !ok {
74+
return nil, nil
75+
}
6576
commentParser, err := lege.NewParser(options)
6677
if err != nil {
6778
return nil, err
@@ -78,8 +89,15 @@ func SearchFile(filePath string) ([]*lege.Collection, error) {
7889
}
7990
s = strings.Replace(comment.String(), "TODO", "", 1)
8091
s = strings.Trim(s, " ")
81-
fmt.Printf("%q\n", s)
92+
// fmt.Printf("%q\n", s)
93+
todo := &ToDo{
94+
FilePath: filePath,
95+
Line: comment.StartLocation.Line,
96+
Position: comment.StartLocation.Pos,
97+
String: s,
98+
}
99+
todos = append(todos, todo)
82100
}
83101

84-
return comments, nil
102+
return todos, nil
85103
}

0 commit comments

Comments
 (0)