summaryrefslogtreecommitdiff
path: root/src/commands/modification.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/commands/modification.rs')
-rw-r--r--src/commands/modification.rs25
1 files changed, 14 insertions, 11 deletions
diff --git a/src/commands/modification.rs b/src/commands/modification.rs
index 519a0cb..8e39d11 100644
--- a/src/commands/modification.rs
+++ b/src/commands/modification.rs
@@ -1,12 +1,12 @@
1use std::io::{Error, ErrorKind}; 1use std::io::{Error, ErrorKind};
2 2
3use crate::{modrinth::{project, versions, extract_current_version}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, mods_get_all_ids, userlist_get_all_ids}, input::{Input, Subcmd}, get_current_list}; 3use crate::{modrinth::{project, versions, extract_current_version}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, mods_get_all_ids, userlist_get_all_ids}, input::{Input, Subcmd}, get_current_list, download_versions};
4 4
5pub async fn modification(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { 5pub async fn modification(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> {
6 6
7 match input.subcommand.ok_or("")? { 7 match input.subcommand.as_ref().ok_or("")? {
8 Subcmd::Add => { 8 Subcmd::Add => {
9 add(config, input.args.ok_or("")?).await 9 add(config, input).await
10 }, 10 },
11 Subcmd::Remove => { 11 Subcmd::Remove => {
12 remove(config, input.args.ok_or("")?) 12 remove(config, input.args.ok_or("")?)
@@ -15,23 +15,23 @@ pub async fn modification(config: Cfg, input: Input) -> Result<(), Box<dyn std::
15 } 15 }
16} 16}
17 17
18async fn add(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> { 18async fn add(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> {
19 //TODO! DO NOT PANIC IF MOD IS ALREADY IN MODS DB 19
20 let args = input.args.ok_or("")?;
21
20 if args.is_empty() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); }; 22 if args.is_empty() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); };
21 23
22 let current_list = get_current_list(config.clone())?; 24 let current_list = get_current_list(config.clone())?;
23 25
24 let project = project(String::from(&config.apis.modrinth), &args[0]).await; 26 let project = project(String::from(&config.apis.modrinth), &args[0]).await;
25 27
26 if project.versions.is_empty() { panic!("This should never happen"); };
27
28 let available_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), current_list.clone()).await; 28 let available_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), current_list.clone()).await;
29 29
30 let current_id = extract_current_version(available_versions.clone())?; 30 let current_id = extract_current_version(available_versions.clone())?;
31 31
32 let current_version = available_versions.clone().into_iter().find(|v| v.id == current_id).unwrap(); 32 let current_version = available_versions.clone().into_iter().find(|v| v.id == current_id).unwrap();
33 33
34 let file = current_version.files.into_iter().find(|f| f.primary).unwrap().url; 34 let file = current_version.clone().files.into_iter().find(|f| f.primary).unwrap().url;
35 35
36 let mut available_versions_vec: Vec<String> = Vec::new(); 36 let mut available_versions_vec: Vec<String> = Vec::new();
37 for ver in available_versions { 37 for ver in available_versions {
@@ -43,16 +43,16 @@ async fn add(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::E
43 if mods.contains(&project.id) { 43 if mods.contains(&project.id) {
44 return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_ON_LIST"))); } 44 return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_ON_LIST"))); }
45 else { 45 else {
46 userlist_insert(config.clone(), current_list.id, String::from(&project.id), current_version.id, available_versions_vec, file)?; 46 userlist_insert(config.clone(), String::from(&current_list.id), String::from(&project.id), String::from(&current_version.id), available_versions_vec, file)?;
47 } 47 }
48 }, 48 },
49 Err(..) => userlist_insert(config.clone(), current_list.id, String::from(&project.id), current_version.id, available_versions_vec, file)?, 49 Err(..) => userlist_insert(config.clone(), String::from(&current_list.id), String::from(&project.id), String::from(&current_version.id), available_versions_vec, file)?,
50 }; 50 };
51 51
52 match mods_get_all_ids(config.clone()) { 52 match mods_get_all_ids(config.clone()) {
53 Ok(mods) => { 53 Ok(mods) => {
54 if mods.contains(&project.id) { 54 if mods.contains(&project.id) {
55 return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_IN_DATABASE"))) 55 //return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_IN_DATABASE")))
56 } else { 56 } else {
57 mods_insert(config.clone(), String::from(&project.id), String::from(&project.title), project.versions)?; 57 mods_insert(config.clone(), String::from(&project.id), String::from(&project.title), project.versions)?;
58 } 58 }
@@ -61,6 +61,9 @@ async fn add(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::E
61 mods_insert(config.clone(), String::from(&project.id), String::from(&project.title), project.versions)?; 61 mods_insert(config.clone(), String::from(&project.id), String::from(&project.title), project.versions)?;
62 }, 62 },
63 }; 63 };
64
65 if !input.disable_download { download_versions(current_list, vec![current_version]).await?; }
66
64 Ok(()) 67 Ok(())
65} 68}
66 69