Move creation step preparation to a separate function

This commit is contained in:
Eduard Tolosa 2024-02-05 11:48:32 -05:00
parent 68fb2b0157
commit df68951767
2 changed files with 45 additions and 34 deletions

View file

@ -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 {

View file

@ -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(())
}