Refactor subcommands into their own functions.
This commit is contained in:
parent
9c11ae3db6
commit
1a79f9e051
|
@ -0,0 +1,45 @@
|
||||||
|
//! The `install` subcommand.
|
||||||
|
use std::{
|
||||||
|
fs::{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"));
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
write(
|
||||||
|
&hook_path,
|
||||||
|
Tera::one_off(DEFAULT_TEMPLATE, &context, false)?,
|
||||||
|
)?;
|
||||||
|
set_permissions(hook_path, Permissions::from_mode(0o775))?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
|
@ -4,9 +4,13 @@ use std::path::PathBuf;
|
||||||
|
|
||||||
use clap::{Args as Arguments, Parser, Subcommand};
|
use clap::{Args as Arguments, Parser, Subcommand};
|
||||||
|
|
||||||
|
mod install;
|
||||||
mod run;
|
mod run;
|
||||||
|
mod uninstall;
|
||||||
|
|
||||||
|
pub use install::hooked_install;
|
||||||
pub use run::hooked_run;
|
pub use run::hooked_run;
|
||||||
|
pub use uninstall::hooked_uninstall;
|
||||||
|
|
||||||
/// CLI arguments struct using [`clap::Parser`].
|
/// CLI arguments struct using [`clap::Parser`].
|
||||||
#[derive(Debug, Parser)]
|
#[derive(Debug, Parser)]
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
//! The `uninstall` subcommand.
|
||||||
|
|
||||||
|
use std::{
|
||||||
|
fs::{read_to_string, remove_file},
|
||||||
|
path::PathBuf,
|
||||||
|
};
|
||||||
|
|
||||||
|
use {
|
||||||
|
color_eyre::{eyre::eyre, Result},
|
||||||
|
hooked_config::Config,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::{cli::UninstallArgs, HOOK_TYPES};
|
||||||
|
|
||||||
|
/// The `uninstall` subcommand.
|
||||||
|
pub fn hooked_uninstall(_config: Config, args: UninstallArgs) -> Result<()> {
|
||||||
|
let git_hooks_dir = PathBuf::from(".git/hooks/");
|
||||||
|
if !git_hooks_dir.exists() {
|
||||||
|
return Err(eyre!("The \".git/hooks/\" directory does not exist"));
|
||||||
|
}
|
||||||
|
|
||||||
|
for hook_type in HOOK_TYPES {
|
||||||
|
let hook_path = git_hooks_dir.join(hook_type);
|
||||||
|
if !hook_path.exists() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let hook_contents = read_to_string(&hook_path)?;
|
||||||
|
if args.all || hook_contents.contains("# Installed by Hooked.") {
|
||||||
|
remove_file(hook_path)?;
|
||||||
|
} else {
|
||||||
|
println!(
|
||||||
|
"{:?} wasn't installed by Hooked, use --all to remove it",
|
||||||
|
hook_path
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
|
@ -5,17 +5,10 @@
|
||||||
#![forbid(unsafe_code)]
|
#![forbid(unsafe_code)]
|
||||||
#![warn(missing_docs, clippy::missing_docs_in_private_items)]
|
#![warn(missing_docs, clippy::missing_docs_in_private_items)]
|
||||||
|
|
||||||
use std::{
|
|
||||||
fs::{read_to_string, remove_file, set_permissions, write, Permissions},
|
|
||||||
os::unix::fs::PermissionsExt,
|
|
||||||
path::PathBuf,
|
|
||||||
};
|
|
||||||
|
|
||||||
use {
|
use {
|
||||||
clap::Parser,
|
clap::Parser,
|
||||||
color_eyre::{eyre::eyre, install, Result},
|
color_eyre::{install, Result},
|
||||||
hooked_config::Config,
|
hooked_config::Config,
|
||||||
tera::{Context, Tera},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::cli::{Args, MainSubcommands};
|
use crate::cli::{Args, MainSubcommands};
|
||||||
|
@ -35,58 +28,13 @@ fn main() -> Result<()> {
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
let config = Config::from_toml_file(args.config)?;
|
let config = Config::from_toml_file(args.config)?;
|
||||||
|
|
||||||
let git_hooks_dir = PathBuf::from(".git/hooks/");
|
|
||||||
|
|
||||||
match args.command {
|
match args.command {
|
||||||
MainSubcommands::Install(sub_args) => {
|
MainSubcommands::Install(sub_args) => {
|
||||||
let overwrite = sub_args.overwrite;
|
cli::hooked_install(config, sub_args)?;
|
||||||
if !git_hooks_dir.exists() {
|
|
||||||
return Err(eyre!("The \".git/hooks/\" directory does not exist"));
|
|
||||||
}
|
|
||||||
|
|
||||||
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() && !overwrite {
|
|
||||||
println!(
|
|
||||||
"{:?} exists, use --overwrite to replace the existing file",
|
|
||||||
hook_path
|
|
||||||
);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
write(
|
|
||||||
&hook_path,
|
|
||||||
Tera::one_off(DEFAULT_TEMPLATE, &context, false)?,
|
|
||||||
)?;
|
|
||||||
set_permissions(hook_path, Permissions::from_mode(0o775))?;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MainSubcommands::Uninstall(sub_args) => {
|
MainSubcommands::Uninstall(sub_args) => {
|
||||||
if !git_hooks_dir.exists() {
|
cli::hooked_uninstall(config, sub_args)?;
|
||||||
return Err(eyre!("The \".git/hooks/\" directory does not exist"));
|
|
||||||
}
|
|
||||||
|
|
||||||
for hook_type in HOOK_TYPES {
|
|
||||||
let hook_path = git_hooks_dir.join(hook_type);
|
|
||||||
if !hook_path.exists() {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
let hook_contents = read_to_string(&hook_path)?;
|
|
||||||
if sub_args.all || hook_contents.contains("# Installed by Hooked.") {
|
|
||||||
remove_file(hook_path)?;
|
|
||||||
} else {
|
|
||||||
println!(
|
|
||||||
"{:?} wasn't installed by Hooked, use --all to remove it",
|
|
||||||
hook_path
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MainSubcommands::Run(sub_args) => {
|
MainSubcommands::Run(sub_args) => {
|
||||||
|
|
Loading…
Reference in New Issue