Skip to content

Commit

Permalink
Validate regex before starting search
Browse files Browse the repository at this point in the history
  • Loading branch information
WTFox committed May 9, 2022
1 parent 7b6156f commit 04ef6ec
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions cmd/jf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"log"
"os"
"regexp"
"strings"

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

scout := scout.New(lookingFor, result, c.Bool(flagUseRegex))
useRegex := c.Bool(flagUseRegex)
if useRegex {
if _, err := regexp.Compile(lookingFor); err != nil {
return fmt.Errorf("regex search term is invalid: %v", lookingFor)
}
}

scout := scout.New(lookingFor, result, useRegex)
found, err := scout.DoSearch()
if err != nil {
return err
Expand Down

0 comments on commit 04ef6ec

Please sign in to comment.