summaryrefslogblamecommitdiff
path: root/src/commands/list.rs
blob: 52f14f2498377c9037b75bcffa97622db552b0dc (plain) (tree)
1
2
3
4
5
6
7
8
9



                                                                                                   
                                         
      
                                     

                      
 
                                      



                             
                                
 
 

                                                    
                          

 
                




                          
              
                                                              

 

                                                                 

                                                                             

                                               

 

                                                         
                            
 
 
                                                     
   
              
   

                                                   
                          
                 
             







                                                              
 
                                            
 



                                                                   
                                      
                                                            
 
 

                                           
                       
                                          



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

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

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

pub fn list_add(
    config: &Cfg,
    id: &str,
    mc_version: &str,
    modloader: &Modloader,
    directory: &str,
) -> MLE<()> {
    lists_insert(config, id, mc_version, modloader, directory)
}

pub fn list_change(config: &Cfg, id: String) -> MLE<()> {
    if !lists_get_all_ids(config)?.into_iter().any(|l| l == id) {
        return Err(MLError::new(ErrorType::ArgumentError, "List not found"));
    };
    println!("Change default list to: {}", id);
    config_change_current_list(config, id)
}

pub fn list_remove(config: &Cfg, id: String) -> MLE<()> {
    //TODO add logging
    lists_remove(config, id)
}

///Changing the current lists version and updating it
///
/// #Arguments
///
/// * `config` - The current config
/// * `args` - All args, to extract the new version
pub async fn list_version(
    config: &Cfg,
    id: &str,
    mc_version: String,
    download: bool,
    delete: bool,
) -> MLE<()> {
    println!(
        "Change version for list {} to minecraft version: {}",
        id, mc_version
    );

    lists_version(config, id, &mc_version)?;

    println!(
        "\nCheck for updates for new minecraft version in list {}",
        id
    );
    let list = lists_get(config, id)?;
    update(config, vec![list], true, download, delete).await
}

pub fn list_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(())
}