summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorfx <[email protected]>2023-04-23 21:57:09 +0200
committerfx <[email protected]>2023-04-23 21:57:09 +0200
commit99c84b5a81f395f4f094b157019e84bfc4459df6 (patch)
treef852fd5cc3b26a004cf7bfab054e86c0d74c4669 /src
parent416f4dc383ff5a1194da3a5532a8e159a4a1dac0 (diff)
parent3e65975227baa511f570e8223fccda5607cf905e (diff)
downloadmodlist-99c84b5a81f395f4f094b157019e84bfc4459df6.tar
modlist-99c84b5a81f395f4f094b157019e84bfc4459df6.tar.gz
modlist-99c84b5a81f395f4f094b157019e84bfc4459df6.zip
Merge pull request 'added config argument, remove devdir' (#2) from dev into cache
Reviewed-on: http://raspberrypi.fritz.box:7920/fx/modlist/pulls/2
Diffstat (limited to 'src')
-rw-r--r--src/cache.rs4
-rw-r--r--src/commands/io.rs5
-rw-r--r--src/commands/setup.rs4
-rw-r--r--src/commands/update.rs98
-rw-r--r--src/config.rs15
-rw-r--r--src/db.rs65
-rw-r--r--src/files.rs6
-rw-r--r--src/lib.rs16
-rw-r--r--src/main.rs15
9 files changed, 107 insertions, 121 deletions
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 @@
1use std::{collections::HashMap, fs::{read_dir, copy}}; 1use std::{collections::HashMap, fs::{read_dir, copy}};
2 2
3use crate::devdir;
4
5/// . 3/// .
6/// 4///
7/// # Panics 5/// # Panics
@@ -9,7 +7,7 @@ use crate::devdir;
9/// Panics if . 7/// Panics if .
10pub fn get_cached_versions(path: &str) -> HashMap<String, String> { 8pub fn get_cached_versions(path: &str) -> HashMap<String, String> {
11 let mut versions: HashMap<String, String> = HashMap::new(); 9 let mut versions: HashMap<String, String> = HashMap::new();
12 for file in read_dir(devdir(path)).unwrap() { 10 for file in read_dir(path).unwrap() {
13 let path = file.unwrap().path(); 11 let path = file.unwrap().path();
14 if path.is_file() && path.extension().ok_or("BAH").unwrap() == "jar" { 12 if path.is_file() && path.extension().ok_or("BAH").unwrap() == "jar" {
15 let pathstr = path.to_str().ok_or("BAH").unwrap(); 13 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::*;
5use crate::{ 5use crate::{
6 config::Cfg, 6 config::Cfg,
7 db::{lists_get, lists_get_all_ids, lists_insert, userlist_get_all_ids}, 7 db::{lists_get, lists_get_all_ids, lists_insert, userlist_get_all_ids},
8 devdir,
9 error::MLE, 8 error::MLE,
10 mod_add, IDSelector, List, Modloader, 9 mod_add, IDSelector, List, Modloader,
11}; 10};
@@ -61,9 +60,7 @@ pub fn export(config: Cfg, list: Option<String>) -> MLE<()> {
61 60
62 let filestr = dirs::home_dir().unwrap().join("mlexport.toml"); 61 let filestr = dirs::home_dir().unwrap().join("mlexport.toml");
63 62
64 let mut file = File::create(devdir( 63 let mut file = File::create(filestr.into_os_string().into_string().unwrap().as_str())?;
65 filestr.into_os_string().into_string().unwrap().as_str(),
66 ))?;
67 file.write_all(toml.as_bytes())?; 64 file.write_all(toml.as_bytes())?;
68 65
69 Ok(()) 66 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 @@
1use std::{fs::File, path::Path}; 1use std::{fs::File, path::Path};
2 2
3use crate::{config::Cfg, db::db_setup, devdir, error::MLE}; 3use crate::{config::Cfg, db::db_setup, error::MLE};
4 4
5pub async fn setup(config: Cfg) -> MLE<()> { 5pub async fn setup(config: Cfg) -> MLE<()> {
6 let db_file = devdir(format!("{}/data.db", config.data).as_str()); 6 let db_file = format!("{}/data.db", config.data);
7 7
8 if !Path::new(&db_file).exists() { 8 if !Path::new(&db_file).exists() {
9 create(config, db_file)?; 9 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
156 Ok(current[0].clone()) 156 Ok(current[0].clone())
157} 157}
158 158
159#[tokio::test] 159// #[tokio::test]
160async fn download_updates_test() { 160// async fn download_updates_test() {
161 use crate::{ 161// use crate::{
162 modrinth::{Hash, Version, VersionFile, VersionType}, 162// modrinth::{Hash, Version, VersionFile, VersionType},
163 List, Modloader, 163// List, Modloader,
164 }; 164// };
165 165//
166 let config = Cfg::init("modlist.toml").unwrap(); 166// let config = Cfg::init().unwrap();
167 let current_list = List { 167// let current_list = List {
168 id: String::from("..."), 168// id: String::from("..."),
169 mc_version: String::from("..."), 169// mc_version: String::from("..."),
170 modloader: Modloader::Fabric, 170// modloader: Modloader::Fabric,
171 download_folder: String::from("./dev/tests/dl"), 171// download_folder: String::from("./dev/tests/dl"),
172 }; 172// };
173 173//
174 let versions = vec![Version { 174// let versions = vec![Version {
175 id: "dEqtGnT9".to_string(), 175// id: "dEqtGnT9".to_string(),
176 project_id: "kYuIpRLv".to_string(), 176// project_id: "kYuIpRLv".to_string(),
177 author_id: "Qnt13hO8".to_string(), 177// author_id: "Qnt13hO8".to_string(),
178 featured: true, 178// featured: true,
179 name: "1.2.2-1.19 - Fabric".to_string(), 179// name: "1.2.2-1.19 - Fabric".to_string(),
180 version_number: "1.2.2-1.19".to_string(), 180// version_number: "1.2.2-1.19".to_string(),
181 changelog: None, 181// changelog: None,
182 date_published: "2022-11-02T17:41:43.072267Z".to_string(), 182// date_published: "2022-11-02T17:41:43.072267Z".to_string(),
183 downloads: 58, 183// downloads: 58,
184 version_type: VersionType::release, 184// version_type: VersionType::release,
185 files: vec![VersionFile { 185// files: vec![VersionFile {
186 hashes: Hash { 186// hashes: Hash {
187 sha1: "fdc6dc39427fc92cc1d7ad8b275b5b83325e712b".to_string(), 187// sha1: "fdc6dc39427fc92cc1d7ad8b275b5b83325e712b".to_string(),
188 sha512: "5b372f00d6e5d6a5ef225c3897826b9f6a2be5506905f7f71b9e939779765b41be6f2a9b029cfc752ad0751d0d2d5f8bb4544408df1363eebdde15641e99a849".to_string() 188// sha512: "5b372f00d6e5d6a5ef225c3897826b9f6a2be5506905f7f71b9e939779765b41be6f2a9b029cfc752ad0751d0d2d5f8bb4544408df1363eebdde15641e99a849".to_string()
189 }, 189// },
190 url: "https://cdn.modrinth.com/data/kYuIpRLv/versions/dEqtGnT9/waveycapes-fabric-1.2.2-mc1.19.2.jar".to_string(), 190// url: "https://cdn.modrinth.com/data/kYuIpRLv/versions/dEqtGnT9/waveycapes-fabric-1.2.2-mc1.19.2.jar".to_string(),
191 filename: "waveycapes-fabric-1.2.2-mc1.19.2.jar".to_string(), 191// filename: "waveycapes-fabric-1.2.2-mc1.19.2.jar".to_string(),
192 primary: true, 192// primary: true,
193 size: 323176 193// size: 323176
194 }], 194// }],
195 game_versions: vec![ 195// game_versions: vec![
196 "1.19".to_string(), 196// "1.19".to_string(),
197 "1.19.1".to_string(), 197// "1.19.1".to_string(),
198 "1.19.2".to_string() 198// "1.19.2".to_string()
199 ], 199// ],
200 loaders: vec![ 200// loaders: vec![
201 "fabric".to_string() 201// "fabric".to_string()
202 ] 202// ]
203 }]; 203// }];
204 assert!(download_versions(current_list, config, versions) 204// assert!(download_versions(current_list, config, versions)
205 .await 205// .await
206 .is_ok()) 206// .is_ok())
207} 207// }
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::{
5 5
6use serde::{Deserialize, Serialize}; 6use serde::{Deserialize, Serialize};
7 7
8use crate::{devdir, error::MLE}; 8use crate::error::MLE;
9 9
10#[derive(Debug, Clone, Serialize, Deserialize)] 10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct Cfg { 11pub struct Cfg {
@@ -20,10 +20,13 @@ pub struct Apis {
20} 20}
21 21
22impl Cfg { 22impl Cfg {
23 pub fn init(filename: &str) -> MLE<Self> { 23 pub fn init(path: Option<String>) -> MLE<Self> {
24 let configfile = dirs::config_dir().unwrap().join(filename); 24 let configfile = match path {
25 Some(p) => String::from(p),
26 None => dirs::config_dir().unwrap().join("modlist.toml").to_string_lossy().into(),
27 };
25 28
26 let mut file = match File::open(devdir(configfile.to_str().unwrap())) { 29 let mut file = match File::open(&configfile) {
27 Ok(file) => file, 30 Ok(file) => file,
28 Err(err) => { 31 Err(err) => {
29 if err.kind() == std::io::ErrorKind::NotFound { 32 if err.kind() == std::io::ErrorKind::NotFound {
@@ -35,10 +38,10 @@ impl Cfg {
35 modrinth: String::from("https://api.modrinth.com/v2/"), 38 modrinth: String::from("https://api.modrinth.com/v2/"),
36 }, 39 },
37 }; 40 };
38 let mut file = File::create(devdir(configfile.to_str().unwrap()))?; 41 let mut file = File::create(&configfile)?;
39 println!("Created config file"); 42 println!("Created config file");
40 file.write_all(toml::to_string(&default_cfg)?.as_bytes())?; 43 file.write_all(toml::to_string(&default_cfg)?.as_bytes())?;
41 File::open(devdir(configfile.to_str().unwrap()))? 44 File::open(&configfile)?
42 } else { 45 } else {
43 return Err(err.into()); 46 return Err(err.into());
44 } 47 }
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;
4 4
5use crate::{ 5use crate::{
6 config::Cfg, 6 config::Cfg,
7 devdir,
8 error::{ErrorType, MLError, MLE}, 7 error::{ErrorType, MLError, MLE},
9 List, Modloader, 8 List, Modloader,
10}; 9};
11 10
12//MODS 11//MODS
13pub fn mods_insert(config: Cfg, id: &str, slug: &str, name: &str) -> MLE<()> { 12pub fn mods_insert(config: Cfg, id: &str, slug: &str, name: &str) -> MLE<()> {
14 let data = devdir(format!("{}/data.db", config.data).as_str()); 13 let data = format!("{}/data.db", config.data);
15 let connection = Connection::open(data)?; 14 let connection = Connection::open(data)?;
16 15
17 connection.execute( 16 connection.execute(
@@ -23,7 +22,7 @@ pub fn mods_insert(config: Cfg, id: &str, slug: &str, name: &str) -> MLE<()> {
23} 22}
24 23
25pub fn mods_get_all_ids(config: Cfg) -> Result<Vec<String>, Box<dyn std::error::Error>> { 24pub fn mods_get_all_ids(config: Cfg) -> Result<Vec<String>, Box<dyn std::error::Error>> {
26 let data = devdir(format!("{}/data.db", config.data).as_str()); 25 let data = format!("{}/data.db", config.data);
27 let connection = Connection::open(data).unwrap(); 26 let connection = Connection::open(data).unwrap();
28 27
29 let mut mods: Vec<String> = Vec::new(); 28 let mut mods: Vec<String> = Vec::new();
@@ -53,7 +52,7 @@ pub fn mods_get_all_ids(config: Cfg) -> Result<Vec<String>, Box<dyn std::error::
53pub fn mods_get_id(data: &str, slug: &str) -> MLE<String> { 52pub fn mods_get_id(data: &str, slug: &str) -> MLE<String> {
54 //TODO check if "slug" is id 53 //TODO check if "slug" is id
55 54
56 let data = devdir(format!("{}/data.db", data).as_str()); 55 let data = format!("{}/data.db", data);
57 let connection = Connection::open(data)?; 56 let connection = Connection::open(data)?;
58 57
59 let mut mod_id = String::new(); 58 let mut mod_id = String::new();
@@ -88,7 +87,7 @@ pub struct ModInfo {
88} 87}
89 88
90pub fn mods_get_info(config: Cfg, id: &str) -> MLE<ModInfo> { 89pub fn mods_get_info(config: Cfg, id: &str) -> MLE<ModInfo> {
91 let data = devdir(format!("{}/data.db", config.data).as_str()); 90 let data = format!("{}/data.db", config.data);
92 let connection = Connection::open(data)?; 91 let connection = Connection::open(data)?;
93 92
94 let mut mod_info: Option<ModInfo> = None; 93 let mut mod_info: Option<ModInfo> = None;
@@ -117,7 +116,7 @@ pub fn mods_get_info(config: Cfg, id: &str) -> MLE<ModInfo> {
117pub fn mods_remove(config: Cfg, id: String) -> MLE<()> { 116pub fn mods_remove(config: Cfg, id: String) -> MLE<()> {
118 println!("Removing mod {} from database", id); 117 println!("Removing mod {} from database", id);
119 118
120 let data = devdir(format!("{}/data.db", config.data).as_str()); 119 let data = format!("{}/data.db", config.data);
121 let connection = Connection::open(data)?; 120 let connection = Connection::open(data)?;
122 121
123 connection.execute("DELETE FROM mods WHERE id = ?", [id])?; 122 connection.execute("DELETE FROM mods WHERE id = ?", [id])?;
@@ -132,7 +131,7 @@ pub struct DBModlistVersions {
132} 131}
133 132
134pub fn mods_get_versions(config: Cfg, mods: Vec<String>) -> MLE<Vec<DBModlistVersions>> { 133pub fn mods_get_versions(config: Cfg, mods: Vec<String>) -> MLE<Vec<DBModlistVersions>> {
135 let data = devdir(format!("{}/data.db", config.data).as_str()); 134 let data = format!("{}/data.db", config.data);
136 let connection = Connection::open(data)?; 135 let connection = Connection::open(data)?;
137 136
138 if mods.is_empty() { 137 if mods.is_empty() {
@@ -188,7 +187,7 @@ pub fn userlist_insert(
188 current_link: &str, 187 current_link: &str,
189 set_version: bool, 188 set_version: bool,
190) -> MLE<()> { 189) -> MLE<()> {
191 let data = devdir(format!("{}/data.db", config.data).as_str()); 190 let data = format!("{}/data.db", config.data);
192 let connection = Connection::open(data)?; 191 let connection = Connection::open(data)?;
193 192
194 let sv = match set_version { 193 let sv = match set_version {
@@ -215,7 +214,7 @@ pub fn userlist_insert(
215} 214}
216 215
217pub fn userlist_get_all_ids(config: Cfg, list_id: String) -> MLE<Vec<String>> { 216pub fn userlist_get_all_ids(config: Cfg, list_id: String) -> MLE<Vec<String>> {
218 let data = devdir(format!("{}/data.db", config.data).as_str()); 217 let data = format!("{}/data.db", config.data);
219 let connection = Connection::open(data).unwrap(); 218 let connection = Connection::open(data).unwrap();
220 219
221 let mut mod_ids: Vec<String> = Vec::new(); 220 let mut mod_ids: Vec<String> = Vec::new();
@@ -234,7 +233,7 @@ pub fn userlist_get_all_ids(config: Cfg, list_id: String) -> MLE<Vec<String>> {
234} 233}
235 234
236pub fn userlist_remove(config: Cfg, list_id: &str, mod_id: &str) -> MLE<()> { 235pub fn userlist_remove(config: Cfg, list_id: &str, mod_id: &str) -> MLE<()> {
237 let data = devdir(format!("{}/data.db", config.data).as_str()); 236 let data = format!("{}/data.db", config.data);
238 let connection = Connection::open(data)?; 237 let connection = Connection::open(data)?;
239 238
240 connection.execute( 239 connection.execute(
@@ -249,7 +248,7 @@ pub fn userlist_get_applicable_versions(
249 list_id: String, 248 list_id: String,
250 mod_id: String, 249 mod_id: String,
251) -> MLE<String> { 250) -> MLE<String> {
252 let data = devdir(format!("{}/data.db", config.data).as_str()); 251 let data = format!("{}/data.db", config.data);
253 let connection = Connection::open(data).unwrap(); 252 let connection = Connection::open(data).unwrap();
254 253
255 let mut version: String = String::new(); 254 let mut version: String = String::new();
@@ -276,7 +275,7 @@ pub fn userlist_get_all_applicable_versions_with_mods(
276 config: Cfg, 275 config: Cfg,
277 list_id: String, 276 list_id: String,
278) -> MLE<Vec<(String, String)>> { 277) -> MLE<Vec<(String, String)>> {
279 let data = devdir(format!("{}/data.db", config.data).as_str()); 278 let data = format!("{}/data.db", config.data);
280 let connection = Connection::open(data)?; 279 let connection = Connection::open(data)?;
281 280
282 let mut versions: Vec<(String, String)> = Vec::new(); 281 let mut versions: Vec<(String, String)> = Vec::new();
@@ -302,7 +301,7 @@ pub fn userlist_get_all_applicable_versions_with_mods(
302} 301}
303 302
304pub fn userlist_get_current_version(config: Cfg, list_id: &str, mod_id: &str) -> MLE<String> { 303pub fn userlist_get_current_version(config: Cfg, list_id: &str, mod_id: &str) -> MLE<String> {
305 let data = devdir(format!("{}/data.db", config.data).as_str()); 304 let data = format!("{}/data.db", config.data);
306 let connection = Connection::open(data).unwrap(); 305 let connection = Connection::open(data).unwrap();
307 306
308 let mut version: String = String::new(); 307 let mut version: String = String::new();
@@ -324,7 +323,7 @@ pub fn userlist_get_all_current_version_ids(
324 config: Cfg, 323 config: Cfg,
325 list_id: String, 324 list_id: String,
326) -> Result<Vec<String>, Box<dyn std::error::Error>> { 325) -> Result<Vec<String>, Box<dyn std::error::Error>> {
327 let data = devdir(format!("{}/data.db", config.data).as_str()); 326 let data = format!("{}/data.db", config.data);
328 let connection = Connection::open(data)?; 327 let connection = Connection::open(data)?;
329 328
330 let mut versions: Vec<String> = Vec::new(); 329 let mut versions: Vec<String> = Vec::new();
@@ -350,7 +349,7 @@ pub fn userlist_get_all_current_versions_with_mods(
350 config: Cfg, 349 config: Cfg,
351 list_id: String, 350 list_id: String,
352) -> Result<Vec<(String, String)>, Box<dyn std::error::Error>> { 351) -> Result<Vec<(String, String)>, Box<dyn std::error::Error>> {
353 let data = devdir(format!("{}/data.db", config.data).as_str()); 352 let data = format!("{}/data.db", config.data);
354 let connection = Connection::open(data)?; 353 let connection = Connection::open(data)?;
355 354
356 let mut versions: Vec<(String, String)> = Vec::new(); 355 let mut versions: Vec<(String, String)> = Vec::new();
@@ -379,7 +378,7 @@ pub fn userlist_get_all_current_versions_with_mods(
379} 378}
380 379
381pub fn userlist_get_set_version(config: Cfg, list_id: &str, mod_id: &str) -> MLE<bool> { 380pub fn userlist_get_set_version(config: Cfg, list_id: &str, mod_id: &str) -> MLE<bool> {
382 let data = devdir(format!("{}/data.db", config.data).as_str()); 381 let data = format!("{}/data.db", config.data);
383 let connection = Connection::open(data).unwrap(); 382 let connection = Connection::open(data).unwrap();
384 383
385 let mut set_version: bool = false; 384 let mut set_version: bool = false;
@@ -402,7 +401,7 @@ pub fn userlist_change_versions(
402 link: String, 401 link: String,
403 mod_id: String, 402 mod_id: String,
404) -> MLE<()> { 403) -> MLE<()> {
405 let data = devdir(format!("{}/data.db", config.data).as_str()); 404 let data = format!("{}/data.db", config.data);
406 let connection = Connection::open(data)?; 405 let connection = Connection::open(data)?;
407 406
408 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])?; 407 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(
415 disabled_version: String, 414 disabled_version: String,
416 mod_id: String, 415 mod_id: String,
417) -> MLE<()> { 416) -> MLE<()> {
418 let data = devdir(format!("{}/data.db", config.data).as_str()); 417 let data = format!("{}/data.db", config.data);
419 let connection = Connection::open(data)?; 418 let connection = Connection::open(data)?;
420 419
421 let currently_disabled_versions = 420 let currently_disabled_versions =
@@ -437,7 +436,7 @@ pub fn userlist_add_disabled_versions(
437} 436}
438 437
439pub fn userlist_get_disabled_versions(config: Cfg, list_id: String, mod_id: String) -> MLE<String> { 438pub fn userlist_get_disabled_versions(config: Cfg, list_id: String, mod_id: String) -> MLE<String> {
440 let data = devdir(format!("{}/data.db", config.data).as_str()); 439 let data = format!("{}/data.db", config.data);
441 let connection = Connection::open(data).unwrap(); 440 let connection = Connection::open(data).unwrap();
442 441
443 let mut version: String = String::new(); 442 let mut version: String = String::new();
@@ -459,7 +458,7 @@ pub fn userlist_get_all_downloads(
459 config: Cfg, 458 config: Cfg,
460 list_id: String, 459 list_id: String,
461) -> Result<Vec<String>, Box<dyn std::error::Error>> { 460) -> Result<Vec<String>, Box<dyn std::error::Error>> {
462 let data = devdir(format!("{}/data.db", config.data).as_str()); 461 let data = format!("{}/data.db", config.data);
463 let connection = Connection::open(data).unwrap(); 462 let connection = Connection::open(data).unwrap();
464 463
465 let mut links: Vec<String> = Vec::new(); 464 let mut links: Vec<String> = Vec::new();
@@ -494,7 +493,7 @@ pub fn lists_insert(
494) -> MLE<()> { 493) -> MLE<()> {
495 println!("Creating list {}", id); 494 println!("Creating list {}", id);
496 495
497 let data = devdir(format!("{}/data.db", config.data).as_str()); 496 let data = format!("{}/data.db", config.data);
498 let connection = Connection::open(data)?; 497 let connection = Connection::open(data)?;
499 498
500 connection.execute( 499 connection.execute(
@@ -512,7 +511,7 @@ pub fn lists_insert(
512} 511}
513 512
514pub fn lists_remove(config: Cfg, id: String) -> MLE<()> { 513pub fn lists_remove(config: Cfg, id: String) -> MLE<()> {
515 let data = devdir(format!("{}/data.db", config.data).as_str()); 514 let data = format!("{}/data.db", config.data);
516 let connection = Connection::open(data)?; 515 let connection = Connection::open(data)?;
517 516
518 connection.execute("DELETE FROM lists WHERE id = ?", [&id])?; 517 connection.execute("DELETE FROM lists WHERE id = ?", [&id])?;
@@ -521,7 +520,7 @@ pub fn lists_remove(config: Cfg, id: String) -> MLE<()> {
521} 520}
522 521
523pub fn lists_get(config: Cfg, list_id: String) -> MLE<List> { 522pub fn lists_get(config: Cfg, list_id: String) -> MLE<List> {
524 let data = devdir(format!("{}/data.db", config.data).as_str()); 523 let data = format!("{}/data.db", config.data);
525 let connection = Connection::open(data).unwrap(); 524 let connection = Connection::open(data).unwrap();
526 525
527 let mut list = List { 526 let mut list = List {
@@ -559,7 +558,7 @@ pub fn lists_get(config: Cfg, list_id: String) -> MLE<List> {
559} 558}
560 559
561pub fn lists_version(config: Cfg, list_id: &str, version: &str) -> MLE<()> { 560pub fn lists_version(config: Cfg, list_id: &str, version: &str) -> MLE<()> {
562 let data = devdir(format!("{}/data.db", config.data).as_str()); 561 let data = format!("{}/data.db", config.data);
563 let connection = Connection::open(data).unwrap(); 562 let connection = Connection::open(data).unwrap();
564 563
565 connection.execute( 564 connection.execute(
@@ -570,7 +569,7 @@ pub fn lists_version(config: Cfg, list_id: &str, version: &str) -> MLE<()> {
570} 569}
571 570
572pub fn lists_get_all_ids(config: Cfg) -> MLE<Vec<String>> { 571pub fn lists_get_all_ids(config: Cfg) -> MLE<Vec<String>> {
573 let data = devdir(format!("{}/data.db", config.data).as_str()); 572 let data = format!("{}/data.db", config.data);
574 let connection = Connection::open(data).unwrap(); 573 let connection = Connection::open(data).unwrap();
575 574
576 let mut list_ids: Vec<String> = Vec::new(); 575 let mut list_ids: Vec<String> = Vec::new();
@@ -589,7 +588,7 @@ pub fn lists_get_all_ids(config: Cfg) -> MLE<Vec<String>> {
589 588
590//config 589//config
591pub fn config_change_current_list(config: Cfg, id: String) -> MLE<()> { 590pub fn config_change_current_list(config: Cfg, id: String) -> MLE<()> {
592 let data = devdir(format!("{}/data.db", config.data).as_str()); 591 let data = format!("{}/data.db", config.data);
593 let connection = Connection::open(data)?; 592 let connection = Connection::open(data)?;
594 593
595 connection.execute( 594 connection.execute(
@@ -600,7 +599,7 @@ pub fn config_change_current_list(config: Cfg, id: String) -> MLE<()> {
600} 599}
601 600
602pub fn config_get_current_list(config: Cfg) -> MLE<String> { 601pub fn config_get_current_list(config: Cfg) -> MLE<String> {
603 let data = devdir(format!("{}/data.db", config.data).as_str()); 602 let data = format!("{}/data.db", config.data);
604 let connection = Connection::open(data).unwrap(); 603 let connection = Connection::open(data).unwrap();
605 604
606 let mut list_id = String::new(); 605 let mut list_id = String::new();
@@ -625,7 +624,7 @@ pub fn s_userlist_update_download(
625 mod_id: String, 624 mod_id: String,
626 link: String, 625 link: String,
627) -> Result<(), Box<dyn std::error::Error>> { 626) -> Result<(), Box<dyn std::error::Error>> {
628 let data = devdir(format!("{}/data.db", config.data).as_str()); 627 let data = format!("{}/data.db", config.data);
629 let connection = Connection::open(data)?; 628 let connection = Connection::open(data)?;
630 629
631 connection.execute( 630 connection.execute(
@@ -640,7 +639,7 @@ pub fn s_userlist_update_download(
640} 639}
641 640
642pub fn s_config_create_version(config: Cfg) -> Result<(), Box<dyn std::error::Error>> { 641pub fn s_config_create_version(config: Cfg) -> Result<(), Box<dyn std::error::Error>> {
643 let data = devdir(format!("{}/data.db", config.data).as_str()); 642 let data = format!("{}/data.db", config.data);
644 let connection = Connection::open(data)?; 643 let connection = Connection::open(data)?;
645 644
646 connection.execute( 645 connection.execute(
@@ -651,7 +650,7 @@ pub fn s_config_create_version(config: Cfg) -> Result<(), Box<dyn std::error::Er
651} 650}
652 651
653pub fn s_config_update_version(config: Cfg, ver: String) -> Result<(), Box<dyn std::error::Error>> { 652pub fn s_config_update_version(config: Cfg, ver: String) -> Result<(), Box<dyn std::error::Error>> {
654 let data = devdir(format!("{}/data.db", config.data).as_str()); 653 let data = format!("{}/data.db", config.data);
655 let connection = Connection::open(data)?; 654 let connection = Connection::open(data)?;
656 655
657 connection.execute( 656 connection.execute(
@@ -662,7 +661,7 @@ pub fn s_config_update_version(config: Cfg, ver: String) -> Result<(), Box<dyn s
662} 661}
663 662
664pub fn s_config_get_version(config: Cfg) -> Result<String, Box<dyn std::error::Error>> { 663pub fn s_config_get_version(config: Cfg) -> Result<String, Box<dyn std::error::Error>> {
665 let data = devdir(format!("{}/data.db", config.data).as_str()); 664 let data = format!("{}/data.db", config.data);
666 let connection = Connection::open(data)?; 665 let connection = Connection::open(data)?;
667 666
668 let mut version: String = String::new(); 667 let mut version: String = String::new();
@@ -689,7 +688,7 @@ pub fn s_insert_column(
689 c_type: String, 688 c_type: String,
690 default: Option<String>, 689 default: Option<String>,
691) -> Result<(), Box<dyn std::error::Error>> { 690) -> Result<(), Box<dyn std::error::Error>> {
692 let data = devdir(format!("{}/data.db", config.data).as_str()); 691 let data = format!("{}/data.db", config.data);
693 let connection = Connection::open(data)?; 692 let connection = Connection::open(data)?;
694 693
695 let mut sql = format!("ALTER TABLE {} ADD '{}' {}", table, column, c_type); 694 let mut sql = format!("ALTER TABLE {} ADD '{}' {}", table, column, c_type);
@@ -705,7 +704,7 @@ pub fn s_insert_column(
705pub fn db_setup(config: Cfg) -> MLE<()> { 704pub fn db_setup(config: Cfg) -> MLE<()> {
706 println!("Initiating database"); 705 println!("Initiating database");
707 706
708 let data = devdir(format!("{}/data.db", config.data).as_str()); 707 let data = format!("{}/data.db", config.data);
709 let connection = Connection::open(data)?; 708 let connection = Connection::open(data)?;
710 709
711 connection.execute_batch( 710 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::{
11 db::{mods_get_info, userlist_add_disabled_versions}, 11 db::{mods_get_info, userlist_add_disabled_versions},
12 error::{ErrorType, MLError, MLE}, 12 error::{ErrorType, MLError, MLE},
13 modrinth::Version, 13 modrinth::Version,
14 List, cache::{get_cached_versions, copy_cached_version}, devdir, 14 List, cache::{get_cached_versions, copy_cached_version},
15}; 15};
16 16
17pub async fn download_versions(list: List, config: Cfg, versions: Vec<Version>) -> MLE<String> { 17pub async fn download_versions(list: List, config: Cfg, versions: Vec<Version>) -> MLE<String> {
18 18
19 let mut cached = get_cached_versions(&devdir(&config.cache)); 19 let mut cached = get_cached_versions(&config.cache);
20 20
21 println!("{:#?}", cached); 21 println!("{:#?}", cached);
22 22
@@ -59,7 +59,7 @@ pub async fn download_versions(list: List, config: Cfg, versions: Vec<Version>)
59 //Force flush of stdout, else print! doesn't print instantly 59 //Force flush of stdout, else print! doesn't print instantly
60 std::io::stdout().flush().unwrap(); 60 std::io::stdout().flush().unwrap();
61 let dl_path_file = format!("{}/{}", list.download_folder, filename); 61 let dl_path_file = format!("{}/{}", list.download_folder, filename);
62 let cache_path = format!("{}/{}", devdir(&config.clone().cache), filename); 62 let cache_path = format!("{}/{}", &config.clone().cache, filename);
63 // println!("{}:{}", dl_path_file, cache_path); 63 // println!("{}:{}", dl_path_file, cache_path);
64 copy(dl_path_file, cache_path)?; 64 copy(dl_path_file, cache_path)?;
65 println!(" ✓"); 65 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;
6pub mod files; 6pub mod files;
7pub mod cache; 7pub mod cache;
8 8
9use std::{fmt::Display, path::Path}; 9use std::fmt::Display;
10 10
11pub use apis::*; 11pub use apis::*;
12pub use commands::*; 12pub use commands::*;
@@ -36,17 +36,3 @@ impl Display for Modloader {
36 } 36 }
37 } 37 }
38} 38}
39
40pub fn devdir(path: &str) -> String {
41 let p = Path::new(path);
42 let dev = std::env::var("DEV");
43 let lvl = match dev {
44 Ok(dev) => dev.parse::<i32>().unwrap(),
45 Err(..) => 0,
46 };
47 if lvl >= 1 {
48 format!("./dev/{}", p.file_name().unwrap().to_str().unwrap())
49 } else {
50 String::from(path)
51 }
52}
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};
2use modlist::{ 2use modlist::{
3 config::Cfg, 3 config::Cfg,
4 db::{config_get_current_list, lists_get, lists_get_all_ids}, 4 db::{config_get_current_list, lists_get, lists_get_all_ids},
5 devdir, download, export, get_current_list, import, list_add, list_change, list_remove, 5 download, export, get_current_list, import, list_add, list_change, list_remove,
6 list_version, mod_add, mod_remove, update, IDSelector, List, Modloader, 6 list_version, mod_add, mod_remove, update, IDSelector, List, Modloader,
7}; 7};
8 8
@@ -14,6 +14,10 @@ use modlist::{
14struct Cli { 14struct Cli {
15 #[command(subcommand)] 15 #[command(subcommand)]
16 command: Commands, 16 command: Commands,
17
18 /// config file path
19 #[arg(short, long)]
20 config: Option<String>,
17} 21}
18 22
19#[derive(Subcommand)] 23#[derive(Subcommand)]
@@ -142,8 +146,8 @@ enum ListCommands {
142#[tokio::main] 146#[tokio::main]
143async fn main() { 147async fn main() {
144 let cli = Cli::parse(); 148 let cli = Cli::parse();
145 149
146 let config = Cfg::init("modlist.toml").unwrap(); 150 let config = Cfg::init(cli.config).unwrap();
147 println!("{:?}", config); 151 println!("{:?}", config);
148 152
149 //TODO setup? maybe setup on install 153 //TODO setup? maybe setup on install
@@ -250,15 +254,14 @@ async fn main() {
250 Commands::Import { file, download } => { 254 Commands::Import { file, download } => {
251 let filestr: String = match file { 255 let filestr: String = match file {
252 Some(args) => args, 256 Some(args) => args,
253 None => devdir( 257 None =>
254 dirs::home_dir() 258 dirs::home_dir()
255 .unwrap() 259 .unwrap()
256 .join("mlexport.toml") 260 .join("mlexport.toml")
257 .into_os_string() 261 .into_os_string()
258 .into_string() 262 .into_string()
259 .unwrap() 263 .unwrap()
260 .as_str(), 264 ,
261 ),
262 }; 265 };
263 266
264 import(config, filestr, download).await 267 import(config, filestr, download).await