introduce global variable

This commit is contained in:
grimhilt 2023-06-06 16:51:40 +02:00
parent 457dd32ad8
commit 08a61ee1aa
7 changed files with 47 additions and 4 deletions

1
Cargo.lock generated
View File

@ -523,6 +523,7 @@ dependencies = [
"clap", "clap",
"colored", "colored",
"dotenv", "dotenv",
"lazy_static",
"regex", "regex",
"reqwest", "reqwest",
"rust-crypto", "rust-crypto",

View File

@ -14,3 +14,4 @@ rust-crypto = "0.2.36"
colored = "2.0.0" colored = "2.0.0"
xml-rs = "0.8.0" xml-rs = "0.8.0"
regex = "1.8.3" regex = "1.8.3"
lazy_static = "1.4.0"

View File

@ -12,15 +12,26 @@ use crate::services::list_folders::ListFolders;
use crate::services::download_files::DownloadFiles; use crate::services::download_files::DownloadFiles;
use crate::utils::object; use crate::utils::object;
use crate::commands; use crate::commands;
use crate::global::global::DIR_PATH;
pub fn clone(remote: Values<'_>) { pub fn clone(remote: Values<'_>) {
let d = DIR_PATH.lock().unwrap().clone();
dbg!(d.clone());
let url = remote.clone().next().unwrap(); let url = remote.clone().next().unwrap();
let (domain, tmp_user, path_str) = get_url_props(url); let (domain, tmp_user, path_str) = get_url_props(url);
let path = Path::new(path_str); let path = match d.clone() {
let mut iter_path = path.iter(); 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 / iter_path.next(); // go through the /
let dest_dir = iter_path.next().unwrap(); 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 { let username = match tmp_user {
Some(u) => u, Some(u) => u,
None => { None => {
@ -59,6 +70,7 @@ pub fn clone(remote: Values<'_>) {
// destination path 'path' already exists and is not an empty directory. // destination path 'path' already exists and is not an empty directory.
//std::process::exit(1); //std::process::exit(1);
} else { } else {
dbg!(dest_path.to_str());
commands::init::init(Some(dest_path.to_str().unwrap())); commands::init::init(Some(dest_path.to_str().unwrap()));
} }
} else { } else {

View File

@ -1,9 +1,11 @@
use std::fs::{DirBuilder, File}; use std::fs::{DirBuilder, File};
use std::path::PathBuf; use std::path::PathBuf;
use std::env; use std::env;
use crate::global::global::DIR_PATH;
pub fn init(directory: Option<&str>) { 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), Some(dir) => PathBuf::from(dir),
None => env::current_dir().unwrap(), None => env::current_dir().unwrap(),
}; };

1
src/global.rs Normal file
View File

@ -0,0 +1 @@
pub mod global;

11
src/global/global.rs Normal file
View File

@ -0,0 +1,11 @@
use std::sync::Mutex;
use lazy_static::lazy_static;
lazy_static! {
pub static ref DIR_PATH: Mutex<Option<String>> = Mutex::new(None);
}
pub fn set_dir_path(path: String) {
let mut directory_path = DIR_PATH.lock().unwrap();
*directory_path = Some(path);
}

View File

@ -2,6 +2,8 @@ use clap::{App, Arg, SubCommand};
mod commands; mod commands;
mod utils; mod utils;
mod services; mod services;
mod global;
fn main() { fn main() {
let matches = App::new("NextSync") let matches = App::new("NextSync")
.version("1.0") .version("1.0")
@ -26,6 +28,12 @@ fn main() {
.takes_value(true) .takes_value(true)
.value_name("REMOTE") .value_name("REMOTE")
) )
.arg(
Arg::with_name("directory")
.required(false)
.takes_value(true)
.value_name("DIRECTORY")
)
) )
.subcommand( .subcommand(
SubCommand::with_name("add") SubCommand::with_name("add")
@ -41,6 +49,9 @@ fn main() {
.get_matches(); .get_matches();
if let Some(matches) = matches.subcommand_matches("init") { 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") { match matches.values_of("directory") {
Some(d) => commands::init::init(d.clone().next()), Some(d) => commands::init::init(d.clone().next()),
None => commands::init::init(None), None => commands::init::init(None),
@ -54,8 +65,12 @@ fn main() {
} else if let Some(_) = matches.subcommand_matches("reset") { } else if let Some(_) = matches.subcommand_matches("reset") {
commands::reset::reset(); commands::reset::reset();
} else if let Some(matches) = matches.subcommand_matches("clone") { } 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") { if let Some(remote) = matches.values_of("remote") {
commands::clone::clone(remote); commands::clone::clone(remote);
} }
} }
} }