From 1b97db40662b2fe9bd63c6da9898d10427b97d07 Mon Sep 17 00:00:00 2001 From: Andrew Frantz Date: Thu, 19 Dec 2024 11:14:42 -0500 Subject: [PATCH] feat: output dir increments --- .gitignore | 4 +++- src/commands/run.rs | 49 ++++++++++++++++++++++++++++++--------------- test.json | 3 +++ 3 files changed, 39 insertions(+), 17 deletions(-) create mode 100644 test.json diff --git a/.gitignore b/.gitignore index afe9a4b..926fb53 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,6 @@ target/ .vscode/ **/Cargo.lock -rustc-ice* \ No newline at end of file +rustc-ice* + +sprocket_run*/ \ No newline at end of file diff --git a/src/commands/run.rs b/src/commands/run.rs index 0e775cb..781b154 100644 --- a/src/commands/run.rs +++ b/src/commands/run.rs @@ -12,6 +12,7 @@ use anyhow::bail; use clap::Parser; use codespan_reporting::files::SimpleFile; use codespan_reporting::term::emit; +use tracing_log::log; use url::Url; use wdl::analysis::path_to_uri; use wdl::engine::local::LocalTaskExecutionBackend; @@ -129,28 +130,44 @@ pub async fn run(args: RunArgs) -> Result<()> { }; + let output_dir_specified = args.output.is_some(); let output_dir = args .output - .unwrap_or_else(|| Path::new(&name).to_path_buf()); - - // Check to see if the output directory already exists and if it should be - // removed - if output_dir.exists() { - if !args.overwrite { + .unwrap_or_else(|| Path::new(&format!("sprocket_run-{}-0", &name)).to_path_buf()); + + let output_dir = if output_dir.exists() { + if args.overwrite { + fs::remove_dir_all(&output_dir).with_context(|| { + format!( + "failed to remove output directory `{dir}`", + dir = output_dir.display() + ) + })?; + output_dir + } else if output_dir_specified { bail!( - "output directory `{dir}` exists; use the `--overwrite` option to overwrite \ - its contents", + "output directory `{dir}` already exists; use the `--overwrite` option to overwrite it", dir = output_dir.display() ); - } - - fs::remove_dir_all(&output_dir).with_context(|| { - format!( - "failed to remove output directory `{dir}`", + } else { + log::warn!( + "output directory `{dir}` already exists; incrementing the run number", dir = output_dir.display() - ) - })?; - } + ); + let mut run_number: usize = 1; + let mut new_output_dir = output_dir.clone(); + while new_output_dir.exists() { + new_output_dir = output_dir.with_file_name(format!( + "sprocket_run-{}-{}", + &name, run_number + )); + run_number += 1; + } + new_output_dir + } + } else { + output_dir + }; match inputs { Inputs::Task(mut inputs) => { diff --git a/test.json b/test.json new file mode 100644 index 0000000..1ee5478 --- /dev/null +++ b/test.json @@ -0,0 +1,3 @@ +{ + "validate_string_is_12bit_oct_dec_or_hex.number": "05" +}