Skip to content

Commit

Permalink
Treat 2nd arg as a keyword to search content (same as --grep option)
Browse files Browse the repository at this point in the history
  • Loading branch information
bayashi committed Mar 4, 2024
1 parent 3d7a5f6 commit d07c189
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 15 deletions.
54 changes: 44 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,63 @@
# xfg

Do `find` paths by a keyword, and also search for contents like a `grep` in one command.
Do `find` paths by a keyword, and also search for contents like a `grep` in one command, gracefully.

<a href="/~https://github.com/bayashi/xfg/actions" title="xfg CI"><img src="/~https://github.com/bayashi/xfg/workflows/main/badge.svg" alt="xfg CI"></a>
<a href="https://goreportcard.com/report/github.com/bayashi/xfg" title="xfg report card" target="_blank"><img src="https://goreportcard.com/badge/github.com/bayashi/xfg" alt="xfg report card"></a>
<a href="https://pkg.go.dev/github.com/bayashi/xfg" title="Go xfg package reference" target="_blank"><img src="https://pkg.go.dev/badge/github.com/bayashi/xfg.svg" alt="Go Reference: xfg"></a>

## Usage of `xfg` command

Search for directories and files that include `service-a` in those path.
For example, there are directories and files like below:

```go
$ xfg service-a
```
.
├── service-a
│   └── main.go
├── service-b
│   └── main.go
└── service-c
└── main.go
```

Search for directories and files that include `service-b` in those path.

```sh
$ xfg service-b
```

Specific:

```sh
$ xfg --path service-b
```

Output:

Search for directories and files that match the `service-a` in those path and extract content that matches the `memory`.
```
$ xfg service-b
service-b
service-b/main.go
```

Search for directories and files that match the `service-b` in those path and extract content that matches the `bar`.

```sh
$ xfg service-b bar
```

Specific:

```go
$ xfg service-a --grep memory
```
$ xfg --path service-b --grep bar
```

`xfg` is the shorthand and enhancement of below one-liner.
Output:

```go
find . -type f | grep "service-a" | xargs grep "memory" -n
```
service-b
service-b/main.go
4: bar := 34
```

## Notes
Expand Down
9 changes: 5 additions & 4 deletions arg.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,15 @@ func (o *options) targetPathFromArgs(cli *runner) {
cli.putHelp(errNeedToSetPath)
}

arg := flag.Args()[0]
if arg != "-" && arg != "--" {
o.searchPath = arg
}
o.searchPath = flag.Args()[0]

if o.searchPath == "" {
cli.putHelp(errNeedToSetPath)
}

if len(flag.Args()) == 2 {
o.searchGrep = flag.Args()[1]
}
}

func versionDetails() string {
Expand Down
2 changes: 1 addition & 1 deletion cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (cli *runner) putErr(message ...interface{}) {
}

func (cli *runner) putUsage() {
cli.putErr(fmt.Sprintf("Usage: %s [OPTIONS]", cmdName))
cli.putErr(fmt.Sprintf("Usage: %s [SEARCH_PATH_KEYWORD] [SEARCH_CONTENT_KEYWORD] [OPTIONS]", cmdName))
}

func (cli *runner) putHelp(message string) {
Expand Down

0 comments on commit d07c189

Please sign in to comment.