Skip to content

Commit

Permalink
feature: Basic Functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Periecle committed Sep 24, 2024
1 parent 3877efc commit 3106c09
Show file tree
Hide file tree
Showing 6 changed files with 814 additions and 2 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: CI

on:
push:
branches:
- main
pull_request:

jobs:
build-and-test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]

steps:
- uses: actions/checkout@v3

- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true

- name: Build
run: cargo build --verbose

- name: Run Tests
run: cargo test --verbose

- name: Run Clippy
run: cargo clippy -- -D warnings

- name: Run Formatter Check
run: cargo fmt -- --check
79 changes: 79 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
name: Release

on:
push:
tags:
- "[0-9]+.[0-9]+.[0-9]+"

permissions:
contents: write

jobs:
build-and-upload:
name: Build and upload
runs-on: ${{ matrix.os }}

strategy:
matrix:
# You can add more, for any target you'd like!
include:
- build: linux
os: ubuntu-latest
target: x86_64-unknown-linux-musl

- build: macos
os: macos-latest
target: x86_64-apple-darwin


- build: windows
os: windows-latest
target: x86_64-pc-windows-msvc

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Get the release version from the tag
shell: bash
run: echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV

- name: Install Rust
# Or @nightly if you want
uses: dtolnay/rust-toolchain@stable
# Arguments to pass in
with:
# Make Rust compile to our target (defined in the matrix)
targets: ${{ matrix.target }}

- name: Build
uses: actions-rs/cargo@v1
with:
use-cross: true
command: build
args: --verbose --release --target ${{ matrix.target }}

- name: Build archive
shell: bash
run: |
# Replace with the name of your binary
binary_name="gf"
dirname="$binary_name-${{ env.VERSION }}-${{ matrix.target }}"
mkdir "$dirname"
if [ "${{ matrix.os }}" = "windows-latest" ]; then
mv "target/${{ matrix.target }}/release/$binary_name.exe" "$dirname"
else
mv "target/${{ matrix.target }}/release/$binary_name" "$dirname"
fi
if [ "${{ matrix.os }}" = "windows-latest" ]; then
7z a "$dirname.zip" "$dirname"
echo "ASSET=$dirname.zip" >> $GITHUB_ENV
else
tar -czf "$dirname.tar.gz" "$dirname"
echo "ASSET=$dirname.tar.gz" >> $GITHUB_ENV
fi
- name: Release
uses: softprops/action-gh-release@v1
with:
files: |
${{ env.ASSET }}
30 changes: 30 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
name = "gf"
version = "1.0.0"
authors = ["Roman Kvasnytskyi <[email protected]>"]
edition = "2021"

[dependencies]
clap = { version = "4.5.18", features = ["derive"] }
serde = { version = "1.0.210", features = ["derive"] }
atty = "0.2.14"
serde_json = "1.0.128"
dirs = "5.0.1"
colored = "2.1.0"
anyhow = "1.0.89"


[dev-dependencies]
assert_cmd = "2.0.16"
predicates = "3.1.2"
tempfile = "3.12.0"

[[bin]]
name = "gf"
path = "src/main.rs"
bench = true

[profile.release]
lto = true
codegen-units = 1
panic = "abort"
176 changes: 174 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,174 @@
# gf
Rust reimplementation of tomnomnom gf
# gf - Grep-like Pattern Manager

`gf` is a powerful command-line tool written in Rust for managing and using reusable search patterns with grep-like tools. It allows you to save, list, and execute complex search patterns easily, improving your productivity in tasks like code analysis, security auditing, and text processing.

## Features

- **Save Search Patterns**: Save complex grep patterns with custom flags and reuse them anytime.
- **Pattern Listing**: List all saved patterns for easy reference.
- **Customizable Engines**: Use different search engines like `grep`, `rg` (ripgrep), or `ag` (The Silver Searcher).
- **Pattern Execution**: Execute saved patterns directly on files or piped input.
- **Command Dumping**: Print the full command that would be executed without running it.

## Installation

### Pre-built Binaries

Download the latest release for your platform from the [Releases](https://github.com/Periecle/gf/releases) page and place the binary in your `$PATH`.

### Build from Source

To build `gf` from source, ensure you have Rust and Cargo installed.

```bash
# Clone the repository
git clone https://github.com/Periecle/gf.git
cd gf

# Build the project and install it to PATH
cargo install --path .
```
# Usage

## Basic Usage

## Save a pattern

```bash
gf --save <pattern-name> [--engine <engine>] [flags] <search-pattern>
```

## Use a saved pattern

```bash
gf <pattern-name> [file or directory]
```
## List all saved patterns

```bash
gf --list
```

## Dump the command that would be executed

```bash
gf --dump <pattern-name> [file or directory]
```

# Options

```bash
Usage: gf [OPTIONS] [NAME] [ARGS]...

Pattern manager for grep-like tools

Options:
--save Save a pattern (e.g., gf --save pat-name -Hnri 'search-pattern')
--list List available patterns
--dump Print the command rather than executing it
--engine <ENGINE> Specify the engine to use (e.g., 'grep', 'rg', 'ag')
-h, --help Print help information
-V, --version Print version information

Arguments:
[NAME] The name of the pattern (when saving or using)
[ARGS]... Additional arguments
```

# Examples

## Saving and Using a Pattern

Save a pattern named find-todos to search for TODO comments:

```bash
gf --save find-todos -nri "TODO"
```

Use the saved pattern to search in the current directory:

```bash
gf find-todos
```

## Saving a Pattern with a Custom Engine

Save a pattern using rg (ripgrep) as the search engine:

```bash
gf --save find-errors --engine rg -nri "ERROR"
```

Use the saved pattern:

```bash
gf find-errors /var/log
```

## Listing Saved Patterns

List all saved patterns:

```bash
gf --list
```

## Dumping a Command

Dump the command that would be executed for a pattern:

```bash
gf --dump find-todos src/
```

Output:

```bash
grep -nri "TODO" src/
```

## Executing a Pattern on Piped Input

Use a pattern with piped input:

```bash
cat file.txt | gf find-todos
```

## Saving a Pattern Without Flags
Save a pattern without any flags:

```bash
gf --save simple-search "" "pattern-to-search"
```

## Handling Errors

Attempting to use a non-existent pattern:

```bash
gf nonexistent-pattern
```

Output:

```bash
Error: No such pattern 'nonexistent-pattern'
```

# Original Work
This tool was originally written by [tomnomnom in Go](https://github.com/tomnomnom/gf).


# Contributing
Contributions are welcome! Please follow these steps:

Fork the repository.
Create a new branch with your feature or bug fix.
Commit your changes with clear and descriptive messages.
Push to your branch.
Open a pull request on GitHub.
Please ensure that your code adheres to the existing style and passes all tests.

# License
This project is licensed under the MIT License. See the LICENSE file for details.
Loading

0 comments on commit 3106c09

Please sign in to comment.