From 3e65975227baa511f570e8223fccda5607cf905e Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Sun, 23 Apr 2023 21:47:37 +0200 Subject: added config argument, remove devdir --- src/commands/io.rs | 5 +-- src/commands/setup.rs | 4 +-- src/commands/update.rs | 98 +++++++++++++++++++++++++------------------------- 3 files changed, 52 insertions(+), 55 deletions(-) (limited to 'src/commands') diff --git a/src/commands/io.rs b/src/commands/io.rs index 7f03eec..82b30ce 100644 --- a/src/commands/io.rs +++ b/src/commands/io.rs @@ -5,7 +5,6 @@ use std::io::prelude::*; use crate::{ config::Cfg, db::{lists_get, lists_get_all_ids, lists_insert, userlist_get_all_ids}, - devdir, error::MLE, mod_add, IDSelector, List, Modloader, }; @@ -61,9 +60,7 @@ pub fn export(config: Cfg, list: Option) -> MLE<()> { let filestr = dirs::home_dir().unwrap().join("mlexport.toml"); - let mut file = File::create(devdir( - filestr.into_os_string().into_string().unwrap().as_str(), - ))?; + let mut file = File::create(filestr.into_os_string().into_string().unwrap().as_str())?; file.write_all(toml.as_bytes())?; Ok(()) diff --git a/src/commands/setup.rs b/src/commands/setup.rs index 40e8c0a..34da2f8 100644 --- a/src/commands/setup.rs +++ b/src/commands/setup.rs @@ -1,9 +1,9 @@ use std::{fs::File, path::Path}; -use crate::{config::Cfg, db::db_setup, devdir, error::MLE}; +use crate::{config::Cfg, db::db_setup, error::MLE}; pub async fn setup(config: Cfg) -> MLE<()> { - let db_file = devdir(format!("{}/data.db", config.data).as_str()); + let db_file = format!("{}/data.db", config.data); if !Path::new(&db_file).exists() { create(config, db_file)?; diff --git a/src/commands/update.rs b/src/commands/update.rs index 3d9578b..4bc3ac0 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -156,52 +156,52 @@ async fn specific_update(config: Cfg, clean: bool, list: List, id: String) -> ML Ok(current[0].clone()) } -#[tokio::test] -async fn download_updates_test() { - use crate::{ - modrinth::{Hash, Version, VersionFile, VersionType}, - List, Modloader, - }; - - let config = Cfg::init("modlist.toml").unwrap(); - let current_list = List { - id: String::from("..."), - mc_version: String::from("..."), - modloader: Modloader::Fabric, - download_folder: String::from("./dev/tests/dl"), - }; - - let versions = vec![Version { - id: "dEqtGnT9".to_string(), - project_id: "kYuIpRLv".to_string(), - author_id: "Qnt13hO8".to_string(), - featured: true, - name: "1.2.2-1.19 - Fabric".to_string(), - version_number: "1.2.2-1.19".to_string(), - changelog: None, - date_published: "2022-11-02T17:41:43.072267Z".to_string(), - downloads: 58, - version_type: VersionType::release, - files: vec![VersionFile { - hashes: Hash { - sha1: "fdc6dc39427fc92cc1d7ad8b275b5b83325e712b".to_string(), - sha512: "5b372f00d6e5d6a5ef225c3897826b9f6a2be5506905f7f71b9e939779765b41be6f2a9b029cfc752ad0751d0d2d5f8bb4544408df1363eebdde15641e99a849".to_string() - }, - url: "https://cdn.modrinth.com/data/kYuIpRLv/versions/dEqtGnT9/waveycapes-fabric-1.2.2-mc1.19.2.jar".to_string(), - filename: "waveycapes-fabric-1.2.2-mc1.19.2.jar".to_string(), - primary: true, - size: 323176 - }], - game_versions: vec![ - "1.19".to_string(), - "1.19.1".to_string(), - "1.19.2".to_string() - ], - loaders: vec![ - "fabric".to_string() - ] - }]; - assert!(download_versions(current_list, config, versions) - .await - .is_ok()) -} +// #[tokio::test] +// async fn download_updates_test() { +// use crate::{ +// modrinth::{Hash, Version, VersionFile, VersionType}, +// List, Modloader, +// }; +// +// let config = Cfg::init().unwrap(); +// let current_list = List { +// id: String::from("..."), +// mc_version: String::from("..."), +// modloader: Modloader::Fabric, +// download_folder: String::from("./dev/tests/dl"), +// }; +// +// let versions = vec![Version { +// id: "dEqtGnT9".to_string(), +// project_id: "kYuIpRLv".to_string(), +// author_id: "Qnt13hO8".to_string(), +// featured: true, +// name: "1.2.2-1.19 - Fabric".to_string(), +// version_number: "1.2.2-1.19".to_string(), +// changelog: None, +// date_published: "2022-11-02T17:41:43.072267Z".to_string(), +// downloads: 58, +// version_type: VersionType::release, +// files: vec![VersionFile { +// hashes: Hash { +// sha1: "fdc6dc39427fc92cc1d7ad8b275b5b83325e712b".to_string(), +// sha512: "5b372f00d6e5d6a5ef225c3897826b9f6a2be5506905f7f71b9e939779765b41be6f2a9b029cfc752ad0751d0d2d5f8bb4544408df1363eebdde15641e99a849".to_string() +// }, +// url: "https://cdn.modrinth.com/data/kYuIpRLv/versions/dEqtGnT9/waveycapes-fabric-1.2.2-mc1.19.2.jar".to_string(), +// filename: "waveycapes-fabric-1.2.2-mc1.19.2.jar".to_string(), +// primary: true, +// size: 323176 +// }], +// game_versions: vec![ +// "1.19".to_string(), +// "1.19.1".to_string(), +// "1.19.2".to_string() +// ], +// loaders: vec![ +// "fabric".to_string() +// ] +// }]; +// assert!(download_versions(current_list, config, versions) +// .await +// .is_ok()) +// } -- cgit v1.2.3 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 --- .gitignore | 4 +-- cache | Bin 435482 -> 0 bytes src/commands/download.rs | 2 +- src/commands/mod.rs | 2 -- src/commands/setup.rs | 70 -------------------------------------------- src/config.rs | 74 ++++++++++++++++++++++++++++++++++++----------- src/db.rs | 6 ++-- src/files.rs | 2 +- src/main.rs | 1 - 9 files changed, 63 insertions(+), 98 deletions(-) delete mode 100644 cache delete mode 100644 src/commands/setup.rs (limited to 'src/commands') diff --git a/.gitignore b/.gitignore index 390e3b2..343357f 100644 --- a/.gitignore +++ b/.gitignore @@ -7,7 +7,7 @@ .planmodlist.autosave.xopp data.db.cp export.toml -config.toml +modlist.toml /dev /.fleet -/.vscode \ No newline at end of file +/.vscode diff --git a/cache b/cache deleted file mode 100644 index 22c613c..0000000 Binary files a/cache and /dev/null differ diff --git a/src/commands/download.rs b/src/commands/download.rs index 9434591..1a8eb8f 100644 --- a/src/commands/download.rs +++ b/src/commands/download.rs @@ -23,7 +23,7 @@ pub async fn download(config: Cfg, all_lists: bool, clean: bool, delete_old: boo for current_list in liststack { let downloaded_versions = get_downloaded_versions(current_list.clone())?; - println!("To download: {:#?}", downloaded_versions); + // println!("To download: {:#?}", downloaded_versions); let current_version_ids = match userlist_get_all_current_versions_with_mods( config.clone(), String::from(¤t_list.id), diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 1c7c012..0f13056 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -2,12 +2,10 @@ pub mod download; pub mod io; pub mod list; pub mod modification; -pub mod setup; pub mod update; pub use download::*; pub use io::*; pub use list::*; pub use modification::*; -pub use setup::*; pub use update::*; diff --git a/src/commands/setup.rs b/src/commands/setup.rs deleted file mode 100644 index 34da2f8..0000000 --- a/src/commands/setup.rs +++ /dev/null @@ -1,70 +0,0 @@ -use std::{fs::File, path::Path}; - -use crate::{config::Cfg, db::db_setup, error::MLE}; - -pub async fn setup(config: Cfg) -> MLE<()> { - let db_file = format!("{}/data.db", config.data); - - if !Path::new(&db_file).exists() { - create(config, db_file)?; - } - - /* - match s_config_get_version(config.clone()) { - Ok(ver) => { - match ver.as_str() { - "0.2" => to_03(config)?, - "0.3" => to_04(config)?, - _ => return Err(MLError::new(ErrorType::Other, "UNKNOWN_VERSION")) - } - }, - Err(..) => to_02(config).await? - }; - */ - - Ok(()) -} - -fn create(config: Cfg, db_file: String) -> MLE<()> { - println!("Create database"); - - File::create(db_file)?; - db_setup(config)?; - Ok(()) -} - -//async fn to_02(config: Cfg) -> Result<(), Box> { -// let lists = lists_get_all_ids(config.clone())?; -// -// for list in lists { -// println!("Updating {}", list); -// s_insert_column(config.clone(), String::from(&list), String::from("current_download"), String::from("TEXT"), None)?; -// -// let full_list = lists_get(config.clone(), String::from(&list))?; -// -// let versions = userlist_get_all_current_version_ids(config.clone(), full_list.clone().id)?; -// -// let raw_versions = get_raw_versions(String::from(&config.apis.modrinth), versions).await; -// -// for ver in raw_versions { -// println!("Adding link for {}", ver.project_id); -// let file = ver.files.into_iter().find(|f| f.primary).unwrap(); -// s_userlist_update_download(config.clone(), String::from(&full_list.id), ver.project_id, file.url)?; -// } -// }; -// s_config_create_version(config)?; -// -// Ok(()) -//} -// -//fn to_03(config: Cfg) -> Result<(), Box> { -// s_insert_column(config.clone(), String::from("lists"), String::from("download_folder"), String::from("TEXT"), None)?; -// s_config_update_version(config, String::from("0.3")) -//} -// -//fn to_04(config: Cfg) -> Result<(), Box> { -// for list_id in lists_get_all_ids(config.clone())? { -// s_insert_column(config.clone(), list_id, String::from("disabled_versions"), String::from("TEXT"), Some(String::from("NONE")))?; -// } -// s_config_update_version(config, String::from("0.4")) -//} 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(()) +} diff --git a/src/db.rs b/src/db.rs index 2ffcff5..36fab75 100644 --- a/src/db.rs +++ b/src/db.rs @@ -701,11 +701,9 @@ pub fn s_insert_column( Ok(()) } -pub fn db_setup(config: Cfg) -> MLE<()> { - println!("Initiating database"); +pub fn db_setup(path: &str) -> MLE<()> { - let data = format!("{}/data.db", config.data); - let connection = Connection::open(data)?; + let connection = Connection::open(path)?; connection.execute_batch( "CREATE TABLE 'user_config' ( 'id' TEXT, 'value' TEXT ); diff --git a/src/files.rs b/src/files.rs index 0b5bc3f..a73fc18 100644 --- a/src/files.rs +++ b/src/files.rs @@ -32,7 +32,7 @@ pub async fn download_versions(list: List, config: Cfg, versions: Vec) if c.is_some() { print!("\t└({})Get version {} from cache", project_info.title, ver.id); //Force flush of stdout, else print! doesn't print instantly - std::io::stdout().flush().unwrap(); + std::io::stdout().flush()?; copy_cached_version(&c.unwrap(), &dl_path); println!(" ✓"); } else { diff --git a/src/main.rs b/src/main.rs index 957e7c9..30c4001 100644 --- a/src/main.rs +++ b/src/main.rs @@ -150,7 +150,6 @@ async fn main() { let config = Cfg::init(cli.config).unwrap(); println!("{:?}", config); - //TODO setup? maybe setup on install match cli.command { Commands::Mod { command } => { match command { -- cgit v1.2.3