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

                                            
            







                                                                               
  
 








                                       
                                                                



                                                                
 
 


                                                  
                                                                
                                                          
 
                                                                 
                                        

                                            
 


                                                                   
 


                                                  
                                                                




                                                   
 














                                                     
                                                                


                                                                         
 
                                            
 


                                                                          
 


                                                            
 





                                                                   
     
          
 
use indicatif::{ProgressBar, ProgressStyle};

use crate::{
    config::Cfg,
    data::modloader::Modloader,
    db::{
        config_change_current_list, lists_get, lists_get_all_ids, lists_insert,
        lists_remove, lists_version,
    },
    errors::{Error, MLE},
    update, STYLE_OPERATION,
};

/// # 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)?);
    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)?);
    p.set_message(format!("Change default list to {id}"));

    if !lists_get_all_ids(config)?.into_iter().any(|l| l == id) {
        return Err(Error::ListNotFound);
    };
    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)?);
    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)?);
    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(())
}