add init when clone

This commit is contained in:
grimhilt 2023-06-05 19:25:04 +02:00
parent 5b7b75846c
commit 2117ce3016
3 changed files with 27 additions and 23 deletions

View File

@ -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/");

View File

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

View File

@ -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);
// }
//});
}