push modification

This commit is contained in:
grimhilt
2023-08-24 20:59:41 +02:00
parent f64d719b31
commit 498fada9ec
8 changed files with 254 additions and 30 deletions

View File

@@ -6,6 +6,7 @@ use clap::Values;
use regex::Regex;
use crate::services::downloader::Downloader;
use crate::utils::api::ApiProps;
use crate::utils::path::path_buf_to_string;
use crate::utils::remote::{enumerate_remote, EnumerateOptions};
use crate::global::global::{DIR_PATH, set_dir_path};
use crate::services::api::ApiError;
@@ -46,7 +47,7 @@ pub fn clone(args: CloneArgs) {
let iter = Path::new(dist_path_str).iter();
let dest_dir = iter.last().unwrap();
let lp = std::env::current_dir().unwrap().join(dest_dir);
set_dir_path(lp.to_str().unwrap().to_string());
set_dir_path(path_buf_to_string(lp.clone()));
lp
},
};
@@ -93,7 +94,7 @@ pub fn clone(args: CloneArgs) {
let downloader = Downloader::new()
.set_api_props(api_props.clone())
.set_files(files)
.should_log()
//.should_log()
.download(ref_path.clone(), Some(&save_blob));
}

View File

@@ -1,12 +1,16 @@
use std::path::PathBuf;
use crate::commands::{status, config};
use crate::commands::push::push_factory::{PushFactory, PushState};
use crate::store::index;
use super::status::LocalObj;
pub mod push_factory;
pub mod new;
pub mod new_dir;
pub mod rm_dir;
pub mod deleted;
pub mod modified;
pub fn push() {
// todo err when pushing new folder
@@ -52,20 +56,24 @@ pub fn push() {
match push_factory.can_push(&mut whitelist) {
PushState::Valid => {
match push_factory.push() {
Ok(()) => (),
Ok(()) => remove_obj_from_index(obj.clone()),
Err(err) => {
eprintln!("err: pushing {}: {}", obj.name, err);
}
}
},
PushState::Done => (),
PushState::Done => remove_obj_from_index(obj.clone()),
PushState::Conflict => {
// download file
}
_ => todo!(),
PushState::Error => (),
}
}
}
// read index
// if dir upload dir
}
fn remove_obj_from_index(obj: LocalObj) {
if let Err(err) = index::rm_line(obj.path.to_str().unwrap()) {
eprintln!("err: removing {} from index: {}", obj.name, err);
}
}

View File

@@ -0,0 +1,79 @@
use std::path::PathBuf;
use std::io;
use crate::services::api::ApiError;
use crate::services::req_props::ReqProps;
use crate::services::upload_file::UploadFile;
use crate::commands::status::LocalObj;
use crate::commands::push::push_factory::{PushState, PushChange, PushFlowState};
use crate::store::object::blob::Blob;
pub struct Modified {
pub obj: LocalObj,
}
impl PushChange for Modified {
fn can_push(&self, whitelist: &mut Option<PathBuf>) -> PushState {
match self.flow(&self.obj, whitelist.clone()) {
PushFlowState::Whitelisted => PushState::Done,
PushFlowState::NotOnRemote => PushState::Valid,
PushFlowState::RemoteIsNewer => PushState::Conflict,
PushFlowState::LocalIsNewer => PushState::Valid,
PushFlowState::Error => PushState::Error,
}
}
fn push(&self) -> io::Result<()> {
let obj = &self.obj;
let res = UploadFile::new()
.set_url(obj.path.to_str().unwrap())
.set_file(obj.path.clone())
.send_with_err();
match res {
Err(ApiError::IncorrectRequest(err)) => {
eprintln!("fatal: error pushing file {}: {}", obj.name, err.status());
std::process::exit(1);
},
Err(ApiError::RequestError(_)) => {
eprintln!("fatal: request error pushing file {}", obj.name);
std::process::exit(1);
}
_ => (),
}
// get lastmodified props to update it
let props = ReqProps::new()
.set_url(obj.path.to_str().unwrap())
.getlastmodified()
.send_req_single();
let prop = match props {
Ok(o) => o,
Err(ApiError::IncorrectRequest(err)) => {
eprintln!("fatal: {}", err.status());
std::process::exit(1);
},
Err(ApiError::EmptyError(_)) => {
eprintln!("Failed to get body");
std::process::exit(1);
}
Err(ApiError::RequestError(err)) => {
eprintln!("fatal: {}", err);
std::process::exit(1);
},
Err(ApiError::Unexpected(_)) => todo!()
};
let lastmodified = prop.lastmodified.unwrap().timestamp_millis();
// update blob
Blob::new(obj.path.clone()).update(&lastmodified.to_string())?;
Ok(())
}
// download file with .distant at the end
fn conflict(&self) {
todo!()
}
}

View File

@@ -3,7 +3,6 @@ use std::io;
use crate::services::api::ApiError;
use crate::services::req_props::ReqProps;
use crate::services::upload_file::UploadFile;
use crate::store::index;
use crate::store::object::blob::Blob;
use crate::commands::status::LocalObj;
use crate::commands::push::push_factory::{PushState, PushChange, PushFlowState};
@@ -70,9 +69,6 @@ impl PushChange for New {
// create new blob
Blob::new(obj.path.clone()).create(&lastmodified.to_string(), true)?;
// remove index
index::rm_line(obj.path.to_str().unwrap())?;
Ok(())
}

View File

@@ -8,6 +8,7 @@ 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;
use crate::commands::push::modified::Modified;
#[derive(Debug)]
pub enum PushState {
@@ -54,6 +55,7 @@ pub trait PushChange {
if err.status() == 404 {
Ok(None)
} else {
eprintln!("err: when requesting properties of {} ({})", obj.name, err.status());
Err(())
}
},
@@ -84,7 +86,7 @@ impl PushFactory {
pub fn new(&self, obj: LocalObj) -> Box<dyn PushChange> {
match obj.state {
State::New => Box::new(New { obj }),
State::Modified => todo!(),
State::Modified => Box::new(Modified { obj }),
State::Deleted => Box::new(Deleted { obj }),
State::Default => todo!(),
_ => todo!(),

View File

@@ -134,12 +134,24 @@ pub struct LocalObj {
}
pub fn get_all_staged() -> Vec<LocalObj> {
let (mut new_objs_hashes, mut del_objs_hashes, mut objs_modified) = get_diff();
// get copy, modified
let staged_objs = get_staged(&mut new_objs_hashes);
let mut lines: Vec<String> = vec![];
staged_objs.clone()
// todo opti getting staged and then finding differences ?
if let Ok(entries) = index::read_line() {
for entry in entries {
lines.push(entry.unwrap());
}
}
let mut staged_objs = vec![];
for line in lines {
let obj = Blob::new(PathBuf::from(line)).get_local_obj();
if obj.state != State::Default {
staged_objs.push(obj);
}
}
staged_objs
}
fn get_staged(hashes: &mut HashMap<String, LocalObj>) -> Vec<LocalObj> {