From b54bc7e6553c51103b545efa0c647313a5262b34 Mon Sep 17 00:00:00 2001 From: Bauke Date: Wed, 8 Nov 2023 14:51:04 +0100 Subject: [PATCH] Add the directory subcommand. --- source/cli/mod.rs | 21 ++++++++++++++++++++- source/cli/run.rs | 13 ++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/source/cli/mod.rs b/source/cli/mod.rs index 911e737..7448bac 100644 --- a/source/cli/mod.rs +++ b/source/cli/mod.rs @@ -27,6 +27,13 @@ pub enum MainSubcommand { command: ArgumentsSubcommand, }, + /// The directory subcommand. + Directory { + /// The directory subcommands. + #[command(subcommand)] + command: DirectorySubcommand, + }, + /// The file subcommand. File { /// The file subcommands. @@ -61,7 +68,19 @@ pub enum ArgumentsSubcommand { }, } -/// The file subcommands +/// The directory subcommands. +#[derive(Debug, Subcommand)] +pub enum DirectorySubcommand { + /// Check whether a directory exists, if the directory does not exist the exit + /// code will be 1. + Exists { + /// The path to a potential directory. + #[arg()] + directory: PathBuf, + }, +} + +/// The file subcommands. #[derive(Debug, Subcommand)] pub enum FileSubcommand { /// Check whether a file exists, if the file does not exist the exit code will diff --git a/source/cli/run.rs b/source/cli/run.rs index 0055a64..30574ef 100644 --- a/source/cli/run.rs +++ b/source/cli/run.rs @@ -3,7 +3,8 @@ use { crate::{ cli::{ - ArgumentsSubcommand, Cli, FileSubcommand, MainSubcommand::*, Parser, + ArgumentsSubcommand, Cli, DirectorySubcommand, FileSubcommand, + MainSubcommand::*, Parser, }, logging::append_line_to_file, }, @@ -29,6 +30,16 @@ pub fn run() { } }, + Directory { + command: directory_subcommand, + } => match directory_subcommand { + DirectorySubcommand::Exists { directory } => { + if !(directory.exists() && directory.is_dir()) { + std::process::exit(1); + } + } + }, + File { command: file_subcommand, } => match file_subcommand {