From 08a61ee1aaa809263880afb3da344799b6192829 Mon Sep 17 00:00:00 2001 From: grimhilt Date: Tue, 6 Jun 2023 16:51:40 +0200 Subject: [PATCH] introduce global variable --- Cargo.lock | 1 + Cargo.toml | 1 + src/commands/clone.rs | 18 +++++++++++++++--- src/commands/init.rs | 4 +++- src/global.rs | 1 + src/global/global.rs | 11 +++++++++++ src/main.rs | 15 +++++++++++++++ 7 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 src/global.rs create mode 100644 src/global/global.rs diff --git a/Cargo.lock b/Cargo.lock index aae0cb0..48c5d72 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -523,6 +523,7 @@ dependencies = [ "clap", "colored", "dotenv", + "lazy_static", "regex", "reqwest", "rust-crypto", diff --git a/Cargo.toml b/Cargo.toml index a852e37..197a05f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,3 +14,4 @@ rust-crypto = "0.2.36" colored = "2.0.0" xml-rs = "0.8.0" regex = "1.8.3" +lazy_static = "1.4.0" diff --git a/src/commands/clone.rs b/src/commands/clone.rs index ded411d..1f05a98 100644 --- a/src/commands/clone.rs +++ b/src/commands/clone.rs @@ -12,15 +12,26 @@ use crate::services::list_folders::ListFolders; use crate::services::download_files::DownloadFiles; use crate::utils::object; use crate::commands; +use crate::global::global::DIR_PATH; pub fn clone(remote: Values<'_>) { + let d = DIR_PATH.lock().unwrap().clone(); + + dbg!(d.clone()); let url = remote.clone().next().unwrap(); let (domain, tmp_user, path_str) = get_url_props(url); - let path = Path::new(path_str); - let mut iter_path = path.iter(); + let path = match d.clone() { + Some(dd) => Path::new(&dd).to_owned(), + None => Path::new(path_str).to_owned(), + }; + let mut iter_path = Path::new(path_str.clone()).iter(); iter_path.next(); // go through the / let dest_dir = iter_path.next().unwrap(); - let dest_path = std::env::current_dir().unwrap().join(dest_dir); + let dest_path = match d.clone() { + Some(dd) => Path::new(&dd).to_owned(), + None => std::env::current_dir().unwrap().join(dest_dir), + }; + dbg!((path.clone(), dest_path.clone(), dest_dir.clone())); let username = match tmp_user { Some(u) => u, None => { @@ -59,6 +70,7 @@ pub fn clone(remote: Values<'_>) { // destination path 'path' already exists and is not an empty directory. //std::process::exit(1); } else { + dbg!(dest_path.to_str()); commands::init::init(Some(dest_path.to_str().unwrap())); } } else { diff --git a/src/commands/init.rs b/src/commands/init.rs index 402e5db..4597ca6 100644 --- a/src/commands/init.rs +++ b/src/commands/init.rs @@ -1,9 +1,11 @@ use std::fs::{DirBuilder, File}; use std::path::PathBuf; use std::env; +use crate::global::global::DIR_PATH; pub fn init(directory: Option<&str>) { - let mut path = match directory { + let d = DIR_PATH.lock().unwrap(); + let mut path = match d.clone() { Some(dir) => PathBuf::from(dir), None => env::current_dir().unwrap(), }; diff --git a/src/global.rs b/src/global.rs new file mode 100644 index 0000000..cdcd83a --- /dev/null +++ b/src/global.rs @@ -0,0 +1 @@ +pub mod global; diff --git a/src/global/global.rs b/src/global/global.rs new file mode 100644 index 0000000..cbf9451 --- /dev/null +++ b/src/global/global.rs @@ -0,0 +1,11 @@ +use std::sync::Mutex; +use lazy_static::lazy_static; + +lazy_static! { + pub static ref DIR_PATH: Mutex> = Mutex::new(None); +} + +pub fn set_dir_path(path: String) { + let mut directory_path = DIR_PATH.lock().unwrap(); + *directory_path = Some(path); +} diff --git a/src/main.rs b/src/main.rs index 728bb92..185a0c7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,8 @@ use clap::{App, Arg, SubCommand}; mod commands; mod utils; mod services; +mod global; + fn main() { let matches = App::new("NextSync") .version("1.0") @@ -26,6 +28,12 @@ fn main() { .takes_value(true) .value_name("REMOTE") ) + .arg( + Arg::with_name("directory") + .required(false) + .takes_value(true) + .value_name("DIRECTORY") + ) ) .subcommand( SubCommand::with_name("add") @@ -41,6 +49,9 @@ fn main() { .get_matches(); if let Some(matches) = matches.subcommand_matches("init") { + if let Some(val) = matches.values_of("directory") { + global::global::set_dir_path(String::from(val.clone().next().unwrap())); + } match matches.values_of("directory") { Some(d) => commands::init::init(d.clone().next()), None => commands::init::init(None), @@ -54,8 +65,12 @@ fn main() { } else if let Some(_) = matches.subcommand_matches("reset") { commands::reset::reset(); } else if let Some(matches) = matches.subcommand_matches("clone") { + if let Some(val) = matches.values_of("directory") { + global::global::set_dir_path(String::from(val.clone().next().unwrap())); + } if let Some(remote) = matches.values_of("remote") { commands::clone::clone(remote); } } } +