diff --git a/hooked-cli/source/cli/install.rs b/hooked-cli/source/cli/install.rs index 0b9ac89..d8582eb 100644 --- a/hooked-cli/source/cli/install.rs +++ b/hooked-cli/source/cli/install.rs @@ -1,6 +1,6 @@ //! The `install` subcommand. use std::{ - fs::{set_permissions, write, Permissions}, + fs::{read_to_string, set_permissions, write, Permissions}, os::unix::fs::PermissionsExt, path::PathBuf, }; @@ -20,6 +20,7 @@ pub fn hooked_install(config: Config, args: InstallArgs) -> Result<()> { return Err(eyre!("The \".git/hooks/\" directory does not exist")); } + let hooked_directory = config.general.directory; for hook_type in HOOK_TYPES { let mut context = Context::new(); context.insert("config_path", &config.general.config); @@ -34,10 +35,14 @@ pub fn hooked_install(config: Config, args: InstallArgs) -> Result<()> { continue; } - write( - &hook_path, - Tera::one_off(DEFAULT_TEMPLATE, &context, false)?, - )?; + let template = match config.general.template.as_ref() { + Some(template_path) => { + read_to_string(hooked_directory.join(template_path))? + } + None => DEFAULT_TEMPLATE.to_string(), + }; + + write(&hook_path, Tera::one_off(&template, &context, false)?)?; set_permissions(hook_path, Permissions::from_mode(0o775))?; } diff --git a/hooked-config/source/config/general.rs b/hooked-config/source/config/general.rs index c8faf1a..4dcfee8 100644 --- a/hooked-config/source/config/general.rs +++ b/hooked-config/source/config/general.rs @@ -13,6 +13,9 @@ pub struct General { /// The directory to use for hooks. pub directory: PathBuf, + + /// Path to a script template for use with the install subcommand. + pub template: Option, } impl Default for General { @@ -20,6 +23,7 @@ impl Default for General { Self { config: PathBuf::from("Hooked.toml"), directory: PathBuf::from("hooks"), + template: None, } } } diff --git a/hooked-config/tests/parsing/with-hooks.toml b/hooked-config/tests/parsing/with-hooks.toml index 166bcfc..d166d7a 100644 --- a/hooked-config/tests/parsing/with-hooks.toml +++ b/hooked-config/tests/parsing/with-hooks.toml @@ -1,5 +1,6 @@ [general] directory = "hooked" +template = "test.sh" [[pre_commit]] name = "Pre Commit 1" diff --git a/hooked-config/tests/snapshots/parsing__defaults.snap b/hooked-config/tests/snapshots/parsing__defaults.snap index 526c179..32fec24 100644 --- a/hooked-config/tests/snapshots/parsing__defaults.snap +++ b/hooked-config/tests/snapshots/parsing__defaults.snap @@ -6,6 +6,7 @@ Config { general: General { config: "Hooked.toml", directory: "hooks", + template: None, }, pre_commit: [ PreCommit { diff --git a/hooked-config/tests/snapshots/parsing__with-hooks.snap b/hooked-config/tests/snapshots/parsing__with-hooks.snap index 883d3a4..8150b40 100644 --- a/hooked-config/tests/snapshots/parsing__with-hooks.snap +++ b/hooked-config/tests/snapshots/parsing__with-hooks.snap @@ -6,6 +6,9 @@ Config { general: General { config: "Hooked.toml", directory: "hooked", + template: Some( + "test.sh", + ), }, pre_commit: [ PreCommit {