From b74c5c176bc283937845c2fabd1d76c701279bc7 Mon Sep 17 00:00:00 2001 From: grimhilt Date: Sun, 25 Jun 2023 18:26:59 +0200 Subject: [PATCH] check if the user is in a nextsync repo and fail else --- src/commands/clone.rs | 3 +-- src/commands/config.rs | 16 ++----------- src/commands/init.rs | 3 +-- src/commands/push.rs | 8 ------- src/commands/push/new_dir.rs | 2 +- src/commands/reset.rs | 9 +------ src/commands/status.rs | 13 ++++------- src/services/api.rs | 7 +----- src/services/create_folder.rs | 2 +- src/store/head.rs | 36 ++-------------------------- src/store/index.rs | 23 +++--------------- src/store/object.rs | 28 +++++----------------- src/store/object/blob.rs | 5 +--- src/store/object/tree.rs | 5 +--- src/utils/path.rs | 44 +++++++++++++++++++---------------- 15 files changed, 49 insertions(+), 155 deletions(-) diff --git a/src/commands/clone.rs b/src/commands/clone.rs index 9e1a05f..f391730 100644 --- a/src/commands/clone.rs +++ b/src/commands/clone.rs @@ -5,7 +5,6 @@ use std::path::{Path, PathBuf}; use clap::Values; use regex::Regex; use crate::utils::api::ApiProps; -use crate::utils::read::read_folder; use crate::global::global::{DIR_PATH, set_dir_path}; use crate::services::api::ApiError; use crate::services::req_props::{ReqProps, ObjProps}; @@ -61,7 +60,7 @@ pub fn clone(remote: Values<'_>) { .getlastmodified() .send_req_multiple(); - let mut objs = match res { + let objs = match res { Ok(o) => o, Err(ApiError::IncorrectRequest(err)) => { eprintln!("fatal: {}", err.status()); diff --git a/src/commands/config.rs b/src/commands/config.rs index f45103e..dad853a 100644 --- a/src/commands/config.rs +++ b/src/commands/config.rs @@ -3,13 +3,7 @@ use std::io::{self, Write}; use crate::utils::{path, read}; pub fn set(var: &str, val: &str) -> io::Result<()> { - let mut root = match path::nextsync() { - Some(path) => path, - None => { - eprintln!("fatal: not a nextsync repository (or any of the parent directories): .nextsync"); - std::process::exit(1); - } - }; + let mut root = path::nextsync(); root.push("config"); // todo check if exist @@ -28,13 +22,7 @@ pub fn set(var: &str, val: &str) -> io::Result<()> { } pub fn get(var: &str) -> Option { - let mut root = match path::nextsync() { - Some(path) => path, - None => { - eprintln!("fatal: not a nextsync repository (or any of the parent directories): .nextsync"); - std::process::exit(1); - } - }; + let mut root = path::nextsync(); root.push("config"); if let Ok(lines) = read::read_lines(root) { diff --git a/src/commands/init.rs b/src/commands/init.rs index ab794b9..b67d885 100644 --- a/src/commands/init.rs +++ b/src/commands/init.rs @@ -12,6 +12,7 @@ pub fn init() { None => env::current_dir().unwrap(), }; + // check if dir is empty if let Ok(entries) = read_folder(path.clone()) { if entries.len() != 0 { eprintln!("fatal: destination path '{}' already exists and is not an empty directory.", path.display()); @@ -23,9 +24,7 @@ pub fn init() { } let builder = DirBuilder::new(); - // todo check if dir empty - // .nextsync folder path.push(".nextsync"); match builder.create(path.clone()) { Ok(()) => (), diff --git a/src/commands/push.rs b/src/commands/push.rs index dc16610..d3e9d75 100644 --- a/src/commands/push.rs +++ b/src/commands/push.rs @@ -1,12 +1,5 @@ use std::path::PathBuf; -use crate::services::api::ApiError; -use crate::services::upload_file::UploadFile; -use crate::services::delete_path::DeletePath; -use crate::services::req_props::{ReqProps, ObjProps}; -use crate::store::index; -use crate::store::object::blob; use crate::commands::{status, config}; -use crate::commands::status::{State, LocalObj}; use crate::commands::push::push_factory::{PushFactory, PushState}; pub mod push_factory; @@ -27,7 +20,6 @@ pub fn push() { }; let staged_objs = status::get_all_staged(); - // todo sort folder first // path that certify that all its children can be push whithout hesistation // (e.g if remote dir has no changes since last sync all children diff --git a/src/commands/push/new_dir.rs b/src/commands/push/new_dir.rs index 59042fb..20c4b0f 100644 --- a/src/commands/push/new_dir.rs +++ b/src/commands/push/new_dir.rs @@ -5,7 +5,7 @@ use crate::services::create_folder::CreateFolder; use crate::store::index; use crate::store::object::tree; use crate::commands::status::LocalObj; -use crate::commands::push::push_factory::{PushState, PushChange, PushFactory, PushFlowState}; +use crate::commands::push::push_factory::{PushState, PushChange, PushFlowState}; pub struct NewDir { pub obj: LocalObj diff --git a/src/commands/reset.rs b/src/commands/reset.rs index dd4ee84..28102e4 100644 --- a/src/commands/reset.rs +++ b/src/commands/reset.rs @@ -2,14 +2,7 @@ use std::fs::File; use crate::utils; pub fn reset() { - let mut 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); - } - }; - root.push(".nextsync"); + let mut root = utils::path::nextsync(); root.push("index"); if File::create(root).is_err() { eprintln!("fatal: failed to reset"); diff --git a/src/commands/status.rs b/src/commands/status.rs index debf84c..1b48e8c 100644 --- a/src/commands/status.rs +++ b/src/commands/status.rs @@ -92,16 +92,11 @@ fn get_diff() -> (Vec, Vec) { let mut hashes = HashMap::new(); let mut objs: Vec = vec![]; - 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); - } - }; + let root = utils::path::repo_root(); + dbg!(utils::path::current()); - let nextsync_path = utils::path::nextsync().unwrap(); + let nextsync_path = utils::path::nextsync(); let current_p = utils::path::current().unwrap(); let dist_path = current_p.strip_prefix(root.clone()).unwrap().to_path_buf(); @@ -279,7 +274,7 @@ fn remove_duplicate(hashes: &mut HashMap, objects: &mut Vec bool { - path.ends_with(".nextsync") || path.ends_with(".nextsyncignore") + path.ends_with(".nextsync") } fn read_head(mut path: PathBuf) -> io::Result>> { diff --git a/src/services/api.rs b/src/services/api.rs index 81542e0..9436c7c 100644 --- a/src/services/api.rs +++ b/src/services/api.rs @@ -2,7 +2,7 @@ use std::env; use dotenv::dotenv; use reqwest::Client; use reqwest::RequestBuilder; -use reqwest::{Response, Error, IntoUrl, Method}; +use reqwest::{Response, Error, Method}; use crate::utils::api::ApiProps; #[derive(Debug)] @@ -26,11 +26,6 @@ impl ApiBuilder { } } - pub fn set_request(&mut self, method: Method, url: U) -> &mut ApiBuilder { - self.request = Some(self.client.request(method, url)); - self - } - pub fn build_request(&mut self, method: Method, path: &str) -> &mut ApiBuilder { dotenv().ok(); // todo remove env diff --git a/src/services/create_folder.rs b/src/services/create_folder.rs index 4000c97..5de28e3 100644 --- a/src/services/create_folder.rs +++ b/src/services/create_folder.rs @@ -1,4 +1,4 @@ -use reqwest::{Method, IntoUrl, Response, Error}; +use reqwest::{Method, Response, Error}; use crate::services::api::{ApiBuilder, ApiError}; pub struct CreateFolder { diff --git a/src/store/head.rs b/src/store/head.rs index b26a276..3bec52a 100644 --- a/src/store/head.rs +++ b/src/store/head.rs @@ -3,35 +3,8 @@ use std::fs::{File, OpenOptions}; use std::io::{self, Write}; use crate::utils::{read, path}; -pub fn _read_only(mut path: PathBuf) -> File { - path.push("HEAD"); - OpenOptions::new() - .read(true) - .open(path).expect("Cannot open HEAD file") -} - -pub fn _open(mut path: PathBuf) -> File { - path.push("HEAD"); - OpenOptions::new() - .read(true) - .write(true) - .append(true) - .create(true) - .open(path).expect("Cannot open HEAD file") -} - -pub fn _read_line(mut path: PathBuf) -> io::Result>> { - path.push("HEAD"); - read::read_lines(path) -} - pub fn add_line(line: String) -> io::Result<()> { - let mut root = match path::nextsync_root() { - Some(path) => path, - None => todo!(), - }; - - root.push(".nextsync"); + let mut root = path::nextsync(); root.push("HEAD"); let mut file = OpenOptions::new() @@ -45,12 +18,7 @@ pub fn add_line(line: String) -> io::Result<()> { } pub fn rm_line(line: &str) -> io::Result<()> { - let mut root = match path::nextsync_root() { - Some(path) => path, - None => todo!(), - }; - - root.push(".nextsync"); + let mut root = path::nextsync(); root.push("HEAD"); read::rm_line(root, line)?; Ok(()) diff --git a/src/store/index.rs b/src/store/index.rs index 73f264b..720c9c3 100644 --- a/src/store/index.rs +++ b/src/store/index.rs @@ -4,18 +4,8 @@ use std::fs::OpenOptions; use std::path::PathBuf; use crate::utils::{read, path}; -pub fn _read_only(mut path: PathBuf) -> File { - path.push("index"); - OpenOptions::new() - .read(true) - .open(path).expect("Cannot open index file") -} - pub fn open() -> File { - let mut path = match path::nextsync() { - Some(p) => p, - None => todo!(), - }; + let mut path = path::nextsync(); path.push("index"); OpenOptions::new() @@ -27,20 +17,13 @@ pub fn open() -> File { } pub fn read_line() -> io::Result>> { - let mut path = match path::nextsync() { - Some(p) => p, - None => todo!(), - }; + let mut path = path::nextsync(); path.push("index"); read::read_lines(path) } pub fn rm_line(line: &str) -> io::Result<()> { - let mut root = match path::nextsync() { - Some(path) => path, - None => todo!(), - }; - + let mut root = path::nextsync(); root.push("index"); read::rm_line(root, line)?; Ok(()) diff --git a/src/store/object.rs b/src/store/object.rs index 5b43dd1..bb3f6c4 100644 --- a/src/store/object.rs +++ b/src/store/object.rs @@ -37,23 +37,16 @@ fn hash_obj(obj: &str) -> (String, String) { } fn _object_path(obj: &str) -> PathBuf { - let mut root = match path::objects() { - Some(path) => path, - None => todo!(), - }; - + let mut root = path::objects(); let (dir, res) = hash_obj(&obj); + root.push(dir); root.push(res); root } fn rm_node(path: &Path, node: &str) -> io::Result<()> { - let mut root = match path::objects() { - Some(path) => path, - None => todo!(), - }; - + let mut root = path::objects(); let (dir, rest) = hash_obj(path.clone().to_str().unwrap()); root.push(dir); @@ -64,10 +57,7 @@ fn rm_node(path: &Path, node: &str) -> io::Result<()> { } fn add_node(path: &Path, node: &str) -> io::Result<()> { - let mut root = match path::objects() { - Some(path) => path, - None => todo!(), - }; + let mut root = path::objects(); let (dir, rest) = hash_obj(path.clone().to_str().unwrap()); @@ -88,10 +78,7 @@ fn add_node(path: &Path, node: &str) -> io::Result<()> { } fn create_obj(name: String, content: &str) -> io::Result<()> { - let mut root = match path::objects() { - Some(path) => path, - None => todo!(), - }; + let mut root = path::objects(); let c = name.clone(); let (dir, rest) = c.split_at(2); @@ -111,10 +98,7 @@ fn create_obj(name: String, content: &str) -> io::Result<()> { } pub fn get_timestamp(path_s: String) -> Option { - let mut obj_p = match path::objects() { - Some(path) => path, - None => todo!(), - }; + let mut obj_p = path::objects(); let (dir, res) = hash_obj(&path_s); obj_p.push(dir); diff --git a/src/store/object/blob.rs b/src/store/object/blob.rs index 10a18ce..1bb59c6 100644 --- a/src/store/object/blob.rs +++ b/src/store/object/blob.rs @@ -35,10 +35,7 @@ pub fn rm(path: &Path) -> io::Result<()> { } // remove blob object - let mut root = match path::objects() { - Some(path) => path, - None => todo!(), - }; + let mut root = path::objects(); let c = hash.clone(); let (dir, rest) = c.split_at(2); diff --git a/src/store/object/tree.rs b/src/store/object/tree.rs index fe06d4c..a60ca97 100644 --- a/src/store/object/tree.rs +++ b/src/store/object/tree.rs @@ -25,10 +25,7 @@ pub fn add(path: &Path, date: &str) -> io::Result<()> { } pub fn read(tree: String) -> Option<(String, io::Lines>)> { - let mut obj_p = match path::objects() { - Some(path) => path, - None => todo!(), - }; + let mut obj_p = path::objects(); let (dir, res) = hash_obj(&tree); obj_p.push(dir); diff --git a/src/utils/path.rs b/src/utils/path.rs index 83b3abd..a36e997 100644 --- a/src/utils/path.rs +++ b/src/utils/path.rs @@ -23,7 +23,7 @@ pub fn current() -> Option { } } -pub fn nextsync_root() -> Option { +pub fn repo_root_without_err() -> Option { let mut path = current()?; let root = loop { @@ -41,32 +41,36 @@ pub fn nextsync_root() -> Option { root } - -pub fn nextsync() -> Option { - if let Some(mut path) = nextsync_root() { - path.push(".nextsync"); - return Some(path); +pub fn repo_root() -> PathBuf { + match repo_root_without_err() { + Some(p) => p, + None => { + eprintln!("fatal: not a nextsync repository (or any of the parent directories): .nextsync"); + std::process::exit(1); + } } - None } -pub fn objects() -> Option { - if let Some(mut path) = nextsync_root() { +pub fn nextsync() -> PathBuf { + let mut path = repo_root(); path.push(".nextsync"); - path.push("objects"); - return Some(path); - } - None + path +} + +pub fn objects() -> PathBuf { + let mut path = repo_root(); + path.push(".nextsync"); + path.push("objects"); + path } pub fn nextsyncignore() -> Option { - if let Some(mut path) = nextsync_root() { - path.push(".nextsyncignore"); - if path.exists() { - return Some(path); - } else { - return None; - } + let mut path = repo_root(); + path.push(".nextsyncignore"); + if path.exists() { + return Some(path); + } else { + return None; } None }