diff --git a/src/main.rs b/src/main.rs index 9ad841d..35d57ce 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,7 @@ -use std::path::Path; - use anyhow::Result; use chrono::Utc; use clap::Parser; -use rusnapshot::{args, controller, structs::ExtraArgs}; +use rusnapshot::{args, controller, structs::ExtraArgs, utils}; fn try_run() -> Result<()> { let mut arguments = args::Args::parse(); @@ -22,37 +20,7 @@ fn try_run() -> Result<()> { }; if arguments.create_snapshot { - arguments.check_for_source_and_dest_dir(); - - arguments.init(&extra_args)?; - - // It's required to have a trailing slash for the source and destination directories. - // Otherwise, when we retrieve the snapshot data from the database, we won't be able to - // restore/delete the snapshot because the source/destination paths won't match. - if !arguments.source_dir.is_empty() && !arguments.source_dir.ends_with('/') { - arguments.source_dir += "/"; - } - if !arguments.dest_dir.is_empty() && !arguments.dest_dir.ends_with('/') { - arguments.dest_dir += "/"; - } - - // We need to make sure that the paths for source and destination are full paths. - // If they are not, we will use the current working directory to build the full path. - if !Path::new(&arguments.source_dir).is_absolute() { - arguments.source_dir = std::env::current_dir()? - .join(&arguments.source_dir) - .to_str() - .expect("Failed to get the source directory") - .to_string(); - } - - if !Path::new(&arguments.dest_dir).is_absolute() { - arguments.dest_dir = std::env::current_dir()? - .join(&arguments.dest_dir) - .to_str() - .expect("Failed to get the destination directory") - .to_string(); - } + utils::check_creation_requirements(&mut arguments, &extra_args)?; } if arguments.create_snapshot { diff --git a/src/utils.rs b/src/utils.rs index 842361c..51bc0e3 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,3 +1,46 @@ +use { + crate::{args::Args, structs::ExtraArgs}, + anyhow::Result, + std::path::Path, +}; + +#[must_use] pub fn is_same_character(s: &str, specific_char: char) -> bool { s.chars().all(|c| c == specific_char) } + +pub fn check_creation_requirements(arguments: &mut Args, extra_args: &ExtraArgs) -> Result<()> { + arguments.check_for_source_and_dest_dir(); + + arguments.init(extra_args)?; + + // It's required to have a trailing slash for the source and destination directories. + // Otherwise, when we retrieve the snapshot data from the database, we won't be able to + // restore/delete the snapshot because the source/destination paths won't match. + if !arguments.source_dir.is_empty() && !arguments.source_dir.ends_with('/') { + arguments.source_dir += "/"; + } + if !arguments.dest_dir.is_empty() && !arguments.dest_dir.ends_with('/') { + arguments.dest_dir += "/"; + } + + // We need to make sure that the paths for source and destination are full paths. + // If they are not, we will use the current working directory to build the full path. + if !Path::new(&arguments.source_dir).is_absolute() { + arguments.source_dir = std::env::current_dir()? + .join(&arguments.source_dir) + .to_str() + .expect("Failed to get the source directory") + .to_string(); + } + + if !Path::new(&arguments.dest_dir).is_absolute() { + arguments.dest_dir = std::env::current_dir()? + .join(&arguments.dest_dir) + .to_str() + .expect("Failed to get the destination directory") + .to_string(); + } + + Ok(()) +}