From 61531f664b3c5abfcb8578a1784c1614343f77eb Mon Sep 17 00:00:00 2001 From: grimhilt Date: Sun, 15 Sep 2024 14:48:45 +0200 Subject: [PATCH] feat(nsconfig): basic config and basic integration in service --- src/commands/push.rs | 2 +- src/commands/reset.rs | 2 +- src/commands/status.rs | 7 ++-- src/commands/test.rs | 18 +++++++++-- src/config.rs | 1 + src/config/config.rs | 10 ++++++ src/config/nsconfig.rs | 67 +++++++++++++++++++++++++++++++++++++++ src/services/req_props.rs | 18 +++++++---- src/services/service.rs | 42 ++++++++++++++++++------ src/store/structs.rs | 6 ++-- src/subcommands/push.rs | 2 +- src/subcommands/reset.rs | 2 +- 12 files changed, 150 insertions(+), 27 deletions(-) create mode 100644 src/config/nsconfig.rs diff --git a/src/commands/push.rs b/src/commands/push.rs index 3c98205..e30669a 100644 --- a/src/commands/push.rs +++ b/src/commands/push.rs @@ -4,7 +4,7 @@ use crate::store::{indexer::Indexer, structs}; pub struct PushArgs {} -pub fn exec(args: PushArgs, config: Config) { +pub fn exec(_args: PushArgs, config: Config) { structs::init(config.get_root_unsafe()); let mut indexer = Indexer::new(config.get_root_unsafe()); diff --git a/src/commands/reset.rs b/src/commands/reset.rs index a6db4fe..1bd96be 100644 --- a/src/commands/reset.rs +++ b/src/commands/reset.rs @@ -3,7 +3,7 @@ use crate::store::indexer::Indexer; pub struct ResetArgs {} -pub fn exec(args: ResetArgs, config: Config) { +pub fn exec(_args: ResetArgs, config: Config) { // Init ignorer let indexer = Indexer::new(config.get_root_unsafe()); let _ = indexer.save(); diff --git a/src/commands/status.rs b/src/commands/status.rs index f477ef3..675e290 100644 --- a/src/commands/status.rs +++ b/src/commands/status.rs @@ -105,11 +105,14 @@ fn setup_staged(obj_statuses: Arc, indexer: &Indexer) -> ObjStaged } pub fn exec(args: StatusArgs, config: Config) { - let status = get_obj_changes(&args, &config); + let status = get_obj_changes(&config); + if args.nostyle { + todo!("Not style not implemented yet"); + } print_status(&status, config.get_root_unsafe()); } -pub fn get_obj_changes(args: &StatusArgs, config: &Config) -> ObjStaged { +pub fn get_obj_changes(config: &Config) -> ObjStaged { structs::init(config.get_root_unsafe()); // use root of repo if no custom path has been set by the command diff --git a/src/commands/test.rs b/src/commands/test.rs index f56964b..cbcfe2e 100644 --- a/src/commands/test.rs +++ b/src/commands/test.rs @@ -6,7 +6,21 @@ use crate::store::{indexer::Indexer, structs}; pub struct TestArgs {} pub fn exec(args: TestArgs, config: Config) { - dbg!(ReqProps::new("") + // let mut settings = CConfig::default(); + // settings.merge(File::with_name("config.toml")).unwrap(); + // dbg!(settings); + + // let config: MainConfig = + // toml::from_str(&std::fs::read_to_string("config.toml").unwrap()).unwrap(); + // dbg!(config); + + // let config: ConfigFile = settings.try_into().unwrap(); + // println!("{:?}", config); + + // Ok(()) + let req = ReqProps::new("") + .set_config(&config) .get_properties(vec![Props::CreationDate, Props::GetLastModified]) - .send()); + .send(); + dbg!(req); } diff --git a/src/config.rs b/src/config.rs index ef68c36..6309e19 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1 +1,2 @@ pub mod config; +pub mod nsconfig; diff --git a/src/config/config.rs b/src/config/config.rs index 374fb78..f45e98e 100644 --- a/src/config/config.rs +++ b/src/config/config.rs @@ -1,3 +1,4 @@ +use super::nsconfig::{load_nsconfig, NsConfig}; use std::env; use std::path::{Path, PathBuf}; use std::sync::OnceLock; @@ -6,10 +7,12 @@ use std::sync::OnceLock; /// # Parameters /// * `execution_path`: path of the command (directory arg) or current path /// * `root`: path of the repo +#[derive(Clone)] pub struct Config { pub execution_path: PathBuf, pub is_custom_execution_path: bool, root: OnceLock>, + nsconfig: OnceLock, } impl Config { @@ -18,6 +21,7 @@ impl Config { execution_path: PathBuf::from(env::current_dir().unwrap()), is_custom_execution_path: false, root: OnceLock::new(), + nsconfig: OnceLock::new(), } } @@ -27,6 +31,7 @@ impl Config { execution_path: PathBuf::from(path), is_custom_execution_path: true, root: OnceLock::new(), + nsconfig: OnceLock::new(), }, None => Config::new(), } @@ -58,4 +63,9 @@ impl Config { None => panic!("fatal: not a nextsync repository (or any parent up to mount point /)"), } } + + pub fn get_nsconfig(&self) -> &NsConfig { + self.nsconfig + .get_or_init(|| load_nsconfig(self.get_root_unsafe())) + } } diff --git a/src/config/nsconfig.rs b/src/config/nsconfig.rs new file mode 100644 index 0000000..991f1dc --- /dev/null +++ b/src/config/nsconfig.rs @@ -0,0 +1,67 @@ +use serde::Deserialize; +use std::collections::HashMap; +use std::fs; +use std::path::PathBuf; + +#[derive(Deserialize, Debug, Clone)] +#[serde(default)] +struct Core {} + +impl Default for Core { + fn default() -> Self { + Core {} + } +} + +#[derive(Deserialize, Debug, Clone)] +#[serde(default)] +pub struct Remote { + pub url: Option, + /// Force connection to nextcloud to be http (not https) + forceinsecure: bool, +} + +impl Default for Remote { + fn default() -> Self { + Remote { + url: None, + forceinsecure: false, + } + } +} + +#[derive(Deserialize, Debug, Clone)] +#[serde(default)] +pub struct NsConfig { + core: Core, + remote: std::collections::HashMap, +} + +impl NsConfig { + pub fn get_remote(&self, name: &str) -> Remote { + self.remote.get(name).unwrap().clone() + } +} + +impl Default for NsConfig { + fn default() -> Self { + NsConfig { + core: Core::default(), + remote: HashMap::new(), + } + } +} + +pub fn load_nsconfig(repo_root: &PathBuf) -> NsConfig { + let mut config_file_path = repo_root.clone(); + config_file_path.push(".nextsync"); + config_file_path.push("config"); + + if !config_file_path.exists() { + return NsConfig::default(); + } + + let config: NsConfig = toml::from_str(&fs::read_to_string(config_file_path).unwrap()).unwrap(); + + config +} diff --git a/src/services/req_props.rs b/src/services/req_props.rs index 79a40b5..99e1c07 100644 --- a/src/services/req_props.rs +++ b/src/services/req_props.rs @@ -1,5 +1,6 @@ use crate::services::service::Service; use crate::store::object::Obj; +use crate::config::config::Config; pub enum Props { CreationDate, @@ -39,12 +40,19 @@ pub struct ReqProps { impl ReqProps { pub fn new(path: &str) -> Self { + let mut service = Service::new(); + service.propfind(path.to_owned()); ReqProps { - service: Service::new(), + service, properties: Vec::new(), } } + pub fn set_config(mut self, config: &Config) -> Self { + self.service.set_config(config.clone()); + self + } + pub fn get_properties(mut self, properties: Vec) -> Self { self.properties.extend(properties); self @@ -66,16 +74,12 @@ impl ReqProps { } pub fn send(&mut self) { - self.service - .propfind(String::from( - "http://localhost:8080/remote.php/dav/files/admin/Documents", - )) - .send(); + self.service.send(); } } impl From for ReqProps { fn from(obj: Obj) -> Self { - ReqProps::new("") + ReqProps::new(obj.get_obj_path().to_str().unwrap()) } } diff --git a/src/services/service.rs b/src/services/service.rs index 2a309f3..ee45040 100644 --- a/src/services/service.rs +++ b/src/services/service.rs @@ -1,6 +1,6 @@ -use core::ops::{Deref, DerefMut}; -use reqwest::blocking::{Client, ClientBuilder, RequestBuilder}; -use reqwest::{header::HeaderMap, Method, Url}; +use crate::config::config::Config; +use reqwest::blocking::{Client, ClientBuilder}; +use reqwest::{header::HeaderMap, Method, Url}; const USER_AGENT: &str = "Nextsync"; @@ -16,17 +16,27 @@ impl ClientConfig { } pub fn default_headers(mut self, headers: HeaderMap) -> Self { - self.client = Some(self.client.take().expect("Client was already built").default_headers(headers)); + self.client = Some( + self.client + .take() + .expect("Client was already built") + .default_headers(headers), + ); self } pub fn build(&mut self) -> Client { - self.client.take().expect("Cannot build the client twice").build().unwrap() + self.client + .take() + .expect("Cannot build the client twice") + .build() + .unwrap() } } pub struct Service { client: ClientConfig, + config: Option, method: Option, url: Option, headers: HeaderMap, @@ -37,6 +47,7 @@ impl Service { pub fn new() -> Self { Service { client: ClientConfig::new(), + config: None, method: None, url: None, headers: HeaderMap::new(), @@ -44,6 +55,10 @@ impl Service { } } + pub fn set_config(&mut self, config: Config) { + self.config = Some(config); + } + pub fn propfind(&mut self, url: String) -> &mut Self { self.method = Some(Method::from_bytes(b"PROPFIND").expect("Cannot be invalid")); self.url = Some(url); @@ -51,12 +66,19 @@ impl Service { } pub fn send(&mut self) { - dbg!(self.client + let mut url = self + .config.clone() + .expect("A config must be provided to service") + .get_nsconfig() + .get_remote("origin") + .url + .expect("An url must be set on the remote"); + url.push_str(&self.url.clone().unwrap()); + + dbg!(self + .client .build() - .request( - self.method.clone().expect("Method must be set"), - self.url.clone().expect("Url must be set"), - ) + .request(self.method.clone().expect("Method must be set"), url,) .bearer_auth("rK5ud2NmrR8p586Th7v272HRgUcZcEKIEluOGjzQQRj7gWMMAISFTiJcFnnmnNiu2VVlENks") .send()); } diff --git a/src/store/structs.rs b/src/store/structs.rs index 88fbbe0..eea400b 100644 --- a/src/store/structs.rs +++ b/src/store/structs.rs @@ -3,7 +3,7 @@ use crypto::{digest::Digest, sha1::Sha1}; use std::env; use std::ops::{Deref, DerefMut}; use std::path::{Path, PathBuf}; -use std::sync::{Mutex, OnceLock}; +use std::sync::OnceLock; static REPO_ROOT: OnceLock = OnceLock::new(); @@ -19,7 +19,9 @@ pub fn init(repo_root: &PathBuf) { pub fn get_repo_root() -> PathBuf { if tests::is_var_setup() { - return env::var("REPO_ROOT_DEV").expect("REPO_ROOT_DEV is not set").into(); + return env::var("REPO_ROOT_DEV") + .expect("REPO_ROOT_DEV is not set") + .into(); } match REPO_ROOT.get() { diff --git a/src/subcommands/push.rs b/src/subcommands/push.rs index 88701d6..cf82b0e 100644 --- a/src/subcommands/push.rs +++ b/src/subcommands/push.rs @@ -8,6 +8,6 @@ pub fn create() -> Command { Command::new("push").about("Push changes on nextcloud") } -pub fn handler(args: &ArgMatches) { +pub fn handler(_args: &ArgMatches) { commands::push::exec(PushArgs {}, Config::new()); } diff --git a/src/subcommands/reset.rs b/src/subcommands/reset.rs index 7d01783..fc13077 100644 --- a/src/subcommands/reset.rs +++ b/src/subcommands/reset.rs @@ -9,7 +9,7 @@ pub fn create() -> Command { .about("Clear the index") } -pub fn handler(args: &ArgMatches) { +pub fn handler(_args: &ArgMatches) { commands::reset::exec( ResetArgs {}, Config::new(),