Skip to content

Commit f80ef25

Browse files
committed
some general refactoring
1 parent eef7662 commit f80ef25

File tree

5 files changed

+26
-18
lines changed

5 files changed

+26
-18
lines changed

cmd/commands/todos.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,9 @@ var todosCmd = &cobra.Command{
5151
commit, err := r.CommitObject(ref.Hash())
5252
handleError(err)
5353

54-
comments, err := comments.SearchCommit(commit)
54+
comments, err := comments.SearchDir(dir)
5555
handleError(err)
5656

57-
// comments, err := comments.SearchDir(dir)
58-
// handleError(err)
59-
6057
t := todos.NewToDos(comments)
6158

6259
ctx := context.Background()
@@ -67,7 +64,6 @@ var todosCmd = &cobra.Command{
6764
total := len(t)
6865
s.Suffix = fmt.Sprintf(" (%d/%d) %s: %s", total-remaining, total, commit.Hash, commit.Author.When)
6966
})
70-
7167
sort.Sort(&t)
7268

7369
handleError(err)

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ require (
1313
github.com/spf13/cobra v0.0.5
1414
github.com/spf13/pflag v1.0.5 // indirect
1515
github.com/src-d/enry/v2 v2.1.0
16+
github.com/stretchr/testify v1.4.0 // indirect
1617
golang.org/x/crypto v0.0.0-20191029031824-8986dd9e96cf // indirect
1718
golang.org/x/net v0.0.0-20191028085509-fe3aa8a45271 // indirect
1819
golang.org/x/sys v0.0.0-20191029155521-f43be2a4598c // indirect
1920
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
2021
gopkg.in/src-d/go-billy.v4 v4.3.2
2122
gopkg.in/src-d/go-git.v4 v4.13.1
23+
gopkg.in/yaml.v2 v2.2.4 // indirect
2224
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoH
114114
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
115115
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
116116
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
117+
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
118+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
117119
github.com/toqueteos/trie v1.0.0 h1:8i6pXxNUXNRAqP246iibb7w/pSFquNTQ+uNfriG7vlk=
118120
github.com/toqueteos/trie v1.0.0/go.mod h1:Ywk48QhEqhU1+DwhMkJ2x7eeGxDHiGkAdc9+0DYcbsM=
119121
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
@@ -173,3 +175,5 @@ gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
173175
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
174176
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
175177
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
178+
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
179+
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

pkg/comments/comments.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ type Comment struct {
7070
}
7171

7272
// SearchFile searches a file for comments. It infers the language
73-
func SearchFile(filePath string, reader io.ReadCloser) (Comments, error) {
73+
func SearchFile(filePath string, reader io.Reader) (Comments, error) {
7474
src, err := ioutil.ReadAll(reader)
7575
if err != nil {
7676
return nil, err
@@ -120,7 +120,7 @@ func SearchDir(dirPath string) (Comments, error) {
120120
if matched {
121121
return nil
122122
}
123-
if !info.IsDir() {
123+
if info.Mode().IsRegular() {
124124
p, err := filepath.Abs(path)
125125
if err != nil {
126126
return err
@@ -129,6 +129,7 @@ func SearchDir(dirPath string) (Comments, error) {
129129
if err != nil {
130130
return err
131131
}
132+
defer f.Close()
132133
t, err := SearchFile(p, f)
133134
if err != nil {
134135
return err

pkg/todos/todos.go

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

33
import (
44
"context"
5+
"fmt"
56
"regexp"
67
"strings"
78
"sync"
@@ -34,6 +35,10 @@ type Author struct {
3435
When time.Time
3536
}
3637

38+
func (a *Author) String() string {
39+
return fmt.Sprintf("%s <%s>", a.Name, a.Email)
40+
}
41+
3742
// ToDos represents a list of ToDo items
3843
type ToDos []*ToDo
3944

@@ -72,30 +77,30 @@ func NewToDos(comments comments.Comments) ToDos {
7277
}
7378

7479
// Len returns the number of todos
75-
func (t *ToDos) Len() int {
76-
return len(*t)
80+
func (t ToDos) Len() int {
81+
return len(t)
7782
}
7883

7984
// Less compares two todos by their creation time
80-
func (t *ToDos) Less(i, j int) bool {
81-
first := (*t)[i]
82-
second := (*t)[j]
85+
func (t ToDos) Less(i, j int) bool {
86+
first := t[i]
87+
second := t[j]
8388
if first.Commit == nil || second.Commit == nil {
8489
return false
8590
}
8691
return first.Commit.Author.When.Before(second.Commit.Author.When)
8792
}
8893

8994
// Swap swaps two todoss
90-
func (t *ToDos) Swap(i, j int) {
91-
temp := (*t)[i]
92-
(*t)[i] = (*t)[j]
93-
(*t)[j] = temp
95+
func (t ToDos) Swap(i, j int) {
96+
temp := t[i]
97+
t[i] = t[j]
98+
t[j] = temp
9499
}
95100

96101
// CountWithCommits returns the number of todos with an associated commit (in which that todo was added)
97-
func (t *ToDos) CountWithCommits() (count int) {
98-
for _, todo := range *t {
102+
func (t ToDos) CountWithCommits() (count int) {
103+
for _, todo := range t {
99104
if todo.Commit != nil {
100105
count++
101106
}

0 commit comments

Comments
 (0)