6
6
"fmt"
7
7
"log"
8
8
"os"
9
+ "strconv"
9
10
"strings"
10
11
11
12
"github.com/jgautheron/goconst"
@@ -24,13 +25,16 @@ Flags:
24
25
-min-occurrences report from how many occurrences (default: 2)
25
26
-match-constant look for existing constants matching the strings
26
27
-numbers search also for duplicated numbers
28
+ -min minimum value, only works with -numbers
29
+ -max maximum value, only works with -numbers
27
30
-output output formatting (text or json)
28
31
29
32
Examples:
30
33
31
34
goconst ./...
32
35
goconst -ignore "yacc|\.pb\." $GOPATH/src/github.com/cockroachdb/cockroach/...
33
36
goconst -min-occurrences 3 -output json $GOPATH/src/github.com/cockroachdb/cockroach
37
+ goconst -numbers -min 60 -max 512 .
34
38
`
35
39
36
40
var (
39
43
flagMinOccurrences = flag .Int ("min-occurrences" , 2 , "report from how many occurrences" )
40
44
flagMatchConstant = flag .Bool ("match-constant" , false , "look for existing constants matching the strings" )
41
45
flagNumbers = flag .Bool ("numbers" , false , "search also for duplicated numbers" )
46
+ flagMin = flag .Int ("min" , 0 , "minimum value, only works with -numbers" )
47
+ flagMax = flag .Int ("max" , 0 , "maximum value, only works with -numbers" )
42
48
flagOutput = flag .String ("output" , "text" , "output formatting" )
43
49
)
44
50
@@ -68,20 +74,30 @@ func main() {
68
74
os .Exit (1 )
69
75
}
70
76
71
- printOutput (strs , consts , * flagOutput , * flagMinOccurrences )
77
+ printOutput (strs , consts , * flagOutput , * flagMinOccurrences , * flagMin , * flagMax )
72
78
}
73
79
74
80
func usage () {
75
81
fmt .Fprintf (os .Stderr , usageDoc )
76
82
os .Exit (1 )
77
83
}
78
84
79
- func printOutput (strs goconst.Strings , consts goconst.Constants , output string , minOccurrences int ) {
80
- // Filter out items whose occurrences don't match the min value
85
+ func printOutput (strs goconst.Strings , consts goconst.Constants , output string , minOccurrences , min , max int ) {
81
86
for str , item := range strs {
87
+ // Filter out items whose occurrences don't match the min value
82
88
if len (item ) < minOccurrences {
83
89
delete (strs , str )
84
90
}
91
+
92
+ // If the value is a number
93
+ if i , err := strconv .Atoi (str ); err == nil {
94
+ if min != 0 && i < min {
95
+ delete (strs , str )
96
+ }
97
+ if max != 0 && i > max {
98
+ delete (strs , str )
99
+ }
100
+ }
85
101
}
86
102
87
103
switch output {
0 commit comments