Add the general.template option.

This commit is contained in:
Bauke 2022-11-04 19:00:09 +01:00
parent 1a79f9e051
commit 8d1baaf57f
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
5 changed files with 19 additions and 5 deletions

View File

@ -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))?;
}

View File

@ -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<PathBuf>,
}
impl Default for General {
@ -20,6 +23,7 @@ impl Default for General {
Self {
config: PathBuf::from("Hooked.toml"),
directory: PathBuf::from("hooks"),
template: None,
}
}
}

View File

@ -1,5 +1,6 @@
[general]
directory = "hooked"
template = "test.sh"
[[pre_commit]]
name = "Pre Commit 1"

View File

@ -6,6 +6,7 @@ Config {
general: General {
config: "Hooked.toml",
directory: "hooks",
template: None,
},
pre_commit: [
PreCommit {

View File

@ -6,6 +6,9 @@ Config {
general: General {
config: "Hooked.toml",
directory: "hooked",
template: Some(
"test.sh",
),
},
pre_commit: [
PreCommit {