diff options
Diffstat (limited to 'src/commands')
-rw-r--r-- | src/commands/io.rs | 35 | ||||
-rw-r--r-- | src/commands/modification.rs | 115 |
2 files changed, 84 insertions, 66 deletions
diff --git a/src/commands/io.rs b/src/commands/io.rs index 47991c5..2edb95d 100644 --- a/src/commands/io.rs +++ b/src/commands/io.rs | |||
@@ -34,17 +34,25 @@ impl ExportList { | |||
34 | 34 | ||
35 | pub async fn io(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { | 35 | pub async fn io(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { |
36 | 36 | ||
37 | match input.subcommand.ok_or("INVALID_INPUT")? { | 37 | match input.subcommand.clone().ok_or("INVALID_INPUT")? { |
38 | Subcmd::Export => { export(config, input.args)? }, | 38 | Subcmd::Export => { export(config, input)? }, |
39 | Subcmd::Import => { import(config).await? }, | 39 | Subcmd::Import => { import(config, input.args).await? }, |
40 | _ => { }, | 40 | _ => { }, |
41 | } | 41 | } |
42 | 42 | ||
43 | Ok(()) | 43 | Ok(()) |
44 | } | 44 | } |
45 | 45 | ||
46 | fn export(config: Cfg, _args: Option<Vec<String>>) -> Result<(), Box<dyn std::error::Error>> { | 46 | fn export(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { |
47 | let list_ids = lists_get_all_ids(config.clone())?; | 47 | let mut list_ids: Vec<String> = vec![]; |
48 | if input.all_lists { | ||
49 | list_ids = lists_get_all_ids(config.clone())?; | ||
50 | } else { | ||
51 | let args = input.args.ok_or("NO_ARGS")?; | ||
52 | for arg in args { | ||
53 | list_ids.push(lists_get(config.clone(), arg)?.id); | ||
54 | } | ||
55 | } | ||
48 | let mut lists: Vec<ExportList> = vec![]; | 56 | let mut lists: Vec<ExportList> = vec![]; |
49 | for list_id in list_ids { | 57 | for list_id in list_ids { |
50 | lists.push(ExportList::from(config.clone(), String::from(list_id), true)?); | 58 | lists.push(ExportList::from(config.clone(), String::from(list_id), true)?); |
@@ -58,9 +66,14 @@ fn export(config: Cfg, _args: Option<Vec<String>>) -> Result<(), Box<dyn std::er | |||
58 | Ok(()) | 66 | Ok(()) |
59 | } | 67 | } |
60 | 68 | ||
61 | async fn import(config: Cfg) -> Result<(), Box<dyn std::error::Error>> { | 69 | async fn import(config: Cfg, args: Option<Vec<String>>) -> Result<(), Box<dyn std::error::Error>> { |
70 | |||
71 | let filestr: String = match args { | ||
72 | Some(args) => String::from(&args[0]), | ||
73 | None => String::from("export.toml"), | ||
74 | }; | ||
62 | 75 | ||
63 | let mut file = File::open("export.toml")?; | 76 | let mut file = File::open(filestr)?; |
64 | let mut content = String::new(); | 77 | let mut content = String::new(); |
65 | file.read_to_string(&mut content)?; | 78 | file.read_to_string(&mut content)?; |
66 | let export: Export = toml::from_str(&content)?; | 79 | let export: Export = toml::from_str(&content)?; |
@@ -70,12 +83,12 @@ async fn import(config: Cfg) -> Result<(), Box<dyn std::error::Error>> { | |||
70 | for exportlist in export.lists { | 83 | for exportlist in export.lists { |
71 | let list = List { id: exportlist.id, mc_version: exportlist.mc_version, modloader: Modloader::from(&exportlist.launcher)?, download_folder: exportlist.download_folder.ok_or("NO_DL")? }; | 84 | let list = List { id: exportlist.id, mc_version: exportlist.mc_version, modloader: Modloader::from(&exportlist.launcher)?, download_folder: exportlist.download_folder.ok_or("NO_DL")? }; |
72 | lists_insert(config.clone(), list.id.clone(), list.mc_version.clone(), list.modloader.clone(), String::from(&list.download_folder))?; | 85 | lists_insert(config.clone(), list.id.clone(), list.mc_version.clone(), list.modloader.clone(), String::from(&list.download_folder))?; |
73 | //TODO currently workaround, too many requests | ||
74 | let mods: Vec<&str> = exportlist.mods.split("|").collect(); | 86 | let mods: Vec<&str> = exportlist.mods.split("|").collect(); |
87 | let mut mod_ids = vec![]; | ||
75 | for mod_id in mods { | 88 | for mod_id in mods { |
76 | println!("Adding {}", mod_id); | 89 | mod_ids.push(String::from(mod_id)); |
77 | mod_add(config.clone(), mod_id, list.clone(), false).await?; | 90 | }; |
78 | } | 91 | mod_add(config.clone(), mod_ids, list.clone(), false).await?; |
79 | } | 92 | } |
80 | Ok(()) | 93 | Ok(()) |
81 | } | 94 | } |
diff --git a/src/commands/modification.rs b/src/commands/modification.rs index f66ce28..7d4be8d 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs | |||
@@ -1,9 +1,8 @@ | |||
1 | use std::io::{Error, ErrorKind}; | 1 | use std::io::{Error, ErrorKind}; |
2 | 2 | ||
3 | use crate::{modrinth::{project, versions, extract_current_version, Version}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, mods_get_all_ids, userlist_get_all_ids, userlist_get_current_version, lists_get_all_ids, mods_remove}, input::{Input, Subcmd}, get_current_list, files::{delete_version, download_versions}, List}; | 3 | use crate::{modrinth::{project, versions, extract_current_version, Version, projects}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, mods_get_all_ids, userlist_get_all_ids, userlist_get_current_version, lists_get_all_ids, mods_remove}, input::{Input, Subcmd}, get_current_list, files::{delete_version, download_versions}, List}; |
4 | 4 | ||
5 | pub async fn modification(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { | 5 | pub async fn modification(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { |
6 | |||
7 | match input.subcommand.as_ref().ok_or("")? { | 6 | match input.subcommand.as_ref().ok_or("")? { |
8 | Subcmd::Add => { | 7 | Subcmd::Add => { |
9 | add(config, input).await | 8 | add(config, input).await |
@@ -23,69 +22,75 @@ async fn add(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error> | |||
23 | 22 | ||
24 | let current_list = get_current_list(config.clone())?; | 23 | let current_list = get_current_list(config.clone())?; |
25 | 24 | ||
26 | mod_add(config, &args[0], current_list, input.disable_download).await?; | 25 | mod_add(config, vec![String::from(&args[0])], current_list, input.disable_download).await?; |
27 | 26 | ||
28 | Ok(()) | 27 | Ok(()) |
29 | } | 28 | } |
30 | 29 | ||
31 | pub async fn mod_add(config: Cfg, mod_id: &str, list: List, disable_download: bool) -> Result<(), Box<dyn std::error::Error>> { | 30 | pub async fn mod_add(config: Cfg, mod_id: Vec<String>, list: List, disable_download: bool) -> Result<(), Box<dyn std::error::Error>> { |
32 | 31 | ||
33 | println!("Adding mod {}", mod_id); | 32 | println!("Adding mod(s) {:?}", mod_id); |
34 | 33 | let projects = if mod_id.len() == 1 { | |
35 | let project = project(String::from(&config.apis.modrinth), &mod_id).await; | 34 | vec![project(String::from(&config.apis.modrinth), &mod_id[0]).await] |
36 | |||
37 | let available_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await; | ||
38 | |||
39 | let mut available_versions_vec: Vec<String> = Vec::new(); | ||
40 | let current_version: Option<Version>; | ||
41 | let current_version_id: String; | ||
42 | let file: String; | ||
43 | if !available_versions.is_empty() { | ||
44 | let current_id = extract_current_version(available_versions.clone())?; | ||
45 | |||
46 | current_version = Some(available_versions.clone().into_iter().find(|v| v.id == current_id).unwrap()); | ||
47 | |||
48 | current_version_id = current_version.clone().unwrap().id; | ||
49 | |||
50 | file = current_version.clone().ok_or("VERSION_CORRUPTED")?.files.into_iter().find(|f| f.primary).unwrap().url; | ||
51 | |||
52 | for ver in available_versions { | ||
53 | available_versions_vec.push(ver.id); | ||
54 | }; | ||
55 | } else { | 35 | } else { |
56 | println!("There's currently no mod version for your specified target"); | 36 | projects(String::from(&config.apis.modrinth), mod_id).await |
57 | current_version = None; | ||
58 | current_version_id = String::from("NONE"); | ||
59 | file = String::from("NONE"); | ||
60 | available_versions_vec.push(String::from("NONE")); | ||
61 | } | ||
62 | |||
63 | //add to current list and mod table | ||
64 | match userlist_get_all_ids(config.clone(), list.clone().id) { | ||
65 | Ok(mods) => { | ||
66 | if mods.contains(&project.id) { | ||
67 | return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_ON_LIST"))); } | ||
68 | else { | ||
69 | userlist_insert(config.clone(), String::from(&list.id), String::from(&project.id), String::from(¤t_version_id), available_versions_vec, file)?; | ||
70 | } | ||
71 | }, | ||
72 | Err(..) => userlist_insert(config.clone(), String::from(&list.id), String::from(&project.id), String::from(¤t_version_id), available_versions_vec, file)?, | ||
73 | }; | 37 | }; |
74 | 38 | ||
75 | match mods_get_all_ids(config.clone()) { | 39 | for project in projects { |
76 | Ok(mods) => { | 40 | let available_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await; |
77 | if mods.contains(&project.id) { | 41 | |
78 | //return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_IN_DATABASE"))) | 42 | let mut available_versions_vec: Vec<String> = Vec::new(); |
79 | } else { | 43 | let current_version: Option<Version>; |
44 | let current_version_id: String; | ||
45 | let file: String; | ||
46 | if !available_versions.is_empty() { | ||
47 | let current_id = extract_current_version(available_versions.clone())?; | ||
48 | |||
49 | current_version = Some(available_versions.clone().into_iter().find(|v| v.id == current_id).unwrap()); | ||
50 | |||
51 | current_version_id = current_version.clone().unwrap().id; | ||
52 | |||
53 | file = current_version.clone().ok_or("VERSION_CORRUPTED")?.files.into_iter().find(|f| f.primary).unwrap().url; | ||
54 | |||
55 | for ver in available_versions { | ||
56 | available_versions_vec.push(ver.id); | ||
57 | }; | ||
58 | } else { | ||
59 | println!("There's currently no mod version for your specified target"); | ||
60 | current_version = None; | ||
61 | current_version_id = String::from("NONE"); | ||
62 | file = String::from("NONE"); | ||
63 | available_versions_vec.push(String::from("NONE")); | ||
64 | } | ||
65 | |||
66 | //add to current list and mod table | ||
67 | match userlist_get_all_ids(config.clone(), list.clone().id) { | ||
68 | Ok(mods) => { | ||
69 | if mods.contains(&project.id) { | ||
70 | return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_ON_LIST"))); } | ||
71 | else { | ||
72 | userlist_insert(config.clone(), String::from(&list.id), String::from(&project.id), String::from(¤t_version_id), available_versions_vec, file)?; | ||
73 | } | ||
74 | }, | ||
75 | Err(..) => userlist_insert(config.clone(), String::from(&list.id), String::from(&project.id), String::from(¤t_version_id), available_versions_vec, file)?, | ||
76 | }; | ||
77 | |||
78 | match mods_get_all_ids(config.clone()) { | ||
79 | Ok(mods) => { | ||
80 | if mods.contains(&project.id) { | ||
81 | //return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_IN_DATABASE"))) | ||
82 | } else { | ||
83 | mods_insert(config.clone(), String::from(&project.id), String::from(&project.title), project.versions)?; | ||
84 | } | ||
85 | }, | ||
86 | Err(..) => { | ||
80 | mods_insert(config.clone(), String::from(&project.id), String::from(&project.title), project.versions)?; | 87 | mods_insert(config.clone(), String::from(&project.id), String::from(&project.title), project.versions)?; |
81 | } | 88 | }, |
82 | }, | 89 | }; |
83 | Err(..) => { | ||
84 | mods_insert(config.clone(), String::from(&project.id), String::from(&project.title), project.versions)?; | ||
85 | }, | ||
86 | }; | ||
87 | 90 | ||
88 | if !disable_download && current_version.is_some() { download_versions(list, vec![current_version.unwrap()]).await?; }; | 91 | if !disable_download && current_version.is_some() { download_versions(list.clone(), vec![current_version.unwrap()]).await?; }; |
92 | |||
93 | } | ||
89 | 94 | ||
90 | Ok(()) | 95 | Ok(()) |
91 | } | 96 | } |