feat(nsconfig): basic config and basic integration in service
This commit is contained in:
parent
e780279acd
commit
61531f664b
@ -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());
|
||||
|
@ -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();
|
||||
|
@ -105,11 +105,14 @@ fn setup_staged(obj_statuses: Arc<ObjStatuses>, 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
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -1 +1,2 @@
|
||||
pub mod config;
|
||||
pub mod nsconfig;
|
||||
|
@ -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<Option<PathBuf>>,
|
||||
nsconfig: OnceLock<NsConfig>,
|
||||
}
|
||||
|
||||
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()))
|
||||
}
|
||||
}
|
||||
|
67
src/config/nsconfig.rs
Normal file
67
src/config/nsconfig.rs
Normal file
@ -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<String>,
|
||||
/// 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<String, Remote>,
|
||||
}
|
||||
|
||||
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
|
||||
}
|
@ -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<Props>) -> 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<Obj> for ReqProps {
|
||||
fn from(obj: Obj) -> Self {
|
||||
ReqProps::new("")
|
||||
ReqProps::new(obj.get_obj_path().to_str().unwrap())
|
||||
}
|
||||
}
|
||||
|
@ -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<Config>,
|
||||
method: Option<Method>,
|
||||
url: Option<String>,
|
||||
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());
|
||||
}
|
||||
|
@ -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<PathBuf> = 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() {
|
||||
|
@ -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());
|
||||
}
|
||||
|
@ -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(),
|
||||
|
Loading…
Reference in New Issue
Block a user