summaryrefslogtreecommitdiff
path: root/src/commands/download.rs
blob: 7748d153960c958d0407aa959efa51335dac035b (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use crate::{files::{get_downloaded_versions, download_versions, delete_version, disable_version, clean_list_dir}, db::{userlist_get_all_current_versions_with_mods, lists_get_all_ids, lists_get}, modrinth::get_raw_versions, error::{MLE, ErrorType, MLError}};
use crate::{List, get_current_list, config::Cfg, input::Input};

pub async fn download(config: Cfg, input: Input) -> MLE<()> {

    let mut liststack: Vec<List> = vec![];
    if input.all_lists {
        let list_ids = lists_get_all_ids(config.clone())?;
        for id in list_ids {
            liststack.push(lists_get(config.clone(), id)?);
        }
    } else {
        let current = get_current_list(config.clone())?;
        println!("Downloading current versions of mods in {}", current.id);
        liststack.push(current)
    }

    for current_list in liststack {
        let downloaded_versions = get_downloaded_versions(current_list.clone())?;
        println!("To download: {:#?}", downloaded_versions);
        let current_version_ids = match userlist_get_all_current_versions_with_mods(config.clone(), String::from(&current_list.id)) {
            Ok(i) => Ok(i),
            Err(e) => Err(MLError::new(ErrorType::DBError, e.to_string().as_str())),
        }?;

        let mut to_download: Vec<String> = vec![];
        //(mod_id, version_id)
        let mut to_disable: Vec<(String, String)> = vec![];

        for version in current_version_ids {
            let mod_id = version.0;
            let current_version = version.1;

            let current_download = downloaded_versions.get(&mod_id);

            if current_download.is_none() || input.clean {
                to_download.push(current_version);
            } else {
                let downloaded_version = current_download.ok_or("SOMETHING_HAS_REALLY_GONE_WRONG").unwrap();
                if &current_version != downloaded_version {
                    to_disable.push((mod_id.clone(), String::from(downloaded_version)));
                    to_download.push(current_version);
                }
            }
        }
        
        if input.clean { clean_list_dir(&current_list)? };

        if !to_download.is_empty() {
            download_versions(current_list.clone(), config.clone(), get_raw_versions(String::from(&config.apis.modrinth), to_download).await).await?;
        } else {
            println!("There are no new versions to download");
        }
        
        if !to_disable.is_empty() {
            for ver in to_disable {
                if input.delete_old {
                    println!("Deleting version {} for mod {}", ver.1, ver.0);
                    delete_version(current_list.clone(), ver.1)?;
                } else { 
                    disable_version(config.clone(), current_list.clone(), ver.1, ver.0)?;
                };
            }
        }
    }

    Ok(())
}