From da3d605baa8170659401fb27cd3be23f1d939b52 Mon Sep 17 00:00:00 2001 From: grimhilt Date: Sun, 25 Jun 2023 21:27:31 +0200 Subject: [PATCH] remove env variable for remote --- src/commands/add.rs | 1 + src/commands/clone.rs | 13 +++++++++++-- src/commands/status.rs | 31 ++++++++++++++++--------------- src/services/api.rs | 18 +++++++++++------- src/store/head.rs | 3 +-- src/store/index.rs | 1 - src/utils/path.rs | 5 ++--- 7 files changed, 42 insertions(+), 30 deletions(-) diff --git a/src/commands/add.rs b/src/commands/add.rs index 40fe46a..e043508 100644 --- a/src/commands/add.rs +++ b/src/commands/add.rs @@ -12,6 +12,7 @@ pub struct AddArgs<'a> { // todo match deleted files // todo match weird reg expression +// todo normalize path pub fn add(args: AddArgs) { let mut index_file = store::index::open(); let mut added_files: Vec = vec![]; diff --git a/src/commands/clone.rs b/src/commands/clone.rs index f391730..e0b0c28 100644 --- a/src/commands/clone.rs +++ b/src/commands/clone.rs @@ -10,6 +10,7 @@ use crate::services::api::ApiError; use crate::services::req_props::{ReqProps, ObjProps}; use crate::services::download_files::DownloadFiles; use crate::store::object::{tree, blob}; +use crate::commands::config; use crate::commands::init; pub fn clone(remote: Values<'_>) { @@ -77,13 +78,21 @@ pub fn clone(remote: Values<'_>) { Err(ApiError::Unexpected(_)) => todo!() }; - // create folder + // create object if first_iter { + // root folder, init and config if DirBuilder::new().recursive(true).create(ref_path.clone()).is_err() { eprintln!("fatal: unable to create the destination directory"); std::process::exit(1); } else { init::init(); + let mut remote_config = api_props.username.clone(); + remote_config.push_str("@"); + remote_config.push_str(api_props.host.strip_prefix("https://").unwrap()); + remote_config.push_str(&api_props.root); + if config::set("remote", &remote_config).is_err() { + eprintln!("err: not able to save remote"); + } } } else { // create folder @@ -147,7 +156,7 @@ fn download_files(ref_p: PathBuf, files: Vec, api_props: &ApiProps) { } } -fn get_url_props(url: &str) -> (String, Option<&str>, &str) { +pub fn get_url_props(url: &str) -> (String, Option<&str>, &str) { let mut username = None; let mut domain = ""; let mut path = ""; diff --git a/src/commands/status.rs b/src/commands/status.rs index 1b48e8c..28318d1 100644 --- a/src/commands/status.rs +++ b/src/commands/status.rs @@ -5,7 +5,8 @@ use std::collections::{HashSet, HashMap}; use crypto::digest::Digest; use crypto::sha1::Sha1; use colored::Colorize; -use crate::utils; +use crate::utils::path; +use crate::utils::read::{read_folder, read_lines}; use crate::store::object::tree; use crate::store::index; @@ -29,7 +30,6 @@ pub enum State { // todo: not catch added empty folder pub fn status() { let (mut new_objs, mut del_objs) = get_diff(); - dbg!(get_diff()); let mut renamed_objs = get_renamed(&mut new_objs, &mut del_objs); // get copy, modified let mut objs = new_objs; @@ -37,6 +37,7 @@ pub fn status() { objs.append(&mut renamed_objs); let staged_objs = get_staged(&mut objs); print_status(staged_objs, objs); + dbg!(get_all_staged()); } #[derive(Debug, Clone)] @@ -76,8 +77,14 @@ fn get_staged(objs: &mut Vec) -> Vec { } } + dbg!(objs.clone()); + let mut tree_to_analyse: Vec = vec![]; + objs.retain(|obj| { if indexes.contains(obj.clone().path.to_str().unwrap()) { + if obj.clone().otype == String::from("tree") { + tree_to_analyse.push(obj.clone()); + } staged_objs.push(obj.clone()); false } else { @@ -92,24 +99,21 @@ fn get_diff() -> (Vec, Vec) { let mut hashes = HashMap::new(); let mut objs: Vec = vec![]; - let root = utils::path::repo_root(); + let root = path::repo_root(); - - dbg!(utils::path::current()); - let nextsync_path = utils::path::nextsync(); - let current_p = utils::path::current().unwrap(); + let nextsync_path = path::nextsync(); + let current_p = path::current().unwrap(); let dist_path = current_p.strip_prefix(root.clone()).unwrap().to_path_buf(); if let Ok(lines) = read_head(nextsync_path.clone()) { add_to_hashmap(lines, &mut hashes, dist_path.clone()); } - if let Ok(entries) = utils::read::read_folder(root.clone()) { + if let Ok(entries) = read_folder(root.clone()) { add_to_vec(entries, &mut objs, root.clone()); } let mut obj_to_analyse = remove_duplicate(&mut hashes, &mut objs, RemoveSide::Both); - dbg!(obj_to_analyse.clone()); while obj_to_analyse.len() > 0 { let cur_obj = obj_to_analyse.pop().unwrap(); @@ -122,7 +126,7 @@ fn get_diff() -> (Vec, Vec) { add_to_hashmap(lines, &mut hashes, cur_path.clone()); } - if let Ok(entries) = utils::read::read_folder(obj_path.clone()) { + if let Ok(entries) = read_folder(obj_path.clone()) { add_to_vec(entries, &mut objs, root.clone()); } @@ -193,8 +197,6 @@ fn add_to_vec(entries: Vec, objects: &mut Vec, root: PathBuf) { } fn print_status(staged_objs: Vec, objs: Vec) { - dbg!(staged_objs.clone()); - dbg!(objs.clone()); if staged_objs.len() == 0 && objs.len() == 0 { println!("Nothing to push, working tree clean"); return; @@ -279,7 +281,7 @@ fn is_nextsync_config(path: PathBuf) -> bool { fn read_head(mut path: PathBuf) -> io::Result>> { path.push("HEAD"); - utils::read::read_lines(path) + read_lines(path) } #[cfg(test)] @@ -317,8 +319,7 @@ mod tests { objects.push(String::from("file2")); objects.push(String::from("file3")); remove_duplicate(&mut hashes, &mut objects, RemoveSide::Both); - dbg!(hashes.clone()); - dbg!(objects.clone()); + assert_eq!(hashes.contains_key(&hash4), true); assert_eq!(hashes.len(), 1); assert_eq!(objects, vec!["file3"]); diff --git a/src/services/api.rs b/src/services/api.rs index 9436c7c..49fda66 100644 --- a/src/services/api.rs +++ b/src/services/api.rs @@ -4,6 +4,8 @@ use reqwest::Client; use reqwest::RequestBuilder; use reqwest::{Response, Error, Method}; use crate::utils::api::ApiProps; +use crate::commands::config; +use crate::commands::clone::get_url_props; #[derive(Debug)] pub enum ApiError { @@ -27,19 +29,21 @@ impl ApiBuilder { } pub fn build_request(&mut self, method: Method, path: &str) -> &mut ApiBuilder { - dotenv().ok(); - // todo remove env - let host = env::var("HOST").unwrap(); - let username = env::var("USERNAME").unwrap(); - let root = env::var("ROOT").unwrap(); + let remote = match config::get("remote") { + Some(r) => r, + None => { + eprintln!("fatal: unable to find a remote"); + std::process::exit(1); + } + }; + let (host, username, root) = get_url_props(&remote); let mut url = String::from(host); url.push_str("/remote.php/dav/files/"); - url.push_str(&username); + url.push_str(username.unwrap()); url.push_str("/"); url.push_str(&root); url.push_str("/"); url.push_str(path); - dbg!(url.clone()); self.request = Some(self.client.request(method, url)); self } diff --git a/src/store/head.rs b/src/store/head.rs index 3bec52a..0736d14 100644 --- a/src/store/head.rs +++ b/src/store/head.rs @@ -1,5 +1,4 @@ -use std::path::PathBuf; -use std::fs::{File, OpenOptions}; +use std::fs::OpenOptions; use std::io::{self, Write}; use crate::utils::{read, path}; diff --git a/src/store/index.rs b/src/store/index.rs index 720c9c3..9e7f7f2 100644 --- a/src/store/index.rs +++ b/src/store/index.rs @@ -1,7 +1,6 @@ use std::io; use std::fs::File; use std::fs::OpenOptions; -use std::path::PathBuf; use crate::utils::{read, path}; pub fn open() -> File { diff --git a/src/utils/path.rs b/src/utils/path.rs index a36e997..31ffd25 100644 --- a/src/utils/path.rs +++ b/src/utils/path.rs @@ -68,9 +68,8 @@ pub fn nextsyncignore() -> Option { let mut path = repo_root(); path.push(".nextsyncignore"); if path.exists() { - return Some(path); + Some(path) } else { - return None; + None } - None }