-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
124 lines (101 loc) · 3.8 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
package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/euforic/todos/todos"
)
func main() {
// Define command line flags
ignores := flag.String("ignore", "", "Comma-separated list of files and directories to ignore")
sortBy := flag.String("sortby", "", "Sort results by field (author, file, line, type, text) to sort descending, postfix with ':desc' (e.g. author:desc)")
commentTypesStr := flag.String("types", "TODO,FIXME", "Comma-separated list of comment types to search for")
searchHidden := flag.Bool("hidden", false, "Search hidden files and directories")
permissive := flag.Bool("permissive", false, "Permissive mode (looser regex, but can match more than intended, strict format is 'TYPE(author): text' where author is optional)")
validateMax := flag.Int("validate-max", 0, "Validate that the number of comments is less than or equal to the max")
outputStyle := flag.String("output", "table", "Output style (table, group, json, md)")
format := flag.String("format", "", "Go template string to use for output style (-output will be ignored if format is set)")
noGitingore := flag.Bool("no-gitignore", false, "Ignore .gitignore file")
flag.Parse()
dir := flag.Arg(0)
if dir == "" {
dir = "."
}
ignoreList := parseIgnoreList(ignores, searchHidden, dir)
if !*noGitingore {
ignorePatterns, err := todos.ParseGitignore(dir)
if err != nil && !os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error())
os.Exit(1)
}
ignoreList = append(ignoreList, ignorePatterns...)
}
commentTypes := strings.Split(*commentTypesStr, ",")
comments, err := todos.Search(dir, commentTypes, ignoreList, *permissive)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error())
os.Exit(1)
}
validateCommentsCount(*validateMax, comments)
sortField, sortDesc := parseSortBy(*sortBy)
formatStr := ""
if *format != "" {
*outputStyle = "format"
formatStr = *format
}
outputComments(*outputStyle, comments, sortField, sortDesc, formatStr)
}
// parseIgnoreList parses the ignore list from the command line
func parseIgnoreList(ignores *string, searchHidden *bool, dir string) []string {
ignoreList := []string{}
if *ignores != "" {
ignoreList = strings.Split(*ignores, ",")
}
if !*searchHidden {
ignoreList = append(ignoreList, ".*")
}
return ignoreList
}
// validateCommentsCount validates that the number of comments is less than or equal to the max
func validateCommentsCount(validateMax int, comments []todos.Comment) {
if validateMax > 0 && len(comments) > validateMax {
fmt.Fprintf(os.Stderr, "Error: %d comments found, max is %d", len(comments), validateMax)
os.Exit(1)
}
}
// parseSortBy parses the sortby flag from the command line
func parseSortBy(sortBy string) (string, bool) {
sortField := ""
sortDesc := false
sortbyParts := strings.Split(sortBy, ":")
if len(sortbyParts) > 0 {
sortField = sortbyParts[0]
}
if len(sortbyParts) > 1 && sortbyParts[1] == "desc" {
sortDesc = true
}
return sortField, sortDesc
}
// outputComments outputs the comments in the specified format
func outputComments(outputStyle string, comments []todos.Comment, sortField string, sortDesc bool, formatStr string) {
var outputErr error
switch outputStyle {
case "table":
outputErr = todos.WriteTable(os.Stdout, comments, sortField, sortDesc)
case "group":
outputErr = todos.WriteFileGroup(os.Stdout, comments, sortField, sortDesc)
case "json":
outputErr = todos.WriteJSON(os.Stdout, comments, sortField, sortDesc)
case "md":
outputErr = todos.WriteMarkdown(os.Stdout, comments, sortField, sortDesc)
case "format":
outputErr = todos.WriteTemplate(os.Stdout, comments, sortField, sortDesc, formatStr)
default:
outputErr = todos.WriteTable(os.Stdout, comments, sortField, sortDesc)
}
if outputErr != nil {
fmt.Fprintf(os.Stderr, "Error: %s", outputErr.Error())
os.Exit(1)
}
}