mirror of
https://github.com/edu4rdshl/rusnapshot.git
synced 2026-07-17 23:24:55 +00:00
Move creation step preparation to a separate function
This commit is contained in:
parent
68fb2b0157
commit
df68951767
2 changed files with 45 additions and 34 deletions
36
src/main.rs
36
src/main.rs
|
|
@ -1,9 +1,7 @@
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use chrono::Utc;
|
use chrono::Utc;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use rusnapshot::{args, controller, structs::ExtraArgs};
|
use rusnapshot::{args, controller, structs::ExtraArgs, utils};
|
||||||
|
|
||||||
fn try_run() -> Result<()> {
|
fn try_run() -> Result<()> {
|
||||||
let mut arguments = args::Args::parse();
|
let mut arguments = args::Args::parse();
|
||||||
|
|
@ -22,37 +20,7 @@ fn try_run() -> Result<()> {
|
||||||
};
|
};
|
||||||
|
|
||||||
if arguments.create_snapshot {
|
if arguments.create_snapshot {
|
||||||
arguments.check_for_source_and_dest_dir();
|
utils::check_creation_requirements(&mut arguments, &extra_args)?;
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if arguments.create_snapshot {
|
if arguments.create_snapshot {
|
||||||
|
|
|
||||||
43
src/utils.rs
43
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 {
|
pub fn is_same_character(s: &str, specific_char: char) -> bool {
|
||||||
s.chars().all(|c| c == specific_char)
|
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(())
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue