summaryrefslogtreecommitdiff
path: root/src/commands/setup.rs
diff options
context:
space:
mode:
authorfxqnlr <[email protected]>2022-11-05 21:53:24 +0100
committerfxqnlr <[email protected]>2022-11-05 21:53:24 +0100
commit0f5223d3d3f6aeb6bb1a0b09ad3d4ef5731774dd (patch)
tree6262c397c500834cf6a06059394ac51328de3aed /src/commands/setup.rs
parent5d50f446a1a4612c0c931bdbc61f945760392f29 (diff)
downloadmodlist-0f5223d3d3f6aeb6bb1a0b09ad3d4ef5731774dd.tar
modlist-0f5223d3d3f6aeb6bb1a0b09ad3d4ef5731774dd.tar.gz
modlist-0f5223d3d3f6aeb6bb1a0b09ad3d4ef5731774dd.zip
added setup & download; direct input
Diffstat (limited to 'src/commands/setup.rs')
-rw-r--r--src/commands/setup.rs54
1 files changed, 54 insertions, 0 deletions
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 @@
1use std::{fs::File, path::Path, io::{Error, ErrorKind}};
2
3use 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
5pub 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
25fn 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
31async 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}