Skip to content

Commit 04ef6ec

Browse files
committed
Validate regex before starting search
1 parent 7b6156f commit 04ef6ec

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

cmd/jf/main.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io/ioutil"
88
"log"
99
"os"
10+
"regexp"
1011
"strings"
1112

1213
"jsonfind/pkg/scout"
@@ -23,7 +24,7 @@ func main() {
2324
Name: "jf",
2425
Usage: "JSONFind",
2526
UsageText: "jf <valueToFind> <jsonFile>",
26-
Version: "1.1.0",
27+
Version: "1.1.1",
2728
Description: "Search a JSON file for a specified value and output full paths of each occurrence found",
2829
Action: entrypoint,
2930
Flags: []cli.Flag{
@@ -64,7 +65,14 @@ func entrypoint(c *cli.Context) error {
6465
return fmt.Errorf("%v\njf was unable to parse the JSON file", err)
6566
}
6667

67-
scout := scout.New(lookingFor, result, c.Bool(flagUseRegex))
68+
useRegex := c.Bool(flagUseRegex)
69+
if useRegex {
70+
if _, err := regexp.Compile(lookingFor); err != nil {
71+
return fmt.Errorf("regex search term is invalid: %v", lookingFor)
72+
}
73+
}
74+
75+
scout := scout.New(lookingFor, result, useRegex)
6876
found, err := scout.DoSearch()
6977
if err != nil {
7078
return err

0 commit comments

Comments
 (0)