Skip to content

Commit beb4690

Browse files
author
XORbit
committed
fix issue #23
1 parent 57e6a30 commit beb4690

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

cmd/root.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ func init() {
4545

4646
rootCmd.Flags().StringP("output", "o", "", "file to export the result (f.json, f.xml, f.txt) / ex: -o result.json")
4747

48-
rootCmd.Flags().StringToString("regexes", map[string]string{}, "regexes to match in each page / ex: --regexes comments=\"\\<\\!--.*?-->\"")
48+
// rootCmd.Flags().StringToString("regexes", map[string]string{}, "regexes to match in each page / ex: --regexes comments=\"\\<\\!--.*?-->\"")
49+
50+
var regexesFlag shared.RegexFlag
51+
rootCmd.Flags().Var(&regexesFlag, "regexes", "regexes to match in each page / ex: --regexes comments=\"\\<\\!--.*?-->\"")
4952

5053
rootCmd.Flags().IntSliceP("exclude-code", "x", []int{}, "status codes to exclude / ex : -x 404,500")
5154

shared/customs.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package shared
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
type RegexFlag struct {
9+
Regexes map[string]string
10+
}
11+
12+
func (r *RegexFlag) String() string {
13+
return fmt.Sprintf("%v", r.Regexes)
14+
}
15+
16+
func (r *RegexFlag) Set(value string) error {
17+
var err error
18+
r.Regexes, err = ParseRegexes(value)
19+
if err != nil {
20+
return err
21+
}
22+
return nil
23+
}
24+
25+
func (r *RegexFlag) Type() string {
26+
return "regex"
27+
}
28+
29+
func (r *RegexFlag) Value() map[string]string {
30+
return r.Regexes
31+
}
32+
33+
func isBetween(s string, a string, b string, index int) bool {
34+
return strings.Count(s[:index], a) == strings.Count(s[index:], b)
35+
}
36+
37+
func ParseRegexes(stream string) (map[string]string, error) {
38+
result := make(map[string]string)
39+
var key, value string
40+
prev := 0
41+
for i, c := range stream {
42+
if c == ',' && !isBetween(stream, "{", "}", i) {
43+
keyValue := strings.Split(stream[prev:i], "=")
44+
if len(keyValue) != 2 {
45+
return nil, fmt.Errorf("invalid regexes format")
46+
}
47+
key = keyValue[0]
48+
value = keyValue[1]
49+
result[key] = value
50+
prev = i + 1
51+
}
52+
}
53+
keyValue := strings.Split(stream[prev:], "=")
54+
if len(keyValue) != 2 {
55+
return nil, fmt.Errorf("invalid regexes format")
56+
}
57+
key = keyValue[0]
58+
value = keyValue[1]
59+
result[key] = value
60+
return result, nil
61+
}

shared/options.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ func ValidateThenBuildOption(cmd *cobra.Command) (*Options, error) {
171171
if err != nil {
172172
return nil, err
173173
}
174-
regexMap, err := cmd.Flags().GetStringToString("regexes")
174+
// regexMap, err := cmd.Flags().GetStringToString("regexes")
175+
regexMap, err := cmd.Flags().Lookup("regexes").Value.(*RegexFlag).Value(), nil
175176
if err != nil {
176177
return nil, err
177178
}

0 commit comments

Comments
 (0)