From 96d400ca1275bf8444e5ad4dc6c8a06b01c3ea9d Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Mon, 24 Apr 2023 19:00:04 +0200 Subject: add auto create dirs, database and default config --- src/config.rs | 74 +++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 57 insertions(+), 17 deletions(-) (limited to 'src/config.rs') diff --git a/src/config.rs b/src/config.rs index 23c7796..817d22b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,11 +1,11 @@ use std::{ - fs::File, - io::{Read, Write}, + fs::{File, create_dir_all}, + io::{Read, Write}, path::Path, }; use serde::{Deserialize, Serialize}; -use crate::error::MLE; +use crate::{error::MLE, db::db_setup}; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Cfg { @@ -21,26 +21,16 @@ pub struct Apis { impl Cfg { pub fn init(path: Option) -> MLE { - let configfile = match path { + let configfile = match path.clone() { Some(p) => String::from(p), - None => dirs::config_dir().unwrap().join("modlist.toml").to_string_lossy().into(), + None => dirs::config_dir().unwrap().join("modlist.toml").to_string_lossy().to_string(), }; let mut file = match File::open(&configfile) { Ok(file) => file, Err(err) => { - if err.kind() == std::io::ErrorKind::NotFound { - println!("No config file found, creating one"); - let default_cfg = Cfg { - data: String::from("~/.cache/modlist/"), - cache: String::from("~/.cache/modlist/cache"), - apis: Apis { - modrinth: String::from("https://api.modrinth.com/v2/"), - }, - }; - let mut file = File::create(&configfile)?; - println!("Created config file"); - file.write_all(toml::to_string(&default_cfg)?.as_bytes())?; + if err.kind() == std::io::ErrorKind::NotFound && path.is_none() { + create_config(&configfile)?; File::open(&configfile)? } else { return Err(err.into()); @@ -50,6 +40,56 @@ impl Cfg { let mut content = String::new(); file.read_to_string(&mut content)?; let config = toml::from_str::(&content)?; + //Check cache + if !Path::new(&config.cache).exists() { + create_cache(&config.cache)?; + }; + //Check database + //TODO check file + let datafile = format!("{}/data.db", config.data); + match File::open(&datafile) { + Ok(..) => (), + Err(..) => create_database(&datafile)?, + }; Ok(config) } } + +fn create_config(path: &str) -> MLE<()> { + print!("No config file found, create default"); + //Force flush of stdout, else print! doesn't print instantly + std::io::stdout().flush()?; + let default_cfg = Cfg { + //TODO get home dir + data: String::from("$HOME/.cache/modlist/"), + cache: String::from("$HOME/.cache/modlist/cache"), + apis: Apis { + modrinth: String::from("https://api.modrinth.com/v2/"), + }, + }; + let mut file = File::create(path)?; + file.write_all(toml::to_string(&default_cfg)?.as_bytes())?; + println!(" ✓"); + Ok(()) +} + +fn create_database(path: &str) -> MLE<()> { + print!("No database found, create base"); + //Force flush of stdout, else print! doesn't print instantly + std::io::stdout().flush()?; + + File::create(path)?; + db_setup(path)?; + println!(" ✓"); + Ok(()) +} + +fn create_cache(path: &str) -> MLE<()> { + print!("No cache direcory found, create one"); + //Force flush of stdout, else print! doesn't print instantly + std::io::stdout().flush()?; + + create_dir_all(path)?; + println!(" ✓"); + Ok(()) +} -- cgit v1.2.3