summaryrefslogtreecommitdiff
path: root/src/commands/setup.rs
blob: 0940959195b580bc96c04c42ad9057ef6d09a4e4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use std::{fs::File, path::Path, io::{Error, ErrorKind}};

use crate::{config::Cfg, db::{db_setup, user_dbversion, create_dbversion, insert_column, get_lists, get_list, get_current_versions, insert_dl_link}, modrinth::get_raw_versions};

pub async fn setup(config: Cfg) -> Result<(), Box<dyn std::error::Error>> {

    let db_file = format!("{}/data.db", String::from(&config.data));
    
    if !Path::new(&db_file).exists() {
        return create(config, db_file);
    }
    
    match user_dbversion(config.clone()) {
        Ok(ver) => {
            match ver.as_str() {
                _ => return Err(Box::new(Error::new(ErrorKind::Other, "UNKNOWN_VERSION")))
            }
        },
        Err(..) => to_02(config).await?
    };
    
    Ok(())
}

fn create(config: Cfg, db_file: String) -> Result<(), Box<dyn std::error::Error>> {
    File::create(db_file)?;
    db_setup(config)?;
    Ok(())
}

async fn to_02(config: Cfg) -> Result<(), Box<dyn std::error::Error>> {
    let lists = get_lists(config.clone())?;

    for list in lists {
        println!("Updating {}", list);
        insert_column(config.clone(), String::from(&list), String::from("current_download"), String::new())?;

        let full_list = get_list(config.clone(), String::from(&list))?;

        let versions = get_current_versions(config.clone(), full_list.clone())?;
        
        let raw_versions = get_raw_versions(String::from(&config.apis.modrinth), versions).await;

        for ver in raw_versions {
            println!("Adding link for {}", ver.project_id);
            let file = ver.files.into_iter().find(|f| f.primary).unwrap();
            insert_dl_link(config.clone(), full_list.clone(), ver.project_id, file.url)?;
        }
    };
    create_dbversion(config)?;


    Ok(())
}