Compare commits

..

5 Commits

Author SHA1 Message Date
Bauke 06899c42c7
Add an explicit --config to the default hook template. 2022-11-01 13:42:14 +01:00
Bauke 4dab507636
Add config file configuration. 2022-11-01 13:38:52 +01:00
Bauke b386e59c3e
Start using Hooked! 2022-11-01 12:27:55 +01:00
Bauke ac1fb0163c
1 is singular, not 0. 🤦 2022-11-01 12:27:01 +01:00
Bauke d4f22fb0c5
Pluralize words correctly. 2022-10-31 22:05:47 +01:00
9 changed files with 36 additions and 6 deletions

3
Hooked.toml Normal file
View File

@ -0,0 +1,3 @@
[[pre_commit]]
name = "Cargo Complete Check"
command = "cargo make complete-check"

View File

@ -10,6 +10,8 @@ use {
supports_color::Stream, supports_color::Stream,
}; };
use crate::utilities::plural;
/// The `run` subcommand. /// The `run` subcommand.
pub fn hooked_run(config: Config, hook_type: String) -> Result<()> { pub fn hooked_run(config: Config, hook_type: String) -> Result<()> {
let (success_style, warn_style, error_style) = let (success_style, warn_style, error_style) =
@ -25,9 +27,11 @@ pub fn hooked_run(config: Config, hook_type: String) -> Result<()> {
}; };
if hook_type == "pre-commit" { if hook_type == "pre-commit" {
let hook_count = config.pre_commit.len();
println!( println!(
"Hooked: Running {} pre-commit hooks.", "Hooked: Running {} pre-commit {}.",
config.pre_commit.len() hook_count,
plural(hook_count, "hook", None)
); );
for hook in config.pre_commit { for hook in config.pre_commit {

View File

@ -27,6 +27,7 @@ pub const DEFAULT_TEMPLATE: &str = include_str!("templates/default.sh");
pub const HOOK_TYPES: [&str; 1] = ["pre-commit"]; pub const HOOK_TYPES: [&str; 1] = ["pre-commit"];
mod cli; mod cli;
mod utilities;
fn main() -> Result<()> { fn main() -> Result<()> {
install()?; install()?;
@ -44,6 +45,7 @@ fn main() -> Result<()> {
for hook_type in HOOK_TYPES { for hook_type in HOOK_TYPES {
let mut context = Context::new(); let mut context = Context::new();
context.insert("config_path", &config.general.config);
context.insert("hook_type", hook_type); context.insert("hook_type", hook_type);
let hook_path = git_hooks_dir.join(hook_type); let hook_path = git_hooks_dir.join(hook_type);

View File

@ -2,4 +2,4 @@
# Installed by Hooked. # Installed by Hooked.
hooked run {{ hook_type }} hooked run {{ hook_type }} --config {{ config_path }}

View File

@ -0,0 +1,14 @@
//! Miscellaneous utilities.
/// Simple function to create a pluralized string.
pub fn plural(count: usize, singular: &str, plural: Option<&str>) -> String {
if count == 1 {
return singular.to_string();
}
if let Some(plural) = plural {
return plural.to_string();
}
format!("{singular}s")
}

View File

@ -8,6 +8,9 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize)]
#[serde(default, deny_unknown_fields)] #[serde(default, deny_unknown_fields)]
pub struct General { pub struct General {
/// Path to the Hooked configuration file.
pub config: PathBuf,
/// The directory to use for hooks. /// The directory to use for hooks.
pub directory: PathBuf, pub directory: PathBuf,
} }
@ -15,6 +18,7 @@ pub struct General {
impl Default for General { impl Default for General {
fn default() -> Self { fn default() -> Self {
Self { Self {
config: PathBuf::from("Hooked.toml"),
directory: PathBuf::from("hooks"), directory: PathBuf::from("hooks"),
} }
} }

View File

@ -1,9 +1,10 @@
--- ---
source: tests/parsing.rs source: hooked-config/tests/parsing.rs
expression: config expression: config
--- ---
Config { Config {
general: General { general: General {
config: "Hooked.toml",
directory: "hooks", directory: "hooks",
}, },
pre_commit: [ pre_commit: [

View File

@ -1,9 +1,10 @@
--- ---
source: tests/parsing.rs source: hooked-config/tests/parsing.rs
expression: config expression: config
--- ---
Config { Config {
general: General { general: General {
config: "Hooked.toml",
directory: "hooked", directory: "hooked",
}, },
pre_commit: [ pre_commit: [

View File

@ -1,8 +1,9 @@
--- ---
source: tests/serialize.rs source: hooked-config/tests/serialize.rs
expression: to_string_pretty(&config).unwrap() expression: to_string_pretty(&config).unwrap()
--- ---
[general] [general]
config = 'Hooked.toml'
directory = 'hooks' directory = 'hooks'
[[pre_commit]] [[pre_commit]]