Compare commits
9 Commits
54657adaf0
...
4f28ced1b1
Author | SHA1 | Date |
---|---|---|
Bauke | 4f28ced1b1 | |
Bauke | 0743a982d1 | |
Bauke | 16757cabce | |
Bauke | 91a5cc4203 | |
Bauke | c4c45a0368 | |
Bauke | 7338b5667a | |
Bauke | 3f4c8f010f | |
Bauke | 3cf6385d2c | |
Bauke | 599a4bcef2 |
|
@ -4,3 +4,6 @@ target/
|
|||
|
||||
# Code coverage results
|
||||
coverage/
|
||||
|
||||
# mdBook output
|
||||
hooked-book/book/
|
||||
|
|
|
@ -35,6 +35,20 @@ dependencies = [
|
|||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "assert_cmd"
|
||||
version = "2.0.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d5c2ca00549910ec251e3bd15f87aeeb206c9456b9a77b43ff6c97c54042a472"
|
||||
dependencies = [
|
||||
"bstr",
|
||||
"doc-comment",
|
||||
"predicates",
|
||||
"predicates-core",
|
||||
"predicates-tree",
|
||||
"wait-timeout",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "atty"
|
||||
version = "0.2.14"
|
||||
|
@ -88,7 +102,9 @@ version = "0.2.17"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ba3569f383e8f1598449f1a423e72e99569137b47740b1da11ef19af3d5c3223"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
"memchr",
|
||||
"regex-automata",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -314,6 +330,12 @@ version = "0.4.3"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "850878694b7933ca4c9569d30a34b55031b9b139ee1fc7b94a527c4ef960d690"
|
||||
|
||||
[[package]]
|
||||
name = "difflib"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8"
|
||||
|
||||
[[package]]
|
||||
name = "digest"
|
||||
version = "0.10.5"
|
||||
|
@ -324,6 +346,18 @@ dependencies = [
|
|||
"crypto-common",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "doc-comment"
|
||||
version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10"
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
|
||||
|
||||
[[package]]
|
||||
name = "encode_unicode"
|
||||
version = "0.3.6"
|
||||
|
@ -416,14 +450,20 @@ dependencies = [
|
|||
name = "hooked-cli"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"assert_cmd",
|
||||
"clap",
|
||||
"color-eyre",
|
||||
"hooked-library",
|
||||
"hooked-config",
|
||||
"insta",
|
||||
"owo-colors",
|
||||
"subprocess",
|
||||
"supports-color",
|
||||
"tera",
|
||||
"test-case",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hooked-library"
|
||||
name = "hooked-config"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"color-eyre",
|
||||
|
@ -500,6 +540,21 @@ dependencies = [
|
|||
"yaml-rust",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "is_ci"
|
||||
version = "1.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "616cde7c720bb2bb5824a224687d8f77bfd38922027f01d825cd7453be5099fb"
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.10.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
|
||||
dependencies = [
|
||||
"either",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.4"
|
||||
|
@ -722,6 +777,33 @@ version = "0.2.16"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
|
||||
|
||||
[[package]]
|
||||
name = "predicates"
|
||||
version = "2.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a5aab5be6e4732b473071984b3164dbbfb7a3674d30ea5ff44410b6bcd960c3c"
|
||||
dependencies = [
|
||||
"difflib",
|
||||
"itertools",
|
||||
"predicates-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "predicates-core"
|
||||
version = "1.0.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "da1c2388b1513e1b605fcec39a95e0a9e8ef088f71443ef37099fa9ae6673fcb"
|
||||
|
||||
[[package]]
|
||||
name = "predicates-tree"
|
||||
version = "1.0.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4d86de6de25020a36c6d3643a86d9a6a9f552107c0559c60ea03551b5e16c032"
|
||||
dependencies = [
|
||||
"predicates-core",
|
||||
"termtree",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-error"
|
||||
version = "1.0.4"
|
||||
|
@ -805,6 +887,12 @@ dependencies = [
|
|||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-automata"
|
||||
version = "0.1.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132"
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.6.27"
|
||||
|
@ -916,6 +1004,26 @@ version = "0.10.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
|
||||
|
||||
[[package]]
|
||||
name = "subprocess"
|
||||
version = "0.2.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0c2e86926081dda636c546d8c5e641661049d7562a68f5488be4a1f7f66f6086"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "supports-color"
|
||||
version = "1.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4872ced36b91d47bae8a214a683fe54e7078875b399dfa251df346c9b547d1f9"
|
||||
dependencies = [
|
||||
"atty",
|
||||
"is_ci",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.103"
|
||||
|
@ -968,6 +1076,12 @@ dependencies = [
|
|||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "termtree"
|
||||
version = "0.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "507e9898683b6c43a9aa55b64259b721b52ba226e0f3779137e50ad114a4c90b"
|
||||
|
||||
[[package]]
|
||||
name = "test-case"
|
||||
version = "2.2.2"
|
||||
|
@ -1165,6 +1279,15 @@ version = "0.9.4"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
|
||||
|
||||
[[package]]
|
||||
name = "wait-timeout"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9f200f5b12eb75f8c1ed65abd4b2db8a6e1b138a20de009dacee265a2498f3f6"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "walkdir"
|
||||
version = "2.3.2"
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
[workspace]
|
||||
members = [
|
||||
"hooked-cli",
|
||||
"hooked-library"
|
||||
"hooked-config"
|
||||
]
|
||||
|
|
|
@ -40,3 +40,22 @@ args = [
|
|||
"--skip-clean",
|
||||
"--target-dir=target/tarpaulin"
|
||||
]
|
||||
|
||||
[tasks.book]
|
||||
workspace = false
|
||||
install_crate = "mdbook"
|
||||
command = "mdbook"
|
||||
|
||||
[tasks.clean-book]
|
||||
extend = "book"
|
||||
args = ["clean", "hooked-book"]
|
||||
|
||||
[tasks.build-book]
|
||||
extend = "book"
|
||||
dependencies = ["clean-book"]
|
||||
args = ["build", "hooked-book"]
|
||||
|
||||
[tasks.serve-book]
|
||||
extend = "book"
|
||||
dependencies = ["clean-book"]
|
||||
args = ["serve", "hooked-book"]
|
||||
|
|
|
@ -13,12 +13,20 @@ path = "source/main.rs"
|
|||
|
||||
[dependencies]
|
||||
color-eyre = "0.6.2"
|
||||
owo-colors = "3.5.0"
|
||||
subprocess = "0.2.9"
|
||||
supports-color = "1.3.0"
|
||||
tera = "1.17.1"
|
||||
|
||||
[dependencies.clap]
|
||||
features = ["derive"]
|
||||
version = "4.0.18"
|
||||
|
||||
[dependencies.hooked-library]
|
||||
path = "../hooked-library"
|
||||
[dependencies.hooked-config]
|
||||
path = "../hooked-config"
|
||||
version = "0.1.0"
|
||||
|
||||
[dev-dependencies]
|
||||
assert_cmd = "2.0.5"
|
||||
insta = "1.21.0"
|
||||
test-case = "2.2.2"
|
||||
|
|
|
@ -4,6 +4,10 @@ use std::path::PathBuf;
|
|||
|
||||
use clap::{Parser, Subcommand};
|
||||
|
||||
mod run;
|
||||
|
||||
pub use run::hooked_run;
|
||||
|
||||
/// CLI arguments struct using [`clap::Parser`].
|
||||
#[derive(Debug, Parser)]
|
||||
#[clap(about, author, version)]
|
||||
|
@ -34,4 +38,11 @@ pub enum MainSubcommands {
|
|||
#[clap(long)]
|
||||
all: bool,
|
||||
},
|
||||
|
||||
/// Manually run hooks.
|
||||
Run {
|
||||
/// The hook type to run.
|
||||
#[clap(value_parser = crate::HOOK_TYPES)]
|
||||
hook_type: String,
|
||||
},
|
||||
}
|
||||
|
|
|
@ -0,0 +1,84 @@
|
|||
//! The `run` subcommand.
|
||||
|
||||
use std::{io::Read, process::exit};
|
||||
|
||||
use {
|
||||
color_eyre::{eyre::eyre, Result},
|
||||
hooked_config::{Config, ExitAction},
|
||||
owo_colors::{OwoColorize, Style},
|
||||
subprocess::{Exec, Redirection},
|
||||
supports_color::Stream,
|
||||
};
|
||||
|
||||
/// The `run` subcommand.
|
||||
pub fn hooked_run(config: Config, hook_type: String) -> Result<()> {
|
||||
let (success_style, warn_style, error_style) =
|
||||
if let Some(_support) = supports_color::on(Stream::Stdout) {
|
||||
let shared_style = Style::new().bold();
|
||||
(
|
||||
shared_style.green(),
|
||||
shared_style.yellow(),
|
||||
shared_style.red(),
|
||||
)
|
||||
} else {
|
||||
(Style::new(), Style::new(), Style::new())
|
||||
};
|
||||
|
||||
if hook_type == "pre-commit" {
|
||||
println!(
|
||||
"Hooked: Running {} pre-commit hooks.",
|
||||
config.pre_commit.len()
|
||||
);
|
||||
|
||||
for hook in config.pre_commit {
|
||||
let hook_name = hook.name.unwrap_or_else(|| "Unnamed Hook".to_string());
|
||||
|
||||
let command = match (hook.task.command, hook.task.script) {
|
||||
(Some(command), _) => Ok(Exec::shell(command)),
|
||||
|
||||
(None, Some(script_file)) => {
|
||||
let script_path = config.general.directory.join(script_file);
|
||||
let script_path_str = script_path
|
||||
.to_str()
|
||||
.ok_or_else(|| eyre!("Failed to convert path to str"))?;
|
||||
Ok(Exec::shell(script_path_str))
|
||||
}
|
||||
|
||||
(None, None) => Err(eyre!(
|
||||
"No command or script provided for hook: {}",
|
||||
hook_name
|
||||
)),
|
||||
}?;
|
||||
|
||||
let mut process = command
|
||||
.stderr(Redirection::Merge)
|
||||
.stdout(Redirection::Pipe)
|
||||
.popen()?;
|
||||
let exit_status = process.wait()?;
|
||||
let output = {
|
||||
let mut output = String::new();
|
||||
let mut stdout_file = process.stdout.take().unwrap();
|
||||
stdout_file.read_to_string(&mut output)?;
|
||||
output
|
||||
};
|
||||
|
||||
let (stop, print_output, prefix, style) =
|
||||
match (exit_status.success(), hook.on_failure) {
|
||||
(true, _) => (false, false, "✓", success_style),
|
||||
(false, ExitAction::Continue) => (false, true, "⚠", warn_style),
|
||||
(false, ExitAction::Stop) => (true, true, "✗", error_style),
|
||||
};
|
||||
|
||||
println!("\t{} {}", prefix.style(style), hook_name.style(style));
|
||||
if !output.is_empty() && print_output {
|
||||
println!("{}", output);
|
||||
}
|
||||
|
||||
if stop {
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
|
@ -14,7 +14,7 @@ use std::{
|
|||
use {
|
||||
clap::Parser,
|
||||
color_eyre::{eyre::eyre, install, Result},
|
||||
hooked_library::Config,
|
||||
hooked_config::Config,
|
||||
tera::{Context, Tera},
|
||||
};
|
||||
|
||||
|
@ -24,7 +24,7 @@ use crate::cli::{Args, MainSubcommands};
|
|||
pub const DEFAULT_TEMPLATE: &str = include_str!("templates/default.sh");
|
||||
|
||||
/// All supported hook types.
|
||||
pub const HOOK_TYPES: &[&str] = &["pre-commit"];
|
||||
pub const HOOK_TYPES: [&str; 1] = ["pre-commit"];
|
||||
|
||||
mod cli;
|
||||
|
||||
|
@ -35,12 +35,13 @@ fn main() -> Result<()> {
|
|||
let config = Config::from_toml_file(args.config)?;
|
||||
|
||||
let git_hooks_dir = PathBuf::from(".git/hooks/");
|
||||
if !git_hooks_dir.exists() {
|
||||
return Err(eyre!("The \".git/hooks/\" directory does not exist"));
|
||||
}
|
||||
|
||||
match args.command {
|
||||
MainSubcommands::Install { overwrite } => {
|
||||
if !git_hooks_dir.exists() {
|
||||
return Err(eyre!("The \".git/hooks/\" directory does not exist"));
|
||||
}
|
||||
|
||||
for hook_type in HOOK_TYPES {
|
||||
let mut context = Context::new();
|
||||
context.insert("hook_type", hook_type);
|
||||
|
@ -63,6 +64,10 @@ fn main() -> Result<()> {
|
|||
}
|
||||
|
||||
MainSubcommands::Uninstall { all } => {
|
||||
if !git_hooks_dir.exists() {
|
||||
return Err(eyre!("The \".git/hooks/\" directory does not exist"));
|
||||
}
|
||||
|
||||
for hook_type in HOOK_TYPES {
|
||||
let hook_path = git_hooks_dir.join(hook_type);
|
||||
if !hook_path.exists() {
|
||||
|
@ -80,6 +85,10 @@ fn main() -> Result<()> {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
MainSubcommands::Run { hook_type } => {
|
||||
cli::hooked_run(config, hook_type)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
use {assert_cmd::Command, insta::assert_snapshot, test_case::test_case};
|
||||
|
||||
#[test_case("pre-commit", "pre-commit.toml", true; "pre-commit")]
|
||||
fn test_hooked_run(hook_type: &str, config: &str, expect_failure: bool) {
|
||||
let config = format!("tests/samples/{}", config);
|
||||
|
||||
let mut command = Command::cargo_bin("hooked").unwrap();
|
||||
command
|
||||
.args(["run", hook_type, "--config", &config])
|
||||
.env("NO_COLOR", "1");
|
||||
|
||||
let assert = if expect_failure {
|
||||
command.assert().failure().code(1)
|
||||
} else {
|
||||
command.assert().success()
|
||||
};
|
||||
|
||||
let stderr = std::str::from_utf8(&assert.get_output().stderr).unwrap();
|
||||
let stdout = std::str::from_utf8(&assert.get_output().stdout).unwrap();
|
||||
assert_snapshot!(hook_type, format!("{}{}", stdout, stderr));
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
[general]
|
||||
directory = "tests/samples"
|
||||
|
||||
[[pre_commit]]
|
||||
script = "simple-echo.bash"
|
||||
|
||||
[[pre_commit]]
|
||||
name = "Exit 1 Continue"
|
||||
command = "echo 'on_failure=continue' && exit 1"
|
||||
on_failure = "continue"
|
||||
|
||||
[[pre_commit]]
|
||||
name = "Exit 1 Stop"
|
||||
command = "echo 'Hey from stdout!' && echo 'Hey from stderr!' 1>&2 && exit 1"
|
|
@ -0,0 +1,3 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
echo "Hey, $USER!"
|
|
@ -0,0 +1,14 @@
|
|||
---
|
||||
source: hooked-cli/tests/run.rs
|
||||
expression: "format!(\"{}{}\", stdout, stderr)"
|
||||
---
|
||||
Hooked: Running 3 pre-commit hooks.
|
||||
✓ Unnamed Hook
|
||||
⚠ Exit 1 Continue
|
||||
on_failure=continue
|
||||
|
||||
✗ Exit 1 Stop
|
||||
Hey from stdout!
|
||||
Hey from stderr!
|
||||
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "hooked-library"
|
||||
description = "Git hooks manager."
|
||||
name = "hooked-config"
|
||||
description = "Configuration for Hooked."
|
||||
repository = "https://git.bauke.xyz/Holllo/hooked"
|
||||
license = "AGPL-3.0-or-later"
|
||||
version = "0.1.0"
|
|
@ -3,7 +3,7 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Action to take on hook exit.
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[derive(Debug, Deserialize, Eq, PartialEq, Serialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum ExitAction {
|
||||
Continue,
|
|
@ -1,6 +1,6 @@
|
|||
use std::path::PathBuf;
|
||||
|
||||
use {color_eyre::Result, hooked_library::Config};
|
||||
use {color_eyre::Result, hooked_config::Config};
|
||||
|
||||
use {insta::assert_debug_snapshot, test_case::test_case};
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
use {
|
||||
hooked_library::{Config, ExitAction, PreCommit, Task},
|
||||
hooked_config::{Config, ExitAction, PreCommit, Task},
|
||||
toml::to_string_pretty,
|
||||
};
|
||||
|
Loading…
Reference in New Issue