diff --git a/src/commands/push.rs b/src/commands/push.rs index d3e9d75..5f20241 100644 --- a/src/commands/push.rs +++ b/src/commands/push.rs @@ -5,11 +5,10 @@ use crate::commands::push::push_factory::{PushFactory, PushState}; pub mod push_factory; pub mod new; pub mod new_dir; +pub mod rm_dir; pub mod deleted; pub fn push() { - dbg!(status::get_all_staged()); - let remote = match config::get("remote") { Some(r) => r, None => { @@ -22,7 +21,7 @@ pub fn push() { let staged_objs = status::get_all_staged(); // 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 + // (e.g. if remote dir has no changes since last sync all children // can be pushed without verification) let mut whitelist: Option = None; diff --git a/src/commands/push/push_factory.rs b/src/commands/push/push_factory.rs index f12cc82..b6c12e8 100644 --- a/src/commands/push/push_factory.rs +++ b/src/commands/push/push_factory.rs @@ -5,6 +5,7 @@ use crate::store::object; use crate::services::req_props::{ObjProps, ReqProps}; use crate::commands::push::new::New; use crate::commands::push::new_dir::NewDir; +use crate::commands::push::rm_dir::RmDir; use crate::commands::push::deleted::Deleted; #[derive(Debug)] @@ -103,7 +104,7 @@ impl PushFactory { State::New => Box::new(NewDir { obj }), State::Renamed => todo!(), State::Modified => todo!(), - State::Deleted => todo!(), + State::Deleted => Box::new(RmDir { obj }), State::Default => todo!(), } } diff --git a/src/commands/push/rm_dir.rs b/src/commands/push/rm_dir.rs new file mode 100644 index 0000000..69cebb6 --- /dev/null +++ b/src/commands/push/rm_dir.rs @@ -0,0 +1,55 @@ +use std::path::PathBuf; +use crate::services::api::ApiError; +use crate::services::delete_path::DeletePath; +use crate::store::index; +use crate::store::object::tree; +use crate::commands::status::LocalObj; +use crate::commands::push::push_factory::{PushState, PushChange, PushFlowState}; + +pub struct RmDir { + pub obj: LocalObj +} + +impl PushChange for RmDir { + fn can_push(&self, whitelist: &mut Option) -> PushState { + match self.flow(&self.obj, whitelist.clone()) { + PushFlowState::Whitelisted => PushState::Done, + PushFlowState::NotOnRemote => { + *whitelist = Some(self.obj.path.clone()); + PushState::Done + }, + PushFlowState::RemoteIsNewer => PushState::Conflict, + PushFlowState::LocalIsNewer => { + *whitelist = Some(self.obj.path.clone()); + PushState::Valid + }, + PushFlowState::Error => PushState::Error, + } + } + + fn push(&self) { + let obj = &self.obj; + let res = DeletePath::new() + .set_url(obj.path.to_str().unwrap()) + .send_with_err(); + + match res { + Err(ApiError::IncorrectRequest(err)) => { + eprintln!("fatal: error deleting dir {}: {}", obj.name, err.status()); + std::process::exit(1); + }, + Err(ApiError::RequestError(_)) => { + eprintln!("fatal: request error deleting dir {}", obj.name); + std::process::exit(1); + } + _ => (), + } + + // update tree + tree::rm(&obj.path.clone()); + // remove index + index::rm_line(obj.path.to_str().unwrap()); + } + + fn conflict(&self) {} +}