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

View File

@ -38,7 +38,8 @@ fn main() -> Result<()> {
let git_hooks_dir = PathBuf::from(".git/hooks/"); let git_hooks_dir = PathBuf::from(".git/hooks/");
match args.command { match args.command {
MainSubcommands::Install { overwrite } => { MainSubcommands::Install(sub_args) => {
let overwrite = sub_args.overwrite;
if !git_hooks_dir.exists() { if !git_hooks_dir.exists() {
return Err(eyre!("The \".git/hooks/\" directory does not exist")); 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() { if !git_hooks_dir.exists() {
return Err(eyre!("The \".git/hooks/\" directory does not exist")); 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)?; 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)?; remove_file(hook_path)?;
} else { } else {
println!( println!(
@ -88,8 +89,8 @@ fn main() -> Result<()> {
} }
} }
MainSubcommands::Run { hook_type } => { MainSubcommands::Run(sub_args) => {
cli::hooked_run(config, hook_type)?; cli::hooked_run(config, sub_args.hook_type)?;
} }
} }