Skip to content

Commit 9740945

Browse files
dnephinjgautheron
authored andcommitted
Support multiple arg paths. (#3)
Signed-off-by: Daniel Nephin <[email protected]>
1 parent 6a7633b commit 9740945

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

cmd/goconst/main.go

+24-16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"flag"
66
"fmt"
7+
"io"
78
"log"
89
"os"
910
"strconv"
@@ -16,7 +17,7 @@ const usageDoc = `goconst: find repeated strings that could be replaced by a con
1617
1718
Usage:
1819
19-
goconst ARGS <directory>
20+
goconst ARGS <directory> [<directory>...]
2021
2122
Flags:
2223
@@ -26,8 +27,8 @@ Flags:
2627
-min-length only report strings with the minimum given length (default: 3)
2728
-match-constant look for existing constants matching the strings
2829
-numbers search also for duplicated numbers
29-
-min minimum value, only works with -numbers
30-
-max maximum value, only works with -numbers
30+
-min minimum value, only works with -numbers
31+
-max maximum value, only works with -numbers
3132
-output output formatting (text or json)
3233
3334
Examples:
@@ -52,17 +53,25 @@ var (
5253

5354
func main() {
5455
flag.Usage = func() {
55-
fmt.Fprint(os.Stderr, usage)
56+
usage(os.Stderr)
5657
}
5758
flag.Parse()
5859
log.SetPrefix("goconst: ")
5960

6061
args := flag.Args()
61-
if len(args) != 1 {
62-
usage()
62+
if len(args) < 1 {
63+
usage(os.Stderr)
64+
os.Exit(1)
65+
}
66+
for _, path := range args {
67+
if err := run(path); err != nil {
68+
log.Println(err)
69+
os.Exit(1)
70+
}
6371
}
64-
path := args[0]
72+
}
6573

74+
func run(path string) error {
6675
gco := goconst.New(
6776
path,
6877
*flagIgnore,
@@ -73,19 +82,17 @@ func main() {
7382
)
7483
strs, consts, err := gco.ParseTree()
7584
if err != nil {
76-
log.Println(err)
77-
os.Exit(1)
85+
return err
7886
}
7987

80-
printOutput(strs, consts, *flagOutput, *flagMinOccurrences, *flagMin, *flagMax)
88+
return printOutput(strs, consts, *flagOutput, *flagMinOccurrences, *flagMin, *flagMax)
8189
}
8290

83-
func usage() {
84-
fmt.Fprintf(os.Stderr, usageDoc)
85-
os.Exit(1)
91+
func usage(out io.Writer) {
92+
fmt.Fprintf(out, usageDoc)
8693
}
8794

88-
func printOutput(strs goconst.Strings, consts goconst.Constants, output string, minOccurrences, min, max int) {
95+
func printOutput(strs goconst.Strings, consts goconst.Constants, output string, minOccurrences, min, max int) error {
8996
for str, item := range strs {
9097
// Filter out items whose occurrences don't match the min value
9198
if len(item) < minOccurrences {
@@ -113,7 +120,7 @@ func printOutput(strs goconst.Strings, consts goconst.Constants, output string,
113120
strs, consts,
114121
})
115122
if err != nil {
116-
log.Fatal(err)
123+
return err
117124
}
118125
case "text":
119126
for str, item := range strs {
@@ -140,8 +147,9 @@ func printOutput(strs goconst.Strings, consts goconst.Constants, output string,
140147
}
141148
}
142149
default:
143-
fmt.Printf(`Unsupported output format: %s`, output)
150+
return fmt.Errorf(`Unsupported output format: %s`, output)
144151
}
152+
return nil
145153
}
146154

147155
func occurrences(item []goconst.ExtendedPos, current goconst.ExtendedPos) string {

0 commit comments

Comments
 (0)