hooked/hooked-cli/source/cli/install.rs

51 lines
1.4 KiB
Rust
Raw Normal View History

//! The `install` subcommand.
use std::{
2022-11-04 18:00:09 +00:00
fs::{read_to_string, set_permissions, write, Permissions},
os::unix::fs::PermissionsExt,
path::PathBuf,
};
use {
color_eyre::{eyre::eyre, Result},
hooked_config::Config,
tera::{Context, Tera},
};
use crate::{cli::InstallArgs, DEFAULT_TEMPLATE, HOOK_TYPES};
/// The `install` subcommand.
pub fn hooked_install(config: Config, args: InstallArgs) -> Result<()> {
let git_hooks_dir = PathBuf::from(".git/hooks/");
if !git_hooks_dir.exists() {
return Err(eyre!("The \".git/hooks/\" directory does not exist"));
}
2022-11-04 18:00:09 +00:00
let hooked_directory = config.general.directory;
for hook_type in HOOK_TYPES {
let mut context = Context::new();
context.insert("config_path", &config.general.config);
context.insert("hook_type", hook_type);
let hook_path = git_hooks_dir.join(hook_type);
if hook_path.exists() && !args.overwrite {
println!(
"{:?} exists, use --overwrite to replace the existing file",
hook_path
);
continue;
}
2022-11-04 18:00:09 +00:00
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))?;
}
Ok(())
}