diff --git a/src/commands/add.rs b/src/commands/add.rs index d75cac7..f5f5d89 100644 --- a/src/commands/add.rs +++ b/src/commands/add.rs @@ -23,18 +23,19 @@ pub fn add(files: Values<'_>) { for file in file_vec { let path = Path::new(file); println!("{}", file); - match path.try_exists() { - Ok(true) => { + match path.exists() { + true => { match writeln!(index_file, "{}", path.display()) { Ok(()) => (), Err(err) => eprintln!("{}", err), } }, - Ok(false) => { + false => { + match writeln!(index_file, "{}", path.display()) { + Ok(()) => (), + Err(err) => eprintln!("{}", err), + } // todo can be regex - }, - Err(err) => { - eprintln!("Error: {}", err); } } } diff --git a/src/commands/push.rs b/src/commands/push.rs index 48eb441..9f27214 100644 --- a/src/commands/push.rs +++ b/src/commands/push.rs @@ -2,7 +2,10 @@ use crate::commands::{status, config}; use crate::services::req_props::ReqProps; use crate::services::api::ApiError; use crate::services::upload_file::UploadFile; +use crate::services::delete_path::DeletePath; use crate::commands::status::{State, Obj}; +use crate::store::object::{add_blob, rm_blob}; +use crate::store::index; pub fn push() { dbg!(status::get_all_staged()); @@ -24,6 +27,7 @@ pub fn push() { let push_factory = PushFactory.new(obj.clone()); match push_factory.can_push() { PushState::Valid => push_factory.push(), + PushState::Done => (), _ => todo!(), } } @@ -35,6 +39,7 @@ pub fn push() { #[derive(Debug)] enum PushState { + Done, Valid, Conflict, Error, @@ -77,7 +82,7 @@ impl PushChange for New { // file doesn't exist on remote PushState::Valid } else { - // check date + // todo check date PushState::Conflict } } else { @@ -105,9 +110,82 @@ impl PushChange for New { } _ => (), } - // todo manage err - // todo remove index }); + + // update tree + add_blob(&obj.path.clone(), "todo_date"); + // remove index + index::rm_line(obj.path.to_str().unwrap()); + } +} + + +struct Deleted { + obj: Obj, +} + +impl PushChange for Deleted { + fn can_push(&self) -> PushState { + // check if exist on server + let file_infos = tokio::runtime::Runtime::new().unwrap().block_on(async { + let res = ReqProps::new() + .set_url(&self.obj.path.to_str().unwrap()) + .getlastmodified() + .send_with_err() + .await; + + match res { + Ok(data) => Ok(data), + Err(ApiError::IncorrectRequest(err)) => { + if err.status() == 404 { + Ok(vec![]) + } else { + Err(()) + } + }, + Err(_) => Err(()), + } + }); + + if let Ok(infos) = file_infos { + if infos.len() == 0 { + // file doesn't exist on remote + PushState::Done + } else { + // todo check date + //PushState::Conflict + PushState::Valid + } + } else { + PushState::Error + } + } + + fn push(&self) { + let obj = &self.obj; + tokio::runtime::Runtime::new().unwrap().block_on(async { + let res = DeletePath::new() + .set_url(obj.path.to_str().unwrap()) + .send_with_err() + .await; + + match res { + Err(ApiError::IncorrectRequest(err)) => { + eprintln!("fatal: error deleting file {}: {}", obj.name, err.status()); + std::process::exit(1); + }, + Err(ApiError::RequestError(_)) => { + eprintln!("fatal: request error deleting file {}", obj.name); + std::process::exit(1); + } + _ => (), + } + }); + + // update tree + rm_blob(&obj.path.clone()); + // remove index + index::rm_line(obj.path.to_str().unwrap()); } } @@ -119,7 +197,7 @@ impl PushFactory { State::New => Box::new(New { obj: obj.clone() }), State::Renamed => todo!(), State::Modified => todo!(), - State::Deleted => todo!(), + State::Deleted => Box::new(Deleted { obj: obj.clone() }), State::Default => todo!(), } } diff --git a/src/services/details.rs b/src/services/details.rs deleted file mode 100644 index 24131a7..0000000 --- a/src/services/details.rs +++ /dev/null @@ -1,50 +0,0 @@ -async fn send_propfind_request() -> Result<(), Box> { - dotenv().ok(); - - let mut api_endpoint = env::var("HOST").unwrap().to_owned(); - api_endpoint.push_str("/remote.php/dav/files/"); - let username = env::var("USERNAME").unwrap(); - api_endpoint.push_str(&username); - api_endpoint.push_str("/test"); - let password = env::var("PASSWORD").unwrap(); - - // Create a reqwest client - let client = Client::new(); - - // Create the XML payload - let xml_payload = r#" - - - - - - - - - ) - "#; - - let mut headers = HeaderMap::new(); - headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/xml")); - - - // Send the request - let response = client - .request(reqwest::Method::from_bytes(b"PROPFIND").unwrap(), api_endpoint) - .basic_auth(username, Some(password)) - .headers(headers) - .body(xml_payload) - .send() - .await?; - - // Handle the response - if response.status().is_success() { - let body = response.text().await?; - println!("Response body: {}", body); - } else { - println!("Request failed with status code: {}", response.status()); - } - - Ok(()) -} - diff --git a/src/services/req_props.rs b/src/services/req_props.rs index c78b197..31447fe 100644 --- a/src/services/req_props.rs +++ b/src/services/req_props.rs @@ -1,7 +1,7 @@ use crate::services::api::{ApiBuilder, ApiError}; use xml::reader::{EventReader, XmlEvent}; -use std::io::{self, Cursor}; -use reqwest::{Method, IntoUrl, Response, Error}; +use std::io::Cursor; +use reqwest::{Method, Response, Error}; pub struct ReqProps { api_builder: ApiBuilder, diff --git a/src/services/upload_file.rs b/src/services/upload_file.rs index 970df21..1cfa119 100644 --- a/src/services/upload_file.rs +++ b/src/services/upload_file.rs @@ -1,9 +1,8 @@ -use xml::reader::{EventReader, XmlEvent}; use std::fs::File; use crate::services::api::{ApiBuilder, ApiError}; use std::path::PathBuf; -use std::io::{self, Read}; -use reqwest::{Method, IntoUrl, Response, Error}; +use std::io::{Read}; +use reqwest::{Method, Response, Error}; pub struct UploadFile { api_builder: ApiBuilder,