From 2117ce30165731b3f3b027027f24927b406dd420 Mon Sep 17 00:00:00 2001 From: grimhilt Date: Mon, 5 Jun 2023 19:25:04 +0200 Subject: [PATCH] add init when clone --- src/commands/clone.rs | 5 ++++- src/commands/init.rs | 12 +++++++++--- src/main.rs | 33 ++++++++++++++------------------- 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/src/commands/clone.rs b/src/commands/clone.rs index 071251c..72350bf 100644 --- a/src/commands/clone.rs +++ b/src/commands/clone.rs @@ -10,6 +10,7 @@ use xml::reader::{EventReader, XmlEvent}; use crate::services::api::ApiError; use crate::services::list_folders::ListFolders; use crate::services::download_files::DownloadFiles; +use crate::commands; pub fn clone(remote: Values<'_>) { let url = remote.clone().next().unwrap(); @@ -49,7 +50,9 @@ pub fn clone(remote: Values<'_>) { // todo add second parameter to save in a folder eprintln!("fatal: directory already exist"); // destination path 'path' already exists and is not an empty directory. - //std::process::exit(1); + std::process::exit(1); + } else { + commands::init::init(Some(env::current_dir().unwrap().to_str().unwrap())); } } else { let mut path = Path::new(&folder).strip_prefix("/remote.php/dav/files/"); diff --git a/src/commands/init.rs b/src/commands/init.rs index 6649596..7dff619 100644 --- a/src/commands/init.rs +++ b/src/commands/init.rs @@ -1,16 +1,22 @@ use std::fs::{DirBuilder, File}; +use std::path::PathBuf; +use clap::Values; use std::env; -pub fn init() { +pub fn init(directory: Option<&str>) { + let mut path = match directory { + Some(dir) => PathBuf::from(dir), + None => env::current_dir().unwrap(), + }; let builder = DirBuilder::new(); - let mut path = env::current_dir().unwrap(); + // todo check if dir empty // .nextsync folder path.push(".nextsync"); match builder.create(path.clone()) { Ok(()) => println!("Directory successfuly created"), Err(_) => println!("Error: cannot create directory"), - } + }; path.push("HEAD"); match File::create(path.clone()) { diff --git a/src/main.rs b/src/main.rs index 0091515..728bb92 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,3 @@ -//use reqwest::Client; -//use std::fs::File; -//use std::io::Read; -//use reqwest::header::{HeaderValue, CONTENT_TYPE, HeaderMap}; -//use std::error::Error; -//use std::env; -//use dotenv::dotenv; - use clap::{App, Arg, SubCommand}; mod commands; mod utils; @@ -15,7 +7,15 @@ fn main() { .version("1.0") .author("grimhilt") .about("") - .subcommand(SubCommand::with_name("init")) + .subcommand( + SubCommand::with_name("init") + .arg( + Arg::with_name("directory") + .required(false) + .takes_value(true) + .value_name("DIRECTORY") + ) + ) .subcommand(SubCommand::with_name("status")) .subcommand(SubCommand::with_name("reset")) .subcommand( @@ -40,8 +40,11 @@ fn main() { ) .get_matches(); - if let Some(_) = matches.subcommand_matches("init") { - commands::init::init(); + if let Some(matches) = matches.subcommand_matches("init") { + match matches.values_of("directory") { + Some(d) => commands::init::init(d.clone().next()), + None => commands::init::init(None), + } } else if let Some(_) = matches.subcommand_matches("status") { commands::status::status(); } else if let Some(matches) = matches.subcommand_matches("add") { @@ -55,12 +58,4 @@ fn main() { commands::clone::clone(remote); } } - - - - //tokio::runtime::Runtime::new().unwrap().block_on(async { - // if let Err(err) = upload_file("tkt").await { - // eprintln!("Error: {}", err); - // } - //}); }