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. //! The `install` subcommand.
use std::{ use std::{
fs::{set_permissions, write, Permissions}, fs::{read_to_string, set_permissions, write, Permissions},
os::unix::fs::PermissionsExt, os::unix::fs::PermissionsExt,
path::PathBuf, 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")); return Err(eyre!("The \".git/hooks/\" directory does not exist"));
} }
let hooked_directory = config.general.directory;
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("config_path", &config.general.config);
@ -34,10 +35,14 @@ pub fn hooked_install(config: Config, args: InstallArgs) -> Result<()> {
continue; continue;
} }
write( let template = match config.general.template.as_ref() {
&hook_path, Some(template_path) => {
Tera::one_off(DEFAULT_TEMPLATE, &context, false)?, 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))?; set_permissions(hook_path, Permissions::from_mode(0o775))?;
} }

View File

@ -13,6 +13,9 @@ pub struct General {
/// The directory to use for hooks. /// The directory to use for hooks.
pub directory: PathBuf, pub directory: PathBuf,
/// Path to a script template for use with the install subcommand.
pub template: Option<PathBuf>,
} }
impl Default for General { impl Default for General {
@ -20,6 +23,7 @@ impl Default for General {
Self { Self {
config: PathBuf::from("Hooked.toml"), config: PathBuf::from("Hooked.toml"),
directory: PathBuf::from("hooks"), directory: PathBuf::from("hooks"),
template: None,
} }
} }
} }

View File

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

View File

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

View File

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