diff options
Diffstat (limited to 'src/commands/setup.rs')
-rw-r--r-- | src/commands/setup.rs | 54 |
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 @@ | |||
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 | } | ||