summaryrefslogtreecommitdiff
path: root/src/commands/list.rs
blob: 148bd163a4d16edf01465780ffb863436b6bee41 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use indicatif::{ProgressBar, ProgressStyle};

use crate::{
    config::Cfg,
    db::{
        config_change_current_list, config_get_current_list, lists_get,
        lists_get_all_ids, lists_insert, lists_remove, lists_version,
    },
    error::{EType, MLErr, MLE},
    update, Modloader, STYLE_OPERATION,
};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct List {
    pub id: String,
    pub mc_version: String,
    pub modloader: Modloader,
    pub download_folder: String,
}

impl List {
    /// # Errors
    pub fn get_current_list(config: &Cfg) -> MLE<List> {
        let id = config_get_current_list(config)?;
        lists_get(config, &id)
    }

    /// # Errors
    pub fn add(
        config: &Cfg,
        id: &str,
        mc_version: &str,
        modloader: &Modloader,
        directory: &str,
    ) -> MLE<()> {
        let p = ProgressBar::new_spinner();
        p.set_style(
            ProgressStyle::with_template(STYLE_OPERATION).map_err(|_| {
                MLErr::new(EType::LibIndicatif, "template error")
            })?,
        );
        p.set_message(format!("Create {id}"));
        lists_insert(config, id, mc_version, modloader, directory)?;
        p.finish_with_message(format!("Created {id}"));
        Ok(())
    }

    /// # Errors
    pub fn change(config: &Cfg, id: &str) -> MLE<()> {
        let p = ProgressBar::new_spinner();
        p.set_style(
            ProgressStyle::with_template(STYLE_OPERATION).map_err(|_| {
                MLErr::new(EType::LibIndicatif, "template error")
            })?,
        );
        p.set_message(format!("Change default list to {id}"));

        if !lists_get_all_ids(config)?.into_iter().any(|l| l == id) {
            return Err(MLErr::new(EType::ArgumentError, "List not found"));
        };
        config_change_current_list(config, id)?;

        p.finish_with_message(format!("Changed default list to {id}"));
        Ok(())
    }

    /// # Errors
    pub fn remove(config: &Cfg, id: &str) -> MLE<()> {
        let p = ProgressBar::new_spinner();
        p.set_style(
            ProgressStyle::with_template(STYLE_OPERATION).map_err(|_| {
                MLErr::new(EType::LibIndicatif, "template error")
            })?,
        );
        p.set_message(format!("Remove {id}"));
        lists_remove(config, id)?;
        p.finish_with_message(format!("Removed {id}"));
        Ok(())
    }

    ///Changing the current lists version and updating it
    ///
    /// #Arguments
    ///
    /// * `config` - The current config
    /// * `args` - All args, to extract the new version
    /// # Errors
    pub async fn version(
        config: &Cfg,
        id: &str,
        mc_version: String,
        download: bool,
        delete: bool,
    ) -> MLE<()> {
        let p = ProgressBar::new_spinner();
        p.set_style(
            ProgressStyle::with_template(STYLE_OPERATION).map_err(|_| {
                MLErr::new(EType::LibIndicatif, "template error")
            })?,
        );
        p.set_message(format!(
            "Change version for list {id} to minecraft version: {mc_version}"
        ));

        lists_version(config, id, &mc_version)?;

        p.finish_with_message(format!(
            "Changed version for list {id} to minecraft version: {mc_version}"
        ));

        let list = lists_get(config, id)?;
        update(config, vec![list], true, download, delete).await
    }

    /// # Errors
    pub fn list(config: &Cfg) -> MLE<()> {
        let lists = lists_get_all_ids(config)?;
        for list in lists {
            let l = lists_get(config, &list)?;
            println!("{}: | {} | {}", l.id, l.mc_version, l.modloader);
        }
        Ok(())
    }
}