diff options
Diffstat (limited to 'src/commands')
-rw-r--r-- | src/commands/download.rs | 46 | ||||
-rw-r--r-- | src/commands/mod.rs | 4 | ||||
-rw-r--r-- | src/commands/modification.rs | 11 | ||||
-rw-r--r-- | src/commands/setup.rs | 54 | ||||
-rw-r--r-- | src/commands/update.rs | 2 |
5 files changed, 112 insertions, 5 deletions
diff --git a/src/commands/download.rs b/src/commands/download.rs new file mode 100644 index 0000000..05c54cb --- /dev/null +++ b/src/commands/download.rs | |||
@@ -0,0 +1,46 @@ | |||
1 | use std::{io::Write, fs::File}; | ||
2 | |||
3 | use reqwest::Client; | ||
4 | |||
5 | use futures_util::StreamExt; | ||
6 | |||
7 | use crate::{get_current_list, config::Cfg, db::get_dl_links}; | ||
8 | |||
9 | pub async fn download(config: Cfg) -> Result<(), Box<dyn std::error::Error>> { | ||
10 | let list = get_current_list(config.clone())?; | ||
11 | |||
12 | let links = get_dl_links(config.clone(), list)?; | ||
13 | |||
14 | download_links(config, links).await?; | ||
15 | |||
16 | Ok(()) | ||
17 | } | ||
18 | |||
19 | async fn download_links(config: Cfg, links: Vec<String>) -> Result<String, Box<dyn std::error::Error>> { | ||
20 | |||
21 | let dl_path = String::from(&config.downloads); | ||
22 | |||
23 | for link in links { | ||
24 | let filename = link.split('/').last().unwrap(); | ||
25 | let dl_path_file = format!("{}/{}", config.downloads, filename); | ||
26 | println!("Downloading {}", link); | ||
27 | |||
28 | let res = Client::new() | ||
29 | .get(String::from(&link)) | ||
30 | .send() | ||
31 | .await | ||
32 | .or(Err(format!("Failed to GET from '{}'", &link)))?; | ||
33 | |||
34 | // download chunks | ||
35 | let mut file = File::create(String::from(&dl_path_file)).or(Err(format!("Failed to create file '{}'", dl_path_file)))?; | ||
36 | let mut stream = res.bytes_stream(); | ||
37 | |||
38 | while let Some(item) = stream.next().await { | ||
39 | let chunk = item.or(Err("Error while downloading file"))?; | ||
40 | file.write_all(&chunk) | ||
41 | .or(Err("Error while writing to file"))?; | ||
42 | } | ||
43 | } | ||
44 | |||
45 | Ok(dl_path) | ||
46 | } | ||
diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 5d008fd..20badcb 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs | |||
@@ -1,7 +1,11 @@ | |||
1 | pub mod modification; | 1 | pub mod modification; |
2 | pub mod list; | 2 | pub mod list; |
3 | pub mod update; | 3 | pub mod update; |
4 | pub mod setup; | ||
5 | pub mod download; | ||
4 | 6 | ||
5 | pub use modification::*; | 7 | pub use modification::*; |
6 | pub use list::*; | 8 | pub use list::*; |
7 | pub use update::*; | 9 | pub use update::*; |
10 | pub use setup::*; | ||
11 | pub use download::*; | ||
diff --git a/src/commands/modification.rs b/src/commands/modification.rs index b90c82c..e877a63 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs | |||
@@ -34,9 +34,12 @@ async fn add(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::E | |||
34 | if project.versions.is_empty() { panic!("This should never happen"); }; | 34 | if project.versions.is_empty() { panic!("This should never happen"); }; |
35 | 35 | ||
36 | let available_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), current_list.clone()).await; | 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 | 37 | ||
38 | let current_id = extract_current_version(available_versions.clone())?; | ||
39 | |||
40 | let current_version = available_versions.clone().into_iter().find(|v| v.id == current_id).unwrap(); | ||
41 | |||
42 | let file = current_version.files.into_iter().find(|f| f.primary).unwrap().url; | ||
40 | //add to current list and mod table | 43 | //add to current list and mod table |
41 | match get_mods_from_list(config.clone(), current_list.clone()) { | 44 | match get_mods_from_list(config.clone(), current_list.clone()) { |
42 | Ok(mods) => { | 45 | Ok(mods) => { |
@@ -44,10 +47,10 @@ async fn add(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::E | |||
44 | if mods.contains(&project.id) { | 47 | if mods.contains(&project.id) { |
45 | return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_ON_LIST"))); } | 48 | return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_ON_LIST"))); } |
46 | else { | 49 | else { |
47 | insert_mod_in_list(config.clone(), current_list.clone(), String::from(&project.id), current_version, available_versions)?; | 50 | insert_mod_in_list(config.clone(), current_list.clone(), String::from(&project.id), current_version.id, available_versions, file)?; |
48 | } | 51 | } |
49 | }, | 52 | }, |
50 | Err(..) => insert_mod_in_list(config.clone(), current_list, String::from(&project.id), current_version, available_versions)?, | 53 | Err(..) => insert_mod_in_list(config.clone(), current_list, String::from(&project.id), current_version.id, available_versions, file)?, |
51 | }; | 54 | }; |
52 | 55 | ||
53 | match get_mods(config.clone()) { | 56 | match get_mods(config.clone()) { |
diff --git a/src/commands/setup.rs b/src/commands/setup.rs new file mode 100644 index 0000000..0223a21 --- /dev/null +++ b/src/commands/setup.rs | |||
@@ -0,0 +1,54 @@ | |||
1 | use std::{fs::File, path::Path, io::{Error, ErrorKind}}; | ||
2 | |||
3 | use crate::{config::Cfg, db::{db_setup, get_dbversion, create_dbversion, insert_column, get_lists, get_list, get_current_versions, insert_dl_link}, modrinth::get_raw_versions}; | ||
4 | |||
5 | pub async fn setup(config: Cfg) -> Result<(), Box<dyn std::error::Error>> { | ||
6 | |||
7 | let db_file = format!("{}/data.db", String::from(&config.data)); | ||
8 | |||
9 | if !Path::new(&db_file).exists() { | ||
10 | return create(config, db_file); | ||
11 | } | ||
12 | |||
13 | match get_dbversion(config.clone()) { | ||
14 | Ok(ver) => { | ||
15 | match ver.as_str() { | ||
16 | _ => return Err(Box::new(Error::new(ErrorKind::Other, "UNKNOWN_VERSION"))) | ||
17 | } | ||
18 | }, | ||
19 | Err(..) => to_02(config).await? | ||
20 | }; | ||
21 | |||
22 | Ok(()) | ||
23 | } | ||
24 | |||
25 | fn create(config: Cfg, db_file: String) -> Result<(), Box<dyn std::error::Error>> { | ||
26 | File::create(db_file)?; | ||
27 | db_setup(config)?; | ||
28 | Ok(()) | ||
29 | } | ||
30 | |||
31 | async fn to_02(config: Cfg) -> Result<(), Box<dyn std::error::Error>> { | ||
32 | let lists = get_lists(config.clone())?; | ||
33 | |||
34 | for list in lists { | ||
35 | println!("Updating {}", list); | ||
36 | insert_column(config.clone(), String::from(&list), String::from("current_download"), sqlite::Type::String)?; | ||
37 | |||
38 | let full_list = get_list(config.clone(), String::from(&list))?; | ||
39 | |||
40 | let versions = get_current_versions(config.clone(), full_list.clone())?; | ||
41 | |||
42 | let raw_versions = get_raw_versions(String::from(&config.apis.modrinth), versions).await; | ||
43 | |||
44 | for ver in raw_versions { | ||
45 | println!("Adding link for {}", ver.project_id); | ||
46 | let file = ver.files.into_iter().find(|f| f.primary).unwrap(); | ||
47 | insert_dl_link(config.clone(), full_list.clone(), ver.project_id, file.url)?; | ||
48 | } | ||
49 | }; | ||
50 | create_dbversion(config)?; | ||
51 | |||
52 | |||
53 | Ok(()) | ||
54 | } | ||
diff --git a/src/commands/update.rs b/src/commands/update.rs index 6275bce..284d289 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs | |||
@@ -37,7 +37,7 @@ pub async fn update(config: Cfg) -> Result<(), Box<dyn std::error::Error>> { | |||
37 | }; | 37 | }; |
38 | //println!("{:?}", updatestack); | 38 | //println!("{:?}", updatestack); |
39 | 39 | ||
40 | //download_updates(config, updatestack).await?; | 40 | download_updates(config, updatestack).await?; |
41 | 41 | ||
42 | Ok(()) | 42 | Ok(()) |
43 | } | 43 | } |