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/cache.rs | 4 +-- src/commands/io.rs | 5 +-- src/commands/setup.rs | 4 +-- src/commands/update.rs | 98 +++++++++++++++++++++++++------------------------- src/config.rs | 15 ++++---- src/db.rs | 65 +++++++++++++++++---------------- src/files.rs | 6 ++-- src/lib.rs | 16 +-------- src/main.rs | 15 ++++---- 9 files changed, 107 insertions(+), 121 deletions(-) (limited to 'src') diff --git a/src/cache.rs b/src/cache.rs index 30c9f09..44029e0 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -1,7 +1,5 @@ use std::{collections::HashMap, fs::{read_dir, copy}}; -use crate::devdir; - /// . /// /// # Panics @@ -9,7 +7,7 @@ use crate::devdir; /// Panics if . pub fn get_cached_versions(path: &str) -> HashMap { let mut versions: HashMap = HashMap::new(); - for file in read_dir(devdir(path)).unwrap() { + for file in read_dir(path).unwrap() { let path = file.unwrap().path(); if path.is_file() && path.extension().ok_or("BAH").unwrap() == "jar" { let pathstr = path.to_str().ok_or("BAH").unwrap(); 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()) +// } diff --git a/src/config.rs b/src/config.rs index a9a937e..23c7796 100644 --- a/src/config.rs +++ b/src/config.rs @@ -5,7 +5,7 @@ use std::{ use serde::{Deserialize, Serialize}; -use crate::{devdir, error::MLE}; +use crate::error::MLE; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Cfg { @@ -20,10 +20,13 @@ pub struct Apis { } impl Cfg { - pub fn init(filename: &str) -> MLE { - let configfile = dirs::config_dir().unwrap().join(filename); + pub fn init(path: Option) -> MLE { + let configfile = match path { + Some(p) => String::from(p), + None => dirs::config_dir().unwrap().join("modlist.toml").to_string_lossy().into(), + }; - let mut file = match File::open(devdir(configfile.to_str().unwrap())) { + let mut file = match File::open(&configfile) { Ok(file) => file, Err(err) => { if err.kind() == std::io::ErrorKind::NotFound { @@ -35,10 +38,10 @@ impl Cfg { modrinth: String::from("https://api.modrinth.com/v2/"), }, }; - let mut file = File::create(devdir(configfile.to_str().unwrap()))?; + let mut file = File::create(&configfile)?; println!("Created config file"); file.write_all(toml::to_string(&default_cfg)?.as_bytes())?; - File::open(devdir(configfile.to_str().unwrap()))? + File::open(&configfile)? } else { return Err(err.into()); } diff --git a/src/db.rs b/src/db.rs index 09d54c2..2ffcff5 100644 --- a/src/db.rs +++ b/src/db.rs @@ -4,14 +4,13 @@ use rusqlite::Connection; use crate::{ config::Cfg, - devdir, error::{ErrorType, MLError, MLE}, List, Modloader, }; //MODS pub fn mods_insert(config: Cfg, id: &str, slug: &str, name: &str) -> MLE<()> { - let data = devdir(format!("{}/data.db", config.data).as_str()); + let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; connection.execute( @@ -23,7 +22,7 @@ pub fn mods_insert(config: Cfg, id: &str, slug: &str, name: &str) -> MLE<()> { } pub fn mods_get_all_ids(config: Cfg) -> Result, Box> { - let data = devdir(format!("{}/data.db", config.data).as_str()); + let data = format!("{}/data.db", config.data); let connection = Connection::open(data).unwrap(); let mut mods: Vec = Vec::new(); @@ -53,7 +52,7 @@ pub fn mods_get_all_ids(config: Cfg) -> Result, Box MLE { //TODO check if "slug" is id - let data = devdir(format!("{}/data.db", data).as_str()); + let data = format!("{}/data.db", data); let connection = Connection::open(data)?; let mut mod_id = String::new(); @@ -88,7 +87,7 @@ pub struct ModInfo { } pub fn mods_get_info(config: Cfg, id: &str) -> MLE { - let data = devdir(format!("{}/data.db", config.data).as_str()); + let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; let mut mod_info: Option = None; @@ -117,7 +116,7 @@ pub fn mods_get_info(config: Cfg, id: &str) -> MLE { pub fn mods_remove(config: Cfg, id: String) -> MLE<()> { println!("Removing mod {} from database", id); - let data = devdir(format!("{}/data.db", config.data).as_str()); + let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; connection.execute("DELETE FROM mods WHERE id = ?", [id])?; @@ -132,7 +131,7 @@ pub struct DBModlistVersions { } pub fn mods_get_versions(config: Cfg, mods: Vec) -> MLE> { - let data = devdir(format!("{}/data.db", config.data).as_str()); + let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; if mods.is_empty() { @@ -188,7 +187,7 @@ pub fn userlist_insert( current_link: &str, set_version: bool, ) -> MLE<()> { - let data = devdir(format!("{}/data.db", config.data).as_str()); + let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; let sv = match set_version { @@ -215,7 +214,7 @@ pub fn userlist_insert( } pub fn userlist_get_all_ids(config: Cfg, list_id: String) -> MLE> { - let data = devdir(format!("{}/data.db", config.data).as_str()); + let data = format!("{}/data.db", config.data); let connection = Connection::open(data).unwrap(); let mut mod_ids: Vec = Vec::new(); @@ -234,7 +233,7 @@ pub fn userlist_get_all_ids(config: Cfg, list_id: String) -> MLE> { } pub fn userlist_remove(config: Cfg, list_id: &str, mod_id: &str) -> MLE<()> { - let data = devdir(format!("{}/data.db", config.data).as_str()); + let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; connection.execute( @@ -249,7 +248,7 @@ pub fn userlist_get_applicable_versions( list_id: String, mod_id: String, ) -> MLE { - let data = devdir(format!("{}/data.db", config.data).as_str()); + let data = format!("{}/data.db", config.data); let connection = Connection::open(data).unwrap(); let mut version: String = String::new(); @@ -276,7 +275,7 @@ pub fn userlist_get_all_applicable_versions_with_mods( config: Cfg, list_id: String, ) -> MLE> { - let data = devdir(format!("{}/data.db", config.data).as_str()); + let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; let mut versions: Vec<(String, String)> = Vec::new(); @@ -302,7 +301,7 @@ pub fn userlist_get_all_applicable_versions_with_mods( } pub fn userlist_get_current_version(config: Cfg, list_id: &str, mod_id: &str) -> MLE { - let data = devdir(format!("{}/data.db", config.data).as_str()); + let data = format!("{}/data.db", config.data); let connection = Connection::open(data).unwrap(); let mut version: String = String::new(); @@ -324,7 +323,7 @@ pub fn userlist_get_all_current_version_ids( config: Cfg, list_id: String, ) -> Result, Box> { - let data = devdir(format!("{}/data.db", config.data).as_str()); + let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; let mut versions: Vec = Vec::new(); @@ -350,7 +349,7 @@ pub fn userlist_get_all_current_versions_with_mods( config: Cfg, list_id: String, ) -> Result, Box> { - let data = devdir(format!("{}/data.db", config.data).as_str()); + let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; let mut versions: Vec<(String, String)> = Vec::new(); @@ -379,7 +378,7 @@ pub fn userlist_get_all_current_versions_with_mods( } pub fn userlist_get_set_version(config: Cfg, list_id: &str, mod_id: &str) -> MLE { - let data = devdir(format!("{}/data.db", config.data).as_str()); + let data = format!("{}/data.db", config.data); let connection = Connection::open(data).unwrap(); let mut set_version: bool = false; @@ -402,7 +401,7 @@ pub fn userlist_change_versions( link: String, mod_id: String, ) -> MLE<()> { - let data = devdir(format!("{}/data.db", config.data).as_str()); + let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; connection.execute(format!("UPDATE {} SET current_version = ?1, applicable_versions = ?2, current_download = ?3 WHERE mod_id = ?4", list_id).as_str(), [current_version, versions, link, mod_id])?; @@ -415,7 +414,7 @@ pub fn userlist_add_disabled_versions( disabled_version: String, mod_id: String, ) -> MLE<()> { - let data = devdir(format!("{}/data.db", config.data).as_str()); + let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; let currently_disabled_versions = @@ -437,7 +436,7 @@ pub fn userlist_add_disabled_versions( } pub fn userlist_get_disabled_versions(config: Cfg, list_id: String, mod_id: String) -> MLE { - let data = devdir(format!("{}/data.db", config.data).as_str()); + let data = format!("{}/data.db", config.data); let connection = Connection::open(data).unwrap(); let mut version: String = String::new(); @@ -459,7 +458,7 @@ pub fn userlist_get_all_downloads( config: Cfg, list_id: String, ) -> Result, Box> { - let data = devdir(format!("{}/data.db", config.data).as_str()); + let data = format!("{}/data.db", config.data); let connection = Connection::open(data).unwrap(); let mut links: Vec = Vec::new(); @@ -494,7 +493,7 @@ pub fn lists_insert( ) -> MLE<()> { println!("Creating list {}", id); - let data = devdir(format!("{}/data.db", config.data).as_str()); + let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; connection.execute( @@ -512,7 +511,7 @@ pub fn lists_insert( } pub fn lists_remove(config: Cfg, id: String) -> MLE<()> { - let data = devdir(format!("{}/data.db", config.data).as_str()); + let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; connection.execute("DELETE FROM lists WHERE id = ?", [&id])?; @@ -521,7 +520,7 @@ pub fn lists_remove(config: Cfg, id: String) -> MLE<()> { } pub fn lists_get(config: Cfg, list_id: String) -> MLE { - let data = devdir(format!("{}/data.db", config.data).as_str()); + let data = format!("{}/data.db", config.data); let connection = Connection::open(data).unwrap(); let mut list = List { @@ -559,7 +558,7 @@ pub fn lists_get(config: Cfg, list_id: String) -> MLE { } pub fn lists_version(config: Cfg, list_id: &str, version: &str) -> MLE<()> { - let data = devdir(format!("{}/data.db", config.data).as_str()); + let data = format!("{}/data.db", config.data); let connection = Connection::open(data).unwrap(); connection.execute( @@ -570,7 +569,7 @@ pub fn lists_version(config: Cfg, list_id: &str, version: &str) -> MLE<()> { } pub fn lists_get_all_ids(config: Cfg) -> MLE> { - let data = devdir(format!("{}/data.db", config.data).as_str()); + let data = format!("{}/data.db", config.data); let connection = Connection::open(data).unwrap(); let mut list_ids: Vec = Vec::new(); @@ -589,7 +588,7 @@ pub fn lists_get_all_ids(config: Cfg) -> MLE> { //config pub fn config_change_current_list(config: Cfg, id: String) -> MLE<()> { - let data = devdir(format!("{}/data.db", config.data).as_str()); + let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; connection.execute( @@ -600,7 +599,7 @@ pub fn config_change_current_list(config: Cfg, id: String) -> MLE<()> { } pub fn config_get_current_list(config: Cfg) -> MLE { - let data = devdir(format!("{}/data.db", config.data).as_str()); + let data = format!("{}/data.db", config.data); let connection = Connection::open(data).unwrap(); let mut list_id = String::new(); @@ -625,7 +624,7 @@ pub fn s_userlist_update_download( mod_id: String, link: String, ) -> Result<(), Box> { - let data = devdir(format!("{}/data.db", config.data).as_str()); + let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; connection.execute( @@ -640,7 +639,7 @@ pub fn s_userlist_update_download( } pub fn s_config_create_version(config: Cfg) -> Result<(), Box> { - let data = devdir(format!("{}/data.db", config.data).as_str()); + let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; connection.execute( @@ -651,7 +650,7 @@ pub fn s_config_create_version(config: Cfg) -> Result<(), Box Result<(), Box> { - let data = devdir(format!("{}/data.db", config.data).as_str()); + let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; connection.execute( @@ -662,7 +661,7 @@ pub fn s_config_update_version(config: Cfg, ver: String) -> Result<(), Box Result> { - let data = devdir(format!("{}/data.db", config.data).as_str()); + let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; let mut version: String = String::new(); @@ -689,7 +688,7 @@ pub fn s_insert_column( c_type: String, default: Option, ) -> Result<(), Box> { - let data = devdir(format!("{}/data.db", config.data).as_str()); + let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; let mut sql = format!("ALTER TABLE {} ADD '{}' {}", table, column, c_type); @@ -705,7 +704,7 @@ pub fn s_insert_column( pub fn db_setup(config: Cfg) -> MLE<()> { println!("Initiating database"); - let data = devdir(format!("{}/data.db", config.data).as_str()); + let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; connection.execute_batch( diff --git a/src/files.rs b/src/files.rs index ecf9b52..0b5bc3f 100644 --- a/src/files.rs +++ b/src/files.rs @@ -11,12 +11,12 @@ use crate::{ db::{mods_get_info, userlist_add_disabled_versions}, error::{ErrorType, MLError, MLE}, modrinth::Version, - List, cache::{get_cached_versions, copy_cached_version}, devdir, + List, cache::{get_cached_versions, copy_cached_version}, }; pub async fn download_versions(list: List, config: Cfg, versions: Vec) -> MLE { - let mut cached = get_cached_versions(&devdir(&config.cache)); + let mut cached = get_cached_versions(&config.cache); println!("{:#?}", cached); @@ -59,7 +59,7 @@ pub async fn download_versions(list: List, config: Cfg, versions: Vec) //Force flush of stdout, else print! doesn't print instantly std::io::stdout().flush().unwrap(); let dl_path_file = format!("{}/{}", list.download_folder, filename); - let cache_path = format!("{}/{}", devdir(&config.clone().cache), filename); + let cache_path = format!("{}/{}", &config.clone().cache, filename); // println!("{}:{}", dl_path_file, cache_path); copy(dl_path_file, cache_path)?; println!(" ✓"); diff --git a/src/lib.rs b/src/lib.rs index 390696c..0bf3076 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,7 @@ pub mod error; pub mod files; pub mod cache; -use std::{fmt::Display, path::Path}; +use std::fmt::Display; pub use apis::*; pub use commands::*; @@ -36,17 +36,3 @@ impl Display for Modloader { } } } - -pub fn devdir(path: &str) -> String { - let p = Path::new(path); - let dev = std::env::var("DEV"); - let lvl = match dev { - Ok(dev) => dev.parse::().unwrap(), - Err(..) => 0, - }; - if lvl >= 1 { - format!("./dev/{}", p.file_name().unwrap().to_str().unwrap()) - } else { - String::from(path) - } -} diff --git a/src/main.rs b/src/main.rs index 2006856..957e7c9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ use clap::{Parser, Subcommand}; use modlist::{ config::Cfg, db::{config_get_current_list, lists_get, lists_get_all_ids}, - devdir, download, export, get_current_list, import, list_add, list_change, list_remove, + download, export, get_current_list, import, list_add, list_change, list_remove, list_version, mod_add, mod_remove, update, IDSelector, List, Modloader, }; @@ -14,6 +14,10 @@ use modlist::{ struct Cli { #[command(subcommand)] command: Commands, + + /// config file path + #[arg(short, long)] + config: Option, } #[derive(Subcommand)] @@ -142,8 +146,8 @@ enum ListCommands { #[tokio::main] async fn main() { let cli = Cli::parse(); - - let config = Cfg::init("modlist.toml").unwrap(); + + let config = Cfg::init(cli.config).unwrap(); println!("{:?}", config); //TODO setup? maybe setup on install @@ -250,15 +254,14 @@ async fn main() { Commands::Import { file, download } => { let filestr: String = match file { Some(args) => args, - None => devdir( + None => dirs::home_dir() .unwrap() .join("mlexport.toml") .into_os_string() .into_string() .unwrap() - .as_str(), - ), + , }; import(config, filestr, download).await -- cgit v1.2.3