4
4
"encoding/json"
5
5
"flag"
6
6
"fmt"
7
+ "io"
7
8
"log"
8
9
"os"
9
10
"strconv"
@@ -16,7 +17,7 @@ const usageDoc = `goconst: find repeated strings that could be replaced by a con
16
17
17
18
Usage:
18
19
19
- goconst ARGS <directory>
20
+ goconst ARGS <directory> [<directory>...]
20
21
21
22
Flags:
22
23
26
27
-min-length only report strings with the minimum given length (default: 3)
27
28
-match-constant look for existing constants matching the strings
28
29
-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
31
32
-output output formatting (text or json)
32
33
33
34
Examples:
@@ -52,17 +53,25 @@ var (
52
53
53
54
func main () {
54
55
flag .Usage = func () {
55
- fmt . Fprint (os .Stderr , usage )
56
+ usage (os .Stderr )
56
57
}
57
58
flag .Parse ()
58
59
log .SetPrefix ("goconst: " )
59
60
60
61
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
+ }
63
71
}
64
- path := args [ 0 ]
72
+ }
65
73
74
+ func run (path string ) error {
66
75
gco := goconst .New (
67
76
path ,
68
77
* flagIgnore ,
@@ -73,19 +82,17 @@ func main() {
73
82
)
74
83
strs , consts , err := gco .ParseTree ()
75
84
if err != nil {
76
- log .Println (err )
77
- os .Exit (1 )
85
+ return err
78
86
}
79
87
80
- printOutput (strs , consts , * flagOutput , * flagMinOccurrences , * flagMin , * flagMax )
88
+ return printOutput (strs , consts , * flagOutput , * flagMinOccurrences , * flagMin , * flagMax )
81
89
}
82
90
83
- func usage () {
84
- fmt .Fprintf (os .Stderr , usageDoc )
85
- os .Exit (1 )
91
+ func usage (out io.Writer ) {
92
+ fmt .Fprintf (out , usageDoc )
86
93
}
87
94
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 {
89
96
for str , item := range strs {
90
97
// Filter out items whose occurrences don't match the min value
91
98
if len (item ) < minOccurrences {
@@ -113,7 +120,7 @@ func printOutput(strs goconst.Strings, consts goconst.Constants, output string,
113
120
strs , consts ,
114
121
})
115
122
if err != nil {
116
- log . Fatal ( err )
123
+ return err
117
124
}
118
125
case "text" :
119
126
for str , item := range strs {
@@ -140,8 +147,9 @@ func printOutput(strs goconst.Strings, consts goconst.Constants, output string,
140
147
}
141
148
}
142
149
default :
143
- fmt .Printf (`Unsupported output format: %s` , output )
150
+ return fmt .Errorf (`Unsupported output format: %s` , output )
144
151
}
152
+ return nil
145
153
}
146
154
147
155
func occurrences (item []goconst.ExtendedPos , current goconst.ExtendedPos ) string {
0 commit comments