From 416f4dc383ff5a1194da3a5532a8e159a4a1dac0 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Sun, 23 Apr 2023 18:24:32 +0200 Subject: added caching, better data location --- src/config.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/config.rs') diff --git a/src/config.rs b/src/config.rs index 1b54d5f..a9a937e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -10,6 +10,7 @@ use crate::{devdir, error::MLE}; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Cfg { pub data: String, + pub cache: String, pub apis: Apis, } @@ -28,7 +29,8 @@ impl Cfg { if err.kind() == std::io::ErrorKind::NotFound { println!("No config file found, creating one"); let default_cfg = Cfg { - data: String::from("./"), + data: String::from("~/.cache/modlist/"), + cache: String::from("~/.cache/modlist/cache"), apis: Apis { modrinth: String::from("https://api.modrinth.com/v2/"), }, -- cgit v1.2.3 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 --- Cargo.lock | 2 +- 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 ++++---- 10 files changed, 108 insertions(+), 122 deletions(-) (limited to 'src/config.rs') diff --git a/Cargo.lock b/Cargo.lock index f99cd87..b591837 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -798,7 +798,7 @@ dependencies = [ [[package]] name = "modlist" -version = "0.12.0" +version = "0.13.0" dependencies = [ "chrono", "clap", 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 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/config.rs') 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 From 4e6466af1329f7b9e341df2e76ab696d11f80c93 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Thu, 27 Apr 2023 10:08:20 +0200 Subject: version bump, cargo fmt and cargo update --- Cargo.lock | 139 +++++++++++++++++++++++++++++----------------------------- src/cache.rs | 5 ++- src/config.rs | 13 ++++-- src/db.rs | 1 - src/files.rs | 19 +++++--- src/lib.rs | 2 +- src/main.rs | 22 +++++----- 7 files changed, 106 insertions(+), 95 deletions(-) (limited to 'src/config.rs') diff --git a/Cargo.lock b/Cargo.lock index b591837..f25b161 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -39,9 +39,9 @@ dependencies = [ [[package]] name = "anstream" -version = "0.3.0" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e579a7752471abc2a8268df8b20005e3eadd975f585398f17efcfd8d4927371" +checksum = "6342bd4f5a1205d7f41e94a41a901f5647c938cdfa96036338e8533c9d6c2450" dependencies = [ "anstyle", "anstyle-parse", @@ -78,9 +78,9 @@ dependencies = [ [[package]] name = "anstyle-wincon" -version = "1.0.0" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4bcd8291a340dd8ac70e18878bc4501dd7b4ff970cfa21c207d36ece51ea88fd" +checksum = "180abfa45703aebe0093f79badacc01b8fd4ea2e35118747e5811127f926e188" dependencies = [ "anstyle", "windows-sys 0.48.0", @@ -121,15 +121,15 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bitflags" -version = "2.0.2" +version = "2.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "487f1e0fcbe47deb8b0574e646def1c903389d95241dd1bbcc6ce4a715dfc0c1" +checksum = "24a6904aef64d73cf10ab17ebace7befb918b82164785cb89907993be7f83813" [[package]] name = "bumpalo" -version = "3.12.0" +version = "3.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d261e256854913907f67ed06efbc3338dfe6179796deefc1ff763fc1aee5535" +checksum = "9b1ce199063694f33ffb7dd4e0ee620741495c32833cde5aa08f02a0bf96f0c8" [[package]] name = "bytes" @@ -166,9 +166,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.2.2" +version = "4.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b802d85aaf3a1cdb02b224ba472ebdea62014fccfcb269b95a4d76443b5ee5a" +checksum = "956ac1f6381d8d82ab4684768f89c0ea3afe66925ceadb4eeb3fc452ffc55d62" dependencies = [ "clap_builder", "clap_derive", @@ -177,9 +177,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.2.2" +version = "4.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14a1a858f532119338887a4b8e1af9c60de8249cd7bafd68036a489e261e37b6" +checksum = "84080e799e54cff944f4b4a4b0e71630b0e0443b25b985175c7dddc1a859b749" dependencies = [ "anstream", "anstyle", @@ -190,9 +190,9 @@ dependencies = [ [[package]] name = "clap_complete" -version = "4.2.0" +version = "4.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "01c22dcfb410883764b29953103d9ef7bb8fe21b3fa1158bc99986c2067294bd" +checksum = "1a19591b2ab0e3c04b588a0e04ddde7b9eaa423646d1b4a8092879216bf47473" dependencies = [ "clap", ] @@ -206,7 +206,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.13", + "syn 2.0.15", ] [[package]] @@ -271,7 +271,7 @@ dependencies = [ "proc-macro2", "quote", "scratch", - "syn 2.0.13", + "syn 2.0.15", ] [[package]] @@ -288,7 +288,7 @@ checksum = "2345488264226bf682893e25de0769f3360aac9957980ec49361b083ddaa5bc5" dependencies = [ "proc-macro2", "quote", - "syn 2.0.13", + "syn 2.0.15", ] [[package]] @@ -322,13 +322,13 @@ dependencies = [ [[package]] name = "errno" -version = "0.3.0" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50d6a0976c999d473fe89ad888d5a284e55366d9dc9038b1ba2aa15128c4afa0" +checksum = "4bcfec3a70f97c962c307b2d2c56e358cf1d00b558d74262b5f929ee8cc7e73a" dependencies = [ "errno-dragonfly", "libc", - "windows-sys 0.45.0", + "windows-sys 0.48.0", ] [[package]] @@ -431,7 +431,7 @@ checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" dependencies = [ "proc-macro2", "quote", - "syn 2.0.13", + "syn 2.0.15", ] [[package]] @@ -465,9 +465,9 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.2.8" +version = "0.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31" +checksum = "c85e1d9ab2eadba7e5040d4e09cbd6d072b76a557ad64e797c2cb9d4da21d7e4" dependencies = [ "cfg-if", "libc", @@ -482,9 +482,9 @@ checksum = "ad0a93d233ebf96623465aad4046a8d3aa4da22d4f4beba5388838c8a434bbb4" [[package]] name = "h2" -version = "0.3.16" +version = "0.3.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5be7b54589b581f624f566bf5d8eb2bab1db736c51528720b6bd36b96b55924d" +checksum = "17f8a914c2987b688368b5138aa05321db91f4090cf26118185672ad588bce21" dependencies = [ "bytes", "fnv", @@ -574,9 +574,9 @@ checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421" [[package]] name = "hyper" -version = "0.14.25" +version = "0.14.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc5e554ff619822309ffd57d8734d77cd5ce6238bc956f037ea06c58238c9899" +checksum = "ab302d72a6f11a3b910431ff93aae7e773078c769f0a3ef15fb9ec692ed147d4" dependencies = [ "bytes", "futures-channel", @@ -664,13 +664,13 @@ dependencies = [ [[package]] name = "io-lifetimes" -version = "1.0.9" +version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09270fd4fa1111bc614ed2246c7ef56239a3063d5be0d1ec3b589c505d400aeb" +checksum = "9c66c74d2ae7e79a5a8f7ac924adbe38ee42a859c6539ad869eb51f0b52dc220" dependencies = [ "hermit-abi 0.3.1", "libc", - "windows-sys 0.45.0", + "windows-sys 0.48.0", ] [[package]] @@ -681,14 +681,14 @@ checksum = "12b6ee2129af8d4fb011108c73d99a1b83a85977f23b82460c0ae2e25bb4b57f" [[package]] name = "is-terminal" -version = "0.4.6" +version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "256017f749ab3117e93acb91063009e1f1bb56d03965b14c2c8df4eb02c524d8" +checksum = "adcf93614601c8129ddf72e2d5633df827ba6551541c6d8c59520a371475be1f" dependencies = [ "hermit-abi 0.3.1", "io-lifetimes", "rustix", - "windows-sys 0.45.0", + "windows-sys 0.48.0", ] [[package]] @@ -714,9 +714,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.141" +version = "0.2.142" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3304a64d199bb964be99741b7a14d26972741915b3649639149b2479bb46f4b5" +checksum = "6a987beff54b60ffa6d51982e1aa1146bc42f19bd26be28b0586f252fccf5317" [[package]] name = "libsqlite3-sys" @@ -740,9 +740,9 @@ dependencies = [ [[package]] name = "linux-raw-sys" -version = "0.3.1" +version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d59d8c75012853d2e872fb56bc8a2e53718e2cafe1a4c823143141c6d90c322f" +checksum = "36eb31c1778188ae1e64398743890d0877fef36d11521ac60406b42016e8c2cf" [[package]] name = "lock_api" @@ -798,7 +798,7 @@ dependencies = [ [[package]] name = "modlist" -version = "0.13.0" +version = "0.13.1" dependencies = [ "chrono", "clap", @@ -878,9 +878,9 @@ checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" [[package]] name = "openssl" -version = "0.10.49" +version = "0.10.52" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d2f106ab837a24e03672c59b1239669a0596406ff657c3c0835b6b7f0f35a33" +checksum = "01b8574602df80f7b85fdfc5392fa884a4e3b3f4f35402c070ab34c3d3f78d56" dependencies = [ "bitflags 1.3.2", "cfg-if", @@ -899,7 +899,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.13", + "syn 2.0.15", ] [[package]] @@ -910,9 +910,9 @@ checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" [[package]] name = "openssl-sys" -version = "0.9.84" +version = "0.9.87" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3a20eace9dc2d82904039cb76dcf50fb1a0bba071cfd1629720b5d6f1ddba0fa" +checksum = "8e17f59264b2809d77ae94f0e1ebabc434773f370d6ca667bd223ea10e06cc7e" dependencies = [ "cc", "libc", @@ -1059,7 +1059,7 @@ version = "0.29.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "549b9d036d571d42e6e85d1c1425e2ac83491075078ca9a15be021c56b1641f2" dependencies = [ - "bitflags 2.0.2", + "bitflags 2.2.1", "fallible-iterator", "fallible-streaming-iterator", "hashlink", @@ -1069,22 +1069,22 @@ dependencies = [ [[package]] name = "rustc-demangle" -version = "0.1.22" +version = "0.1.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d4a36c42d1873f9a77c53bde094f9664d9891bc604a45b4798fd2c389ed12e5b" +checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" [[package]] name = "rustix" -version = "0.37.7" +version = "0.37.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2aae838e49b3d63e9274e1c01833cc8139d3fec468c3b84688c628f44b1ae11d" +checksum = "a0661814f891c57c930a610266415528da53c4933e6dea5fb350cbfe048a9ece" dependencies = [ "bitflags 1.3.2", "errno", "io-lifetimes", "libc", "linux-raw-sys", - "windows-sys 0.45.0", + "windows-sys 0.48.0", ] [[package]] @@ -1139,29 +1139,29 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.159" +version = "1.0.160" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c04e8343c3daeec41f58990b9d77068df31209f2af111e059e9fe9646693065" +checksum = "bb2f3770c8bce3bcda7e149193a069a0f4365bda1fa5cd88e03bca26afc1216c" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.159" +version = "1.0.160" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c614d17805b093df4b147b51339e7e44bf05ef59fba1e45d83500bcfb4d8585" +checksum = "291a097c63d8497e00160b166a967a4a79c64f3facdd01cbd7502231688d77df" dependencies = [ "proc-macro2", "quote", - "syn 2.0.13", + "syn 2.0.15", ] [[package]] name = "serde_json" -version = "1.0.95" +version = "1.0.96" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d721eca97ac802aa7777b701877c8004d950fc142651367300d21c1cc0194744" +checksum = "057d394a50403bcac12672b2b18fb387ab6d289d957dab67dd201875391e52f1" dependencies = [ "itoa", "ryu", @@ -1242,9 +1242,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.13" +version = "2.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c9da457c5285ac1f936ebd076af6dac17a61cfe7826f2076b4d015cf47bc8ec" +checksum = "a34fcf3e8b60f57e6a14301a2e916d323af98b0ea63c599441eec8558660c822" dependencies = [ "proc-macro2", "quote", @@ -1290,7 +1290,7 @@ checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" dependencies = [ "proc-macro2", "quote", - "syn 2.0.13", + "syn 2.0.15", ] [[package]] @@ -1321,9 +1321,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.27.0" +version = "1.28.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0de47a4eecbe11f498978a9b29d792f0d2692d1dd003650c24c76510e3bc001" +checksum = "c3c786bf8134e5a3a166db9b29ab8f48134739014a3eca7bc6bfa95d673b136f" dependencies = [ "autocfg", "bytes", @@ -1335,18 +1335,18 @@ dependencies = [ "signal-hook-registry", "socket2", "tokio-macros", - "windows-sys 0.45.0", + "windows-sys 0.48.0", ] [[package]] name = "tokio-macros" -version = "2.0.0" +version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61a573bdc87985e9d6ddeed1b3d864e8a302c847e40d647746df2f1de209d1ce" +checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.13", + "syn 2.0.15", ] [[package]] @@ -1361,9 +1361,9 @@ dependencies = [ [[package]] name = "tokio-util" -version = "0.7.7" +version = "0.7.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5427d89453009325de0d8f342c9490009f76e999cb7672d77e46267448f7e6b2" +checksum = "806fe8c2c87eccc8b3267cbae29ed3ab2d0bd37fca70ab622e46aaa9375ddb7d" dependencies = [ "bytes", "futures-core", @@ -1415,11 +1415,10 @@ checksum = "b6bc1c9ce2b5135ac7f93c72918fc37feb872bdc6a5533a8b85eb4b86bfdae52" [[package]] name = "tracing" -version = "0.1.37" +version = "0.1.38" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8" +checksum = "cf9cf6a813d3f40c88b0b6b6f29a5c95c6cdbf97c1f9cc53fb820200f5ad814d" dependencies = [ - "cfg-if", "pin-project-lite", "tracing-core", ] diff --git a/src/cache.rs b/src/cache.rs index 44029e0..11645d1 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -1,4 +1,7 @@ -use std::{collections::HashMap, fs::{read_dir, copy}}; +use std::{ + collections::HashMap, + fs::{copy, read_dir}, +}; /// . /// diff --git a/src/config.rs b/src/config.rs index 817d22b..61db1c7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,11 +1,12 @@ use std::{ - fs::{File, create_dir_all}, - io::{Read, Write}, path::Path, + fs::{create_dir_all, File}, + io::{Read, Write}, + path::Path, }; use serde::{Deserialize, Serialize}; -use crate::{error::MLE, db::db_setup}; +use crate::{db::db_setup, error::MLE}; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Cfg { @@ -23,7 +24,11 @@ impl Cfg { pub fn init(path: Option) -> MLE { let configfile = match path.clone() { Some(p) => String::from(p), - None => dirs::config_dir().unwrap().join("modlist.toml").to_string_lossy().to_string(), + None => dirs::config_dir() + .unwrap() + .join("modlist.toml") + .to_string_lossy() + .to_string(), }; let mut file = match File::open(&configfile) { diff --git a/src/db.rs b/src/db.rs index 36fab75..abfe1dd 100644 --- a/src/db.rs +++ b/src/db.rs @@ -702,7 +702,6 @@ pub fn s_insert_column( } pub fn db_setup(path: &str) -> MLE<()> { - let connection = Connection::open(path)?; connection.execute_batch( diff --git a/src/files.rs b/src/files.rs index a73fc18..59fc7de 100644 --- a/src/files.rs +++ b/src/files.rs @@ -2,20 +2,20 @@ use futures_util::StreamExt; use reqwest::Client; use std::{ collections::HashMap, - fs::{read_dir, remove_file, rename, File, copy}, + fs::{copy, read_dir, remove_file, rename, File}, io::Write, }; use crate::{ + cache::{copy_cached_version, get_cached_versions}, config::Cfg, db::{mods_get_info, userlist_add_disabled_versions}, error::{ErrorType, MLError, MLE}, modrinth::Version, - List, cache::{get_cached_versions, copy_cached_version}, + List, }; pub async fn download_versions(list: List, config: Cfg, versions: Vec) -> MLE { - let mut cached = get_cached_versions(&config.cache); println!("{:#?}", cached); @@ -30,7 +30,10 @@ pub async fn download_versions(list: List, config: Cfg, versions: Vec) //Check cache if already downloaded let c = cached.remove(&ver.id); if c.is_some() { - print!("\t└({})Get version {} from cache", project_info.title, ver.id); + 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()?; copy_cached_version(&c.unwrap(), &dl_path); @@ -52,7 +55,12 @@ pub async fn download_versions(list: List, config: Cfg, versions: Vec) ver.id, extension ); - download_file(primary_file.url, list.clone().download_folder, filename.clone()).await?; + download_file( + primary_file.url, + list.clone().download_folder, + filename.clone(), + ) + .await?; println!(" ✓"); //Copy file to cache print!("\t └Copy to cache"); @@ -64,7 +72,6 @@ pub async fn download_versions(list: List, config: Cfg, versions: Vec) copy(dl_path_file, cache_path)?; println!(" ✓"); } - } Ok(dl_path) diff --git a/src/lib.rs b/src/lib.rs index 0bf3076..185edd7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,10 @@ pub mod apis; +pub mod cache; pub mod commands; pub mod config; pub mod db; pub mod error; pub mod files; -pub mod cache; use std::fmt::Display; diff --git a/src/main.rs b/src/main.rs index 30c4001..0ecb850 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,8 +2,8 @@ use clap::{Parser, Subcommand}; use modlist::{ config::Cfg, db::{config_get_current_list, lists_get, lists_get_all_ids}, - download, export, get_current_list, import, list_add, list_change, list_remove, - list_version, mod_add, mod_remove, update, IDSelector, List, Modloader, + download, export, get_current_list, import, list_add, list_change, list_remove, list_version, + mod_add, mod_remove, update, IDSelector, List, Modloader, }; //TODO implement remote sql db @@ -14,7 +14,7 @@ use modlist::{ struct Cli { #[command(subcommand)] command: Commands, - + /// config file path #[arg(short, long)] config: Option, @@ -146,7 +146,7 @@ enum ListCommands { #[tokio::main] async fn main() { let cli = Cli::parse(); - + let config = Cfg::init(cli.config).unwrap(); println!("{:?}", config); @@ -253,14 +253,12 @@ async fn main() { Commands::Import { file, download } => { let filestr: String = match file { Some(args) => args, - None => - dirs::home_dir() - .unwrap() - .join("mlexport.toml") - .into_os_string() - .into_string() - .unwrap() - , + None => dirs::home_dir() + .unwrap() + .join("mlexport.toml") + .into_os_string() + .into_string() + .unwrap(), }; import(config, filestr, download).await -- cgit v1.2.3