Skip to content

Commit

Permalink
Merge pull request tomnomnom#34 from omerxx/master
Browse files Browse the repository at this point in the history
Adding custom engines
  • Loading branch information
tomnomnom authored Jun 18, 2020
2 parents 21e2b81 + a428d41 commit 3016cc0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ becomes simply:
The pattern definitions are stored in `~/.gf` as little JSON files that can be kept under version control:

```
▶ cat ~/.gf/php-sources.json
▶ cat ~/.gf/php-sources.json
{
"flags": "-HnrE",
"pattern": "(\\$_(POST|GET|COOKIE|REQUEST|SERVER|FILES)|php://(input|stdin))"
Expand Down Expand Up @@ -84,6 +84,24 @@ source ~/path/to/gf-completion.zsh
Note: if you're using oh-my-zsh or similar you may find that `gf` is an alias for `git fetch`. You can either
alias the gf binary to something else, or `unalias gf` to remove the `git fetch` alias.

### Using custom engines

There are some amazing code searching engines out there that can be a better replacement for grep.
A good example is [the silver searcher](https://github.com/ggreer/the_silver_searcher).
It's faster (like **way faster**) and presents the results in a more visually digestible manner.
In order to utilize a different engine, add `engine: <other tool>` to the relevant pattern file:
```bash
# Using the silver searcher instead of grep for the aws-keys pattern:
# 1. Adding "ag" engine
# 2. Removing the E flag which is irrelevant for ag
{
"engine": "ag",
"flags": "-Hanr",
"pattern": "([^A-Z0-9]|^)(AKIA|A3T|AGPA|AIDA|AROA|AIPA|ANPA|ANVA|ASIA)[A-Z0-9]{12,}"
}
```
* Note: Different engines use different flags, so in the example above, the flag `E` has to be removed from the `aws-keys.json` file in order for ag to successfully run.


## Install

Expand Down
12 changes: 9 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type pattern struct {
Flags string `json:"flags,omitempty"`
Pattern string `json:"pattern,omitempty"`
Patterns []string `json:"patterns,omitempty"`
Engine string `json:"engine,omitempty"`
}

func main() {
Expand Down Expand Up @@ -93,15 +94,20 @@ func main() {
}

if dumpMode {
fmt.Printf( "grep --color %v %q %v\n", pat.Flags, pat.Pattern, files )
fmt.Printf("grep --color %v %q %v\n", pat.Flags, pat.Pattern, files)
//fmt.Println( "grep --color", pat.Flags, pat.Pattern, files)

} else {
var cmd *exec.Cmd
operator := "grep"
if pat.Engine != "" {
operator = pat.Engine
}

if stdinIsPipe() {
cmd = exec.Command("grep", "--color", pat.Flags, pat.Pattern)
cmd = exec.Command(operator, "--color", pat.Flags, pat.Pattern)
} else {
cmd = exec.Command("grep", "--color", pat.Flags, pat.Pattern, files)
cmd = exec.Command(operator, "--color", pat.Flags, pat.Pattern, files)
}
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
Expand Down

0 comments on commit 3016cc0

Please sign in to comment.