summaryrefslogtreecommitdiff
path: root/src/commands
diff options
context:
space:
mode:
authorfxqnlr <[email protected]>2022-11-03 21:34:04 +0100
committerfxqnlr <[email protected]>2022-11-03 21:34:04 +0100
commit96cc5257de09682df345e768dc2a91303f9b36c9 (patch)
treef505d14c581e2bef4cfe222bd1069661bedd22e0 /src/commands
parentb125dfd03084fff47ab8e90d002c6699b762d998 (diff)
downloadmodlist-96cc5257de09682df345e768dc2a91303f9b36c9.tar
modlist-96cc5257de09682df345e768dc2a91303f9b36c9.tar.gz
modlist-96cc5257de09682df345e768dc2a91303f9b36c9.zip
added update beginnings; init of tests
Diffstat (limited to 'src/commands')
-rw-r--r--src/commands/add.rs52
-rw-r--r--src/commands/list.rs16
-rw-r--r--src/commands/mod.rs6
-rw-r--r--src/commands/modification.rs88
-rw-r--r--src/commands/update.rs40
5 files changed, 146 insertions, 56 deletions
diff --git a/src/commands/add.rs b/src/commands/add.rs
deleted file mode 100644
index ed4a6d8..0000000
--- a/src/commands/add.rs
+++ /dev/null
@@ -1,52 +0,0 @@
1use std::io::{Error, ErrorKind};
2
3use crate::{modrinth::{project, versions}, config::Cfg, db::insert_mod, Modloader, input::Input};
4
5pub async fn modification(config: Cfg, args: Option<Vec<String>>) -> Result<(), Box<dyn std::error::Error>> {
6
7 if args.is_none() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))) }
8
9 let arguments = Input::from(args.unwrap().join(" "))?;
10
11 if arguments.args.is_none() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); };
12
13 match arguments.command.as_str() {
14 "add" => {
15 add(config, arguments.args.unwrap()).await
16 },
17 _ => Err(Box::new(Error::new(ErrorKind::InvalidInput, "UNKNOWN_SUBCOMMAND")))
18 }
19}
20
21pub async fn add(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
22
23 if args.len() < 1 { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); };
24
25 let project = project(String::from(&config.apis.modrinth), &args[0]).await;
26
27 dbg!(&project);
28
29 let loader = Modloader::Fabric;
30
31 if project.versions.is_empty() { panic!("This should never happen"); };
32
33 let current_version = get_current(config, String::from(&project.id)).await?;
34
35 //add to current list and mod table
36 match insert_mod(project.id, project.title, current_version, project.versions, loader, String::from("1.19.2")) {
37 Err(err) => { Err(Box::new(err)) },
38 Ok(()) => Ok(()),
39 }
40
41}
42
43async fn get_current(config: Cfg, id: String) -> Result<String, Box<dyn std::error::Error>> {
44 let available_versions = versions(config.apis.modrinth, id, Modloader::Fabric, String::from("1.19.2")).await;
45
46 match available_versions.len() {
47 0 => Err(Box::new(Error::new(ErrorKind::NotFound, "NO_VERSIONS_AVAILABLE"))),
48 //TODO compare publish dates
49 1.. => Ok(available_versions[0].id.to_string()),
50 _ => panic!("available_versions should never be negative"),
51 }
52}
diff --git a/src/commands/list.rs b/src/commands/list.rs
index 6c260ce..3dfe1ad 100644
--- a/src/commands/list.rs
+++ b/src/commands/list.rs
@@ -1,12 +1,19 @@
1use std::io::{Error, ErrorKind}; 1use std::io::{Error, ErrorKind};
2 2
3use crate::{db::{insert_list, remove_list, change_list, get_lists, get_current_list}, Modloader, config::Cfg, input::Input}; 3use crate::{db::{insert_list, remove_list, change_list, get_lists, get_current_list_id, get_list}, Modloader, config::Cfg, input::Input};
4
5#[derive(Clone)]
6pub struct List {
7 pub id: String,
8 pub mc_version: String,
9 pub modloader: Modloader,
10}
4 11
5pub fn list(config: Cfg, args: Option<Vec<String>>) -> Result<(), Box<dyn std::error::Error>> { 12pub fn list(config: Cfg, args: Option<Vec<String>>) -> Result<(), Box<dyn std::error::Error>> {
6 13
7 if args.is_none() { 14 if args.is_none() {
8 let lists = get_lists(config.clone())?; 15 let lists = get_lists(config.clone())?;
9 let current_list = get_current_list(config)?; 16 let current_list = get_current_list_id(config)?;
10 println!("Your lists:\n{}\n-----\nCurrently selected list: \"{}\"", lists.join(",\n"), current_list); 17 println!("Your lists:\n{}\n-----\nCurrently selected list: \"{}\"", lists.join(",\n"), current_list);
11 return Ok(()); 18 return Ok(());
12 } 19 }
@@ -29,6 +36,11 @@ pub fn list(config: Cfg, args: Option<Vec<String>>) -> Result<(), Box<dyn std::e
29 } 36 }
30} 37}
31 38
39pub fn get_current_list(config: Cfg) -> Result<List, Box<dyn std::error::Error>> {
40 let id = get_current_list_id(config.clone())?;
41 get_list(config, id)
42}
43
32fn add(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> { 44fn add(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
33 match args.len() { 45 match args.len() {
34 1 | 2 => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))), 46 1 | 2 => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))),
diff --git a/src/commands/mod.rs b/src/commands/mod.rs
index 6432746..5d008fd 100644
--- a/src/commands/mod.rs
+++ b/src/commands/mod.rs
@@ -1,5 +1,7 @@
1pub mod add; 1pub mod modification;
2pub mod list; 2pub mod list;
3pub mod update;
3 4
4pub use add::*; 5pub use modification::*;
5pub use list::*; 6pub use list::*;
7pub use update::*;
diff --git a/src/commands/modification.rs b/src/commands/modification.rs
new file mode 100644
index 0000000..43e2180
--- /dev/null
+++ b/src/commands/modification.rs
@@ -0,0 +1,88 @@
1use std::io::{Error, ErrorKind};
2
3use crate::{modrinth::{project, versions, Version}, config::Cfg, db::{insert_mod, remove_mod_from_list, get_mod_id, insert_mod_in_list, get_mods, get_mods_from_list}, input::Input, get_current_list};
4
5pub async fn modification(config: Cfg, args: Option<Vec<String>>) -> Result<(), Box<dyn std::error::Error>> {
6
7 if args.is_none() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))) }
8
9 let arguments = Input::from(args.unwrap().join(" "))?;
10
11 if arguments.args.is_none() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); };
12
13 match arguments.command.as_str() {
14 "add" => {
15 add(config, arguments.args.unwrap()).await
16 },
17 "remove" => {
18 remove(config, arguments.args.unwrap())
19 },
20 _ => Err(Box::new(Error::new(ErrorKind::InvalidInput, "UNKNOWN_SUBCOMMAND")))
21 }
22}
23
24async fn add(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
25
26 if args.is_empty() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); };
27
28 let current_list = get_current_list(config.clone())?;
29
30 let project = project(String::from(&config.apis.modrinth), &args[0]).await;
31
32 dbg!(&project);
33
34 if project.versions.is_empty() { panic!("This should never happen"); };
35
36 let available_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), current_list.clone()).await;
37
38 let current_version = extract_current_version(available_versions.clone())?;
39
40 //add to current list and mod table
41 match get_mods_from_list(config.clone(), current_list.clone()) {
42 Ok(mods) => {
43 dbg!(&mods);
44 if mods.contains(&project.id) {
45 return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_ON_LIST"))); }
46 else {
47 insert_mod_in_list(config.clone(), current_list.clone(), String::from(&project.id), current_version, available_versions)?;
48 }
49 },
50 Err(..) => insert_mod_in_list(config.clone(), current_list, String::from(&project.id), current_version, available_versions)?,
51 };
52
53 match get_mods(config.clone()) {
54 Ok(mods) => {
55 dbg!(&mods);
56 if mods.contains(&project.id) {
57 return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_IN_DATABASE")))
58 } else {
59 insert_mod(config.clone(), String::from(&project.id), String::from(&project.title), project.versions)?;
60 }
61 },
62 Err(..) => insert_mod(config.clone(), String::from(&project.id), String::from(&project.title), project.versions)?,
63 };
64
65 Ok(())
66}
67
68fn remove(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
69 if args.is_empty() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); };
70
71 let current_list = get_current_list(config.clone())?;
72 let mod_id = get_mod_id(config.clone(), String::from(&args[0]))?;
73
74 //TODO implement remove from modlist if not in any other lists && config clean is true
75 match remove_mod_from_list(config, current_list, mod_id) {
76 Err(err) => { Err(Box::new(err)) },
77 Ok(()) => Ok(()),
78 }
79}
80
81fn extract_current_version(versions: Vec<Version>) -> Result<String, Box<dyn std::error::Error>> {
82 match versions.len() {
83 0 => Err(Box::new(Error::new(ErrorKind::NotFound, "NO_VERSIONS_AVAILABLE"))),
84 //TODO compare publish dates
85 1.. => Ok(versions[0].id.to_string()),
86 _ => panic!("available_versions should never be negative"),
87 }
88}
diff --git a/src/commands/update.rs b/src/commands/update.rs
new file mode 100644
index 0000000..14c37ec
--- /dev/null
+++ b/src/commands/update.rs
@@ -0,0 +1,40 @@
1use std::io::{Error, ErrorKind};
2
3use crate::{config::Cfg, modrinth::projects, get_current_list, db::{get_mods_from_list, get_versions}};
4
5pub async fn update(config: Cfg) -> Result<(), Box<dyn std::error::Error>> {
6
7 let current_list = get_current_list(config.clone())?;
8
9 let mods = get_mods_from_list(config.clone(), current_list)?;
10
11 let mut projects = projects(String::from(&config.apis.modrinth), mods.clone()).await;
12
13 let mut versions = get_versions(config, mods)?;
14
15 projects.sort_by_key(|p| p.id.clone());
16
17 versions.sort_by_key(|v| v.mod_id.clone());
18
19 let mut update_stack: Vec<String> = vec![];
20
21 for (index, project) in projects.iter().enumerate() {
22
23 let cmp_version = &versions[index];
24
25 let p_id = &project.id;
26 let v_id = &cmp_version.mod_id;
27
28 if p_id != v_id { return Err(Box::new(Error::new(ErrorKind::Other, "COMPARE_SORTING_ERR"))); };
29 println!("{}:{}", p_id, v_id);
30
31 if project.versions.join("|") != cmp_version.versions {
32 update_stack.push(String::from(&project.id));
33 };
34 };
35
36 //TODO UPDATE
37 dbg!(update_stack);
38
39 Ok(())
40}