From f1724d23f51c5113f37cf372202b87d0085ade53 Mon Sep 17 00:00:00 2001 From: grimhilt Date: Thu, 1 Jun 2023 23:58:23 +0200 Subject: [PATCH] start add command --- src/commands.rs | 1 + src/commands/add.rs | 18 ++++++++++++++++++ src/commands/init.rs | 8 +++++++- src/commands/status.rs | 5 ++--- src/main.rs | 19 ++++++++++++++++++- 5 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 src/commands/add.rs diff --git a/src/commands.rs b/src/commands.rs index 3ed8b62..18af971 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -1,2 +1,3 @@ pub mod init; pub mod status; +pub mod add; diff --git a/src/commands/add.rs b/src/commands/add.rs new file mode 100644 index 0000000..ae28603 --- /dev/null +++ b/src/commands/add.rs @@ -0,0 +1,18 @@ +use clap::Values; +use crate::utils; + +pub fn add(files: Values<'_>) { + let root = match utils::path::nextsync_root() { + Some(path) => path, + None => { + eprintln!("fatal: not a nextsync repository (or any of the parent directories): .nextsync"); + std::process::exit(1); + } + }; + + dbg!(root.clone()); + let file_vec: Vec<&str> = files.collect(); + for file in file_vec { + println!("{}", file); + } +} diff --git a/src/commands/init.rs b/src/commands/init.rs index 8c2cb96..6649596 100644 --- a/src/commands/init.rs +++ b/src/commands/init.rs @@ -18,6 +18,13 @@ pub fn init() { Err(_) => println!("Error: cannot create .nextsyncignore"), } + path.pop(); + path.push("index"); + match File::create(path.clone()) { + Ok(_) => println!("File successfuly created"), + Err(_) => println!("Error: cannot create .nextsyncignore"), + } + path.pop(); path.pop(); path.push(".nextsyncignore"); @@ -26,5 +33,4 @@ pub fn init() { Ok(_) => println!("File successfuly created"), Err(_) => println!("Error: cannot create .nextsyncignore"), } - } diff --git a/src/commands/status.rs b/src/commands/status.rs index 9ef0666..e28d007 100644 --- a/src/commands/status.rs +++ b/src/commands/status.rs @@ -9,9 +9,8 @@ use std::collections::HashSet; use colored::Colorize; use std::fs; -// todo: relative path, filename +// todo: relative path, filename, get modified pub fn status() { - let mut new_files: Vec = Vec::new(); let mut hashes = HashSet::new(); let mut objects: Vec = vec![]; @@ -102,7 +101,7 @@ fn read_folder(path: PathBuf) -> io::Result> { let mut entries = fs::read_dir(path)? .map(|res| res.map(|e| e.path())) .collect::, io::Error>>()?; - + entries.sort(); Ok(entries) } diff --git a/src/main.rs b/src/main.rs index bd1bda9..c89c956 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,8 +6,9 @@ //use std::env; //use dotenv::dotenv; -use clap::{App, SubCommand}; +use clap::{App, Arg, SubCommand}; mod commands; +mod utils; fn main() { let matches = App::new("NextSync") .version("1.0") @@ -15,14 +16,30 @@ fn main() { .about("") .subcommand(SubCommand::with_name("init")) .subcommand(SubCommand::with_name("status")) + .subcommand( + SubCommand::with_name("add") + .arg( + Arg::with_name("files") + .required(true) + .multiple(true) + .takes_value(true) + .value_name("FILE") + .help("Files to add"), + ) + ) .get_matches(); if let Some(_) = matches.subcommand_matches("init") { commands::init::init(); } else if let Some(_) = matches.subcommand_matches("status") { commands::status::status(); + } else if let Some(matches) = matches.subcommand_matches("add") { + if let Some(files) = matches.values_of("files") { + commands::add::add(files); + } } + //tokio::runtime::Runtime::new().unwrap().block_on(async { // if let Err(err) = upload_file("tkt").await { // eprintln!("Error: {}", err);