Switch to dedicated structs for subcommand arguments.

This commit is contained in:
Bauke 2022-11-04 17:54:05 +01:00
parent aed22fad90
commit 9c11ae3db6
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
2 changed files with 34 additions and 21 deletions

View File

@ -2,7 +2,7 @@
use std::path::PathBuf;
use clap::{Parser, Subcommand};
use clap::{Args as Arguments, Parser, Subcommand};
mod run;
@ -26,23 +26,35 @@ pub struct Args {
#[derive(Debug, Subcommand)]
pub enum MainSubcommands {
/// Install Hooked into ".git/hooks".
Install {
/// Overwrite existing files.
#[clap(long)]
overwrite: bool,
},
Install(InstallArgs),
/// Remove installed hooks.
Uninstall {
/// Remove hooks not installed by Hooked.
#[clap(long)]
all: bool,
},
Uninstall(UninstallArgs),
/// Manually run hooks.
Run {
Run(RunArgs),
}
/// The `install` subcommand arguments.
#[derive(Debug, Arguments)]
pub struct InstallArgs {
/// Overwrite existing files.
#[clap(long)]
pub overwrite: bool,
}
/// The `uninstall` subcommand arguments.
#[derive(Debug, Arguments)]
pub struct UninstallArgs {
/// Remove hooks not installed by Hooked.
#[clap(long)]
pub all: bool,
}
/// The `run` subcommand arguments.
#[derive(Debug, Arguments)]
pub struct RunArgs {
/// The hook type to run.
#[clap(value_parser = crate::HOOK_TYPES)]
hook_type: String,
},
pub hook_type: String,
}

View File

@ -38,7 +38,8 @@ fn main() -> Result<()> {
let git_hooks_dir = PathBuf::from(".git/hooks/");
match args.command {
MainSubcommands::Install { overwrite } => {
MainSubcommands::Install(sub_args) => {
let overwrite = sub_args.overwrite;
if !git_hooks_dir.exists() {
return Err(eyre!("The \".git/hooks/\" directory does not exist"));
}
@ -65,7 +66,7 @@ fn main() -> Result<()> {
}
}
MainSubcommands::Uninstall { all } => {
MainSubcommands::Uninstall(sub_args) => {
if !git_hooks_dir.exists() {
return Err(eyre!("The \".git/hooks/\" directory does not exist"));
}
@ -77,7 +78,7 @@ fn main() -> Result<()> {
}
let hook_contents = read_to_string(&hook_path)?;
if all || hook_contents.contains("# Installed by Hooked.") {
if sub_args.all || hook_contents.contains("# Installed by Hooked.") {
remove_file(hook_path)?;
} else {
println!(
@ -88,8 +89,8 @@ fn main() -> Result<()> {
}
}
MainSubcommands::Run { hook_type } => {
cli::hooked_run(config, hook_type)?;
MainSubcommands::Run(sub_args) => {
cli::hooked_run(config, sub_args.hook_type)?;
}
}