From 494b0838b41be1703659b3c5850fb965d609fdf5 Mon Sep 17 00:00:00 2001 From: LemonInTheDark <58055496+LemonInTheDark@users.noreply.github.com> Date: Thu, 29 Aug 2024 05:27:46 -0700 Subject: [PATCH] Implements support for list input It is not inconcievable to want to cut more then one directory in one go, particulary in the case of downstreams. I'm not 100% that we should be avoiding just targeting the whole project in cases like ss13, but a list input is pretty easy to implement so may as well. I am considering doing the same thing to the templates string but that is mildly more complex. --- hypnagogic_cli/src/main.rs | 78 ++++++++++++++++++++++++++++---------- 1 file changed, 57 insertions(+), 21 deletions(-) diff --git a/hypnagogic_cli/src/main.rs b/hypnagogic_cli/src/main.rs index 61e43dd..f7e2548 100644 --- a/hypnagogic_cli/src/main.rs +++ b/hypnagogic_cli/src/main.rs @@ -53,8 +53,9 @@ struct Args { /// Location of the templates folder #[arg(short, long, default_value_t = String::from("templates"))] templates: String, - /// Input directory/file - input: String, + /// List of space separated output directory/file(s) + #[arg(num_args = 1.., value_delimiter = ' ', required = true)] + input: Vec, } const VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -96,27 +97,62 @@ fn main() -> Result<()> { tracing::subscriber::set_global_default(subscriber)?; }; - if !Path::new(&input).exists() { - return Err(anyhow!("Input path does not exist!")); - } + let mut invalid_paths: Vec = vec![]; + let mut inaccessible_paths: Vec = vec![]; + let files_to_process: Vec = input + .into_iter() + .filter_map(|potential_path| { + if !Path::new(&potential_path).exists() { + invalid_paths.push(potential_path); + return None; + } - let files_to_process: Vec = if metadata(&input)?.is_file() { - vec![Path::new(&input).to_path_buf()] - } else { - WalkDir::new(&input) - .into_iter() - .filter_map(Result::ok) - .filter(|e| e.file_type().is_file()) - .filter(|e| { - if let Some(extension) = e.path().extension() { - extension == "toml" - } else { - false + let metadata = match metadata(&potential_path) { + Ok(data) => data, + Err(error) => { + inaccessible_paths.push(error); + return None; } - }) - .map(|e| e.into_path()) - .collect() - }; + }; + if metadata.is_file() { + return Some(vec![Path::new(&potential_path).to_path_buf()]); + } + Some( + WalkDir::new(potential_path) + .into_iter() + .filter_map(Result::ok) + .filter(|e| e.file_type().is_file()) + .filter(|e| { + if let Some(extension) = e.path().extension() { + extension == "toml" + } else { + false + } + }) + .map(|e| e.into_path()) + .collect(), + ) + }) + .flatten() + .collect(); + + if !invalid_paths.is_empty() || !inaccessible_paths.is_empty() { + let mut error_text = if !invalid_paths.is_empty() { + format!( + "The input path(s) [{}] do not exist", + invalid_paths.join(", ") + ) + } else { + "".to_string() + }; + if !inaccessible_paths.is_empty() { + error_text = inaccessible_paths + .iter() + .fold(error_text, |acc, elem| format!("{}\n{}", acc, elem)); + } + return Err(anyhow!("{}", error_text)); + } + debug!(files = ?files_to_process, "Files to process"); let num_files = files_to_process.len();