summaryrefslogtreecommitdiff
path: root/src/commands/list.rs
blob: 3998cce35685066c15ea78ca542ddc0b13503f45 (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
use std::io::{Error, ErrorKind};

use crate::{db::{lists_insert, lists_remove, config_change_current_list, lists_get_all_ids, config_get_current_list, lists_get, lists_version}, Modloader, config::Cfg, input::{Input, Subcmd}, cmd_update, error::{MLE, ErrorType, MLError}, modrinth::MCVersionType};

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

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

    match input.subcommand.ok_or("")? {
        Subcmd::Add => {
            match add(config, input.args.ok_or("")?) {
                Ok(..) => Ok(()),
                Err(e) => Err(Box::new(e))
            }
        },
        Subcmd::Change => {
            change(config, input.args)
        },
        Subcmd::Remove => {
            match remove(config, input.args.ok_or("")?) {
                Ok(..) => Ok(()),
                Err(e) => Err(Box::new(e))
            }
        },
        Subcmd::Version => {
            match version(config, Some(input.args.ok_or("NO_VERSION")?), Some(MCVersionType::Release)).await {
                Ok(..) => Ok(()),
                Err(e) => Err(Box::new(e))
            }
        }
        _ => {
            Err(Box::new(Error::new(ErrorKind::InvalidInput, "WRONG_SUBCOMMAND")))
        }
    }
}

pub fn get_current_list(config: Cfg) -> MLE<List> {
    let id = config_get_current_list(config.clone())?;
    lists_get(config, id)
}

fn add(config: Cfg, args: Vec<String>) -> MLE<()> {
    match args.len() {
        1 | 2 | 3 => Err(MLError::new(ErrorType::ArgumentCountError, "TOO_FEW_ARGUMENTS")),
        4 => {
            let id = String::from(&args[0]);
            let mc_version = String::from(&args[1]);
            let mod_loader = Modloader::from(&args[2])?;
            let download_folder = String::from(&args[3]);
            lists_insert(config, id, mc_version, mod_loader, download_folder)
        },
        5.. => Err(MLError::new(ErrorType::ArgumentCountError, "TOO_MANY_ARGUMENTS")),
        _ => panic!("list arguments should never be zero or lower"),
    }
}

fn change(config: Cfg, args: Option<Vec<String>>) -> Result<(), Box<dyn std::error::Error>> {
    let lists = lists_get_all_ids(config.clone())?;
    if args.is_none() { println!("Currently selected list: {}", get_current_list(config)?.id); return Ok(()) };
    let argsvec = args.ok_or("BAH")?;
    match argsvec.len() {
        1 => {
            let list = String::from(&argsvec[0]);
            if !lists.contains(&list) { return Err(Box::new(Error::new(ErrorKind::NotFound, "LIST_DOESNT_EXIST"))); };
            config_change_current_list(config, list)
        },
        2.. => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_MANY_ARGUMENTS"))),
        _ => panic!("list arguments should never lower than zero"),
    }
}

fn remove(config: Cfg, args: Vec<String>) -> MLE<()> {
    match args.len() {
        1 => lists_remove(config, String::from(&args[0])),
        2.. => Err(MLError::new(ErrorType::ArgumentCountError, "TOO_MANY_ARGUMENTS")),
        _ => panic!("list arguments should never be zero or lower"),
    }
}

///Changing the current lists version and updating it
/// #Arguments
/// 
/// * `config` - The current config
/// * `args` - All args, to extract the new version
async fn version(config: Cfg, args: Option<Vec<String>>, version_type: Option<MCVersionType>) -> MLE<()> {
    let current_list = lists_get(config.clone(), config_get_current_list(config.clone())?)?;

    lists_version(config.clone(), String::from(&current_list.id), String::from(&args.unwrap()[0]))?;
    //update the list & with -- args
    cmd_update(config, vec![current_list], true, true, false).await
}