From 5326d48f6e0a88ad42005c39b73f7baaf91c9b86 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Wed, 28 Dec 2022 13:15:10 +0100 Subject: added devdir; better config dir --- src/commands/download.rs | 3 ++- src/commands/io.rs | 8 +++--- src/commands/setup.rs | 4 +-- src/config.rs | 14 +++++++---- src/db.rs | 64 ++++++++++++++++++++++++------------------------ src/files.rs | 4 +-- src/lib.rs | 16 +++++++++++- src/main.rs | 3 ++- 8 files changed, 69 insertions(+), 47 deletions(-) (limited to 'src') diff --git a/src/commands/download.rs b/src/commands/download.rs index 0f9011c..b958bf3 100644 --- a/src/commands/download.rs +++ b/src/commands/download.rs @@ -11,12 +11,13 @@ pub async fn download(config: Cfg, input: Input) -> Result<(), Box = vec![]; diff --git a/src/commands/io.rs b/src/commands/io.rs index 2edb95d..39f92d5 100644 --- a/src/commands/io.rs +++ b/src/commands/io.rs @@ -2,7 +2,7 @@ use std::fs::File; use std::io::prelude::*; use serde::{Serialize, Deserialize}; -use crate::{input::{Input, Subcmd}, db::{lists_get, userlist_get_all_ids, lists_get_all_ids, lists_insert}, config::Cfg, Modloader, mod_add, List}; +use crate::{input::{Input, Subcmd}, db::{lists_get, userlist_get_all_ids, lists_get_all_ids, lists_insert}, config::Cfg, Modloader, mod_add, List, devdir}; #[derive(Debug, Serialize, Deserialize)] struct Export { @@ -60,7 +60,9 @@ fn export(config: Cfg, input: Input) -> Result<(), Box> { let toml = toml::to_string( &Export { lists } )?; - let mut file = File::create("export.toml")?; + let filestr = dirs::home_dir().unwrap().join("mlexport.toml"); + + let mut file = File::create(devdir(filestr.into_os_string().into_string().unwrap().as_str()))?; file.write_all(&toml.as_bytes())?; Ok(()) @@ -70,7 +72,7 @@ async fn import(config: Cfg, args: Option>) -> Result<(), Box String::from(&args[0]), - None => String::from("export.toml"), + None => String::from(devdir(dirs::home_dir().unwrap().join("mlexport.toml").into_os_string().into_string().unwrap().as_str())), }; let mut file = File::open(filestr)?; diff --git a/src/commands/setup.rs b/src/commands/setup.rs index c7f1bed..e4fa801 100644 --- a/src/commands/setup.rs +++ b/src/commands/setup.rs @@ -1,9 +1,9 @@ use std::{fs::File, path::Path, io::{Error, ErrorKind}}; -use crate::{config::Cfg, db::{db_setup, s_config_get_version, s_config_create_version, s_insert_column, lists_get_all_ids, lists_get, userlist_get_all_current_version_ids, s_userlist_update_download, s_config_update_version}, modrinth::get_raw_versions}; +use crate::{config::Cfg, db::{db_setup, s_config_get_version, s_config_create_version, s_insert_column, lists_get_all_ids, lists_get, userlist_get_all_current_version_ids, s_userlist_update_download, s_config_update_version}, modrinth::get_raw_versions, devdir}; pub async fn setup(config: Cfg) -> Result<(), Box> { - let db_file = format!("{}/data.db", String::from(&config.data)); + let db_file = devdir(format!("{}/data.db", config.data).as_str()); if !Path::new(&db_file).exists() { return create(config, db_file); diff --git a/src/config.rs b/src/config.rs index 99d2ec2..383e7ee 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,7 +2,7 @@ use std::{fs::File, io::{Read, Write}}; use serde::{Serialize, Deserialize}; -use crate::error::MLE; +use crate::{error::MLE, devdir}; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Cfg { @@ -16,16 +16,20 @@ pub struct Apis { } impl Cfg { - pub fn init(path: &str) -> MLE { - let mut file = match File::open(path) { + pub fn init(filename: &str) -> MLE { + let configfile = dirs::config_dir().unwrap().join(filename); + + let mut file = match File::open(devdir(configfile.to_str().unwrap())) { 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("./"), apis: Apis { modrinth: String::from("https://api.modrinth.com/v2/") } }; - let mut file = File::create(path)?; + //TODO Error + let mut file = File::create(devdir(configfile.to_str().unwrap()))?; + println!("Created config file"); file.write_all(&toml::to_string(&default_cfg)?.as_bytes())?; - File::open(path)? + File::open(devdir(configfile.to_str().unwrap()))? } else { return Err(err.into()); } diff --git a/src/db.rs b/src/db.rs index cd32d1f..3f05772 100644 --- a/src/db.rs +++ b/src/db.rs @@ -2,14 +2,14 @@ use std::io::{Error, ErrorKind}; use rusqlite::Connection; -use crate::{Modloader, config::Cfg, List}; +use crate::{Modloader, config::Cfg, List, devdir}; //mods pub fn mods_insert(config: Cfg, id: String, name: String, versions: Vec) -> Result<(), Box> { println!("Inserting mod {}({}) into database", name, id); - let data = format!("{}/data.db", config.data); + let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data)?; connection.execute( @@ -21,7 +21,7 @@ pub fn mods_insert(config: Cfg, id: String, name: String, versions: Vec) } pub fn mods_get_all_ids(config: Cfg) -> Result, Box> { - let data = format!("{}/data.db", config.data); + let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data).unwrap(); let mut mods: Vec = Vec::new(); @@ -43,7 +43,7 @@ pub fn mods_get_all_ids(config: Cfg) -> Result, Box Result> { - let data = format!("{}/data.db", config.data); + let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data)?; let mut mod_id = String::new(); @@ -64,7 +64,7 @@ pub fn mods_get_id(config: Cfg, name: String) -> Result Result> { - let data = format!("{}/data.db", config.data); + let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data)?; let mut mod_name = String::new(); @@ -88,7 +88,7 @@ pub fn mods_change_versions(config: Cfg, versions: String, mod_id: String) -> Re println!("Updating versions for {} with \n {}", mod_id, versions); - let data = format!("{}/data.db", config.data); + let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data)?; connection.execute("UPDATE mods SET versions = ?1 WHERE id = ?2", [versions, mod_id])?; @@ -99,7 +99,7 @@ pub fn mods_remove(config: Cfg, id: String) -> Result<(), Box) -> Result, Box> { - let data = format!("{}/data.db", config.data); + let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data)?; if mods.is_empty() { return Err(Box::new(Error::new(ErrorKind::Other, "MODS_NO_INPUT"))); } @@ -149,7 +149,7 @@ pub fn mods_get_versions(config: Cfg, mods: Vec) -> Result, current_link: String) -> Result<(), Box> { println!("Inserting {} into current list({})", mod_id, list_id); - let data = format!("{}/data.db", config.data); + let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data)?; @@ -159,7 +159,7 @@ pub fn userlist_insert(config: Cfg, list_id: String, mod_id: String, current_ver } pub fn userlist_get_all_ids(config: Cfg, list_id: String) -> Result, Box> { - let data = format!("{}/data.db", config.data); + let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data).unwrap(); let mut mod_ids: Vec = Vec::new(); @@ -181,7 +181,7 @@ pub fn userlist_get_all_ids(config: Cfg, list_id: String) -> Result, pub fn userlist_remove(config: Cfg, list_id: String, mod_id: String) -> Result<(), Box> { - let data = format!("{}/data.db", config.data); + let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data)?; connection.execute(format!("DELETE FROM {} WHERE mod_id = ?", list_id).as_str(), [mod_id])?; @@ -190,7 +190,7 @@ pub fn userlist_remove(config: Cfg, list_id: String, mod_id: String) -> Result<( pub fn userlist_get_applicable_versions(config: Cfg, list_id: String, mod_id: String) -> Result> { - let data = format!("{}/data.db", config.data); + let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data).unwrap(); let mut version: String = String::new(); @@ -210,7 +210,7 @@ pub fn userlist_get_applicable_versions(config: Cfg, list_id: String, mod_id: St } pub fn userlist_get_all_applicable_versions_with_mods(config: Cfg, list_id: String) -> Result, Box> { - let data = format!("{}/data.db", config.data); + let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data)?; let mut versions: Vec<(String, String)> = Vec::new(); @@ -230,7 +230,7 @@ pub fn userlist_get_all_applicable_versions_with_mods(config: Cfg, list_id: Stri } pub fn userlist_get_current_version(config: Cfg, list_id: String, mod_id: String) -> Result> { - let data = format!("{}/data.db", config.data); + let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data).unwrap(); let mut version: String = String::new(); @@ -250,7 +250,7 @@ pub fn userlist_get_current_version(config: Cfg, list_id: String, mod_id: String } pub fn userlist_get_all_current_version_ids(config: Cfg, list_id: String) -> Result, Box> { - let data = format!("{}/data.db", config.data); + let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data)?; let mut versions: Vec = Vec::new(); @@ -269,7 +269,7 @@ pub fn userlist_get_all_current_version_ids(config: Cfg, list_id: String) -> Res } pub fn userlist_get_all_current_versions_with_mods(config: Cfg, list_id: String) -> Result, Box> { - let data = format!("{}/data.db", config.data); + let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data)?; let mut versions: Vec<(String, String)> = Vec::new(); @@ -289,7 +289,7 @@ pub fn userlist_get_all_current_versions_with_mods(config: Cfg, list_id: String) } pub fn userlist_change_versions(config: Cfg, list_id: String, current_version: String, versions: String, link: String, mod_id: String) -> Result<(), Box> { - let data = format!("{}/data.db", config.data); + let data = devdir(format!("{}/data.db", config.data).as_str()); 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])?; @@ -297,7 +297,7 @@ pub fn userlist_change_versions(config: Cfg, list_id: String, current_version: S } pub fn userlist_add_disabled_versions(config: Cfg, list_id: String, disabled_version: String, mod_id: String) -> Result<(), Box> { - let data = format!("{}/data.db", config.data); + let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data)?; let currently_disabled_versions = userlist_get_disabled_versions(config, String::from(&list_id), String::from(&mod_id))?; @@ -311,7 +311,7 @@ pub fn userlist_add_disabled_versions(config: Cfg, list_id: String, disabled_ver } pub fn userlist_get_disabled_versions(config:Cfg, list_id: String, mod_id: String) -> Result> { - let data = format!("{}/data.db", config.data); + let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data).unwrap(); let mut version: String = String::new(); @@ -331,7 +331,7 @@ pub fn userlist_get_disabled_versions(config:Cfg, list_id: String, mod_id: Strin } pub fn userlist_get_all_downloads(config: Cfg, list_id: String) -> Result, Box> { - let data = format!("{}/data.db", config.data); + let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data).unwrap(); let mut links: Vec = Vec::new(); @@ -355,7 +355,7 @@ pub fn userlist_get_all_downloads(config: Cfg, list_id: String) -> Result Result<(), Box> { println!("Creating list {}", id); - let data = format!("{}/data.db", config.data); + let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data)?; connection.execute("INSERT INTO lists VALUES (?1, ?2, ?3, ?4)", [id.clone(), mc_version, mod_loader.stringify(), download_folder])?; @@ -365,7 +365,7 @@ pub fn lists_insert(config: Cfg, id: String, mc_version: String, mod_loader: Mod } pub fn lists_remove(config: Cfg, id: String) -> Result<(), Box> { - let data = format!("{}/data.db", config.data); + let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data)?; connection.execute("DELETE FROM lists WHERE id = ?", [&id])?; @@ -374,7 +374,7 @@ pub fn lists_remove(config: Cfg, id: String) -> Result<(), Box Result> { - let data = format!("{}/data.db", config.data); + let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data).unwrap(); let mut list = List { id: String::new(), mc_version: String::new(), modloader: Modloader::Fabric, download_folder: String::new() }; @@ -395,7 +395,7 @@ pub fn lists_get(config: Cfg, list_id: String) -> Result Result, Box> { - let data = format!("{}/data.db", config.data); + let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data).unwrap(); let mut list_ids: Vec = Vec::new(); @@ -416,7 +416,7 @@ pub fn lists_get_all_ids(config: Cfg) -> Result, Box Result<(), Box> { - let data = format!("{}/data.db", config.data); + let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data)?; connection.execute("UPDATE user_config SET value = ? WHERE id = 'current_list'", [id])?; @@ -424,7 +424,7 @@ pub fn config_change_current_list(config: Cfg, id: String) -> Result<(), Box Result> { - let data = format!("{}/data.db", config.data); + let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data).unwrap(); let mut list_id = String::new(); @@ -444,7 +444,7 @@ pub fn config_get_current_list(config: Cfg) -> Result Result<(), Box> { - let data = format!("{}/data.db", config.data); + let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data)?; connection.execute(format!("UPDATE {} SET current_download = ?1 WHERE mod_id = ?2", list_id).as_str(), [link, mod_id])?; @@ -452,7 +452,7 @@ pub fn s_userlist_update_download(config: Cfg, list_id: String, mod_id: String, } pub fn s_config_create_version(config: Cfg) -> Result<(), Box> { - let data = format!("{}/data.db", config.data); + let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data)?; connection.execute("INSERT INTO 'user_config' VALUES ( 'db_version', '0.2' )", ())?; @@ -460,7 +460,7 @@ pub fn s_config_create_version(config: Cfg) -> Result<(), Box Result<(), Box> { - let data = format!("{}/data.db", config.data); + let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data)?; connection.execute("UPDATE user_config SET value = ? WHERE id = 'db_version'", [ver])?; @@ -468,7 +468,7 @@ pub fn s_config_update_version(config: Cfg, ver: String) -> Result<(), Box Result> { - let data = format!("{}/data.db", config.data); + let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data)?; let mut version: String = String::new(); @@ -486,7 +486,7 @@ pub fn s_config_get_version(config: Cfg) -> Result) -> Result<(), Box> { - let data = format!("{}/data.db", config.data); + let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data)?; let mut sql = format!("ALTER TABLE {} ADD '{}' {}", table, column, c_type); @@ -503,7 +503,7 @@ pub fn db_setup(config: Cfg) -> Result<(), Box> { println!("Initiating database"); - let data = format!("{}/data.db", config.data); + let data = devdir(format!("{}/data.db", config.data).as_str()); let connection = Connection::open(data)?; connection.execute_batch( diff --git a/src/files.rs b/src/files.rs index b1e537c..a3f838a 100644 --- a/src/files.rs +++ b/src/files.rs @@ -28,7 +28,7 @@ async fn download_file(url: String, path: String, name: String) -> Result<(), Bo .await?; // download chunks - let mut file = File::create(String::from(&dl_path_file))?; + let mut file = File::create(&dl_path_file)?; let mut stream = res.bytes_stream(); while let Some(item) = stream.next().await { @@ -78,7 +78,7 @@ pub fn get_file_path(list: List, versionid: String) -> Result Result, Box> { let mut versions: HashMap = HashMap::new(); - for file in read_dir(list.download_folder)? { + for file in read_dir(&list.download_folder)? { let path = file?.path(); if path.is_file() && path.extension().ok_or("BAH")? == "jar" { let pathstr = path.to_str().ok_or("BAH")?; diff --git a/src/lib.rs b/src/lib.rs index 971f544..17ad6b9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,7 @@ pub mod db; pub mod error; pub mod files; -use std::io::{Error, ErrorKind}; +use std::{io::{Error, ErrorKind}, path::Path}; pub use apis::*; pub use commands::*; @@ -33,3 +33,17 @@ impl 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 a8aa15d..59d41c5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,8 @@ use modlist::{config::Cfg, input::get_input}; #[tokio::main] async fn main() { - let config = Cfg::init("config.toml").unwrap(); + let config = Cfg::init("modlist.toml").unwrap(); + match get_input(config).await { Ok(..) => (), Err(e) => { -- cgit v1.2.3