From 9c11ae3db6cc9f6407e0c23eaf178a5314140898 Mon Sep 17 00:00:00 2001 From: Bauke Date: Fri, 4 Nov 2022 17:54:05 +0100 Subject: [PATCH] Switch to dedicated structs for subcommand arguments. --- hooked-cli/source/cli/mod.rs | 44 +++++++++++++++++++++++------------- hooked-cli/source/main.rs | 11 +++++---- 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/hooked-cli/source/cli/mod.rs b/hooked-cli/source/cli/mod.rs index 702d5e4..9dae1c4 100644 --- a/hooked-cli/source/cli/mod.rs +++ b/hooked-cli/source/cli/mod.rs @@ -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 { - /// The hook type to run. - #[clap(value_parser = crate::HOOK_TYPES)] - hook_type: String, - }, + 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)] + pub hook_type: String, } diff --git a/hooked-cli/source/main.rs b/hooked-cli/source/main.rs index 2c859c4..7b4d4e0 100644 --- a/hooked-cli/source/main.rs +++ b/hooked-cli/source/main.rs @@ -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)?; } }