Skip to content

Commit

Permalink
refactor: 'create_output_dir' func
Browse files Browse the repository at this point in the history
  • Loading branch information
a-frantz committed Dec 19, 2024
1 parent 1b97db4 commit 2db5267
Showing 1 changed file with 53 additions and 39 deletions.
92 changes: 53 additions & 39 deletions src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,58 @@ pub struct RunArgs {
pub report_mode: Mode,
}

/// Creates the output directory for the task.
fn create_output_dir(output_dir: Option<PathBuf>, name: &str, overwrite: bool) -> Result<PathBuf> {
let output_dir_specified = output_dir.is_some();
let output_dir = output_dir
.unwrap_or_else(|| Path::new(&format!("sprocket_run-{}-0", &name)).to_path_buf());

let output_dir = if output_dir.exists() {
if 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}` already exists; use the `--overwrite` option to overwrite it",
dir = output_dir.display()
);
} 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
};

fs::create_dir_all(&output_dir).with_context(|| {
format!(
"failed to create output directory `{dir}`",
dir = output_dir.display()
)
})?;

log::info!("output directory: `{dir}`", dir = output_dir.display());

Ok(output_dir)
}

/// Runs a task.
pub async fn run(args: RunArgs) -> Result<()> {
let file = args.file;
Expand Down Expand Up @@ -129,45 +181,7 @@ pub async fn run(args: RunArgs) -> Result<()> {
(None, name, inputs)
};


let output_dir_specified = args.output.is_some();
let output_dir = args
.output
.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}` already exists; use the `--overwrite` option to overwrite it",
dir = output_dir.display()
);
} 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
};
let output_dir = create_output_dir(args.output, &name, args.overwrite)?;

match inputs {
Inputs::Task(mut inputs) => {
Expand Down

0 comments on commit 2db5267

Please sign in to comment.