Skip to content

Commit

Permalink
feat: add setup-hook subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtnvgr committed Mar 22, 2024
1 parent ab53a65 commit 0901e74
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ Check PR and issue URLs state

## Usage

Check all files:

```bash
grawler check
```

Setup a pre-commit hook:

```bash
grawler setup-hook
```

You can specify tokens for increased rate limits:

```bash
Expand All @@ -24,5 +32,4 @@ cargo install grawler

## TODO

- [ ] `grawler setup-hooks`
- [ ] Template for Github Workflows
39 changes: 39 additions & 0 deletions src/hook.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use git2::Repository;
use std::{
fs::{self, File},
io::Write,
os::unix::fs::PermissionsExt,
};

static HOOK: &str = "#!/bin/sh\nexec grawler check";

pub fn setup() {
eprintln!("Creating a git pre-commit hook...");

let hooks_dir = Repository::discover(".")
.map(|x| x.path().join("hooks"))
.unwrap();

fs::create_dir_all(&hooks_dir).expect("Failed to create a dir for hooks");

let hook_path = hooks_dir.join("pre-commit");

if hook_path.exists() {
log::warn!("The pre-commit hook already exists");
}

let hook_bytes = HOOK.as_bytes();
let mut hook = File::create(&hook_path).expect("Failed to create a hook");
hook.write_all(hook_bytes)
.expect("Failed to write to a hook");

let mut perms = hook
.metadata()
.map(|x| x.permissions())
.expect("Failed to get permissions from hook");
perms.set_mode(0o755);

fs::set_permissions(hook_path, perms).expect("Failed to set new perms");

log::info!("Success! :)");
}
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ use clap::Parser;
use git2::Repository;

mod check;
mod hook;

#[derive(Parser)]
enum Commands {
#[command(name = "check", about = "perform checks on urls in files")]
Check(Check),
#[command(name = "setup-hook", about = "set up a git pre-commit hook")]
SetupHook,
}

#[derive(Parser)]
Expand All @@ -31,5 +34,6 @@ async fn main() {

match command {
Commands::Check(args) => check::perform(args).await,
Commands::SetupHook => hook::setup(),
}
}

0 comments on commit 0901e74

Please sign in to comment.