summaryrefslogblamecommitdiff
path: root/src/commands/io.rs
blob: 44604d115c782684d25fb6efeec9947d2988dc37 (plain) (tree)
1
2
3
4
5


                                    
 
                                                                                                                                                                           















                                        
                                                                            



                                                                      
                                                              


                                                                    
                                                                                                                                     


     
                                                       
    


                                                              




          
                                                 



                                                      
                                                                             
     

                                            
                                                                     
     


                                                     


                                                                                                   
                                     


          
 
                                                       
    
                                            

                                                                                                                         
      
 
                                        



                                                   
                                    
                                                                                                                                                                                                         
                                                                                                                                             
                                                                   
                                 
                            

                                               


                                                                                     


          
use std::fs::File;
use std::io::prelude::*;
use serde::{Serialize, Deserialize};

use crate::{input::{Input, IoOptions}, db::{lists_get, userlist_get_all_ids, lists_get_all_ids, lists_insert}, config::Cfg, Modloader, List, devdir, error::MLE, mods_add};

#[derive(Debug, Serialize, Deserialize)]
struct Export {
    lists: Vec<ExportList>
}

#[derive(Debug, Serialize, Deserialize)]
struct ExportList {
    id: String,
    mods: String,
    launcher: String,
    mc_version: String,
    download_folder: Option<String>,
}

impl ExportList {
    pub fn from(config: Cfg, list_id: String, download: bool) -> MLE<Self> {

        let list = lists_get(config.clone(), String::from(&list_id))?;

        let mut dl_folder = None;
        if download{ dl_folder = Some(list.download_folder) };

        let mods = userlist_get_all_ids(config, list_id)?.join("|");

        Ok(Self { id: list.id, mods, launcher: list.modloader.to_string(), mc_version: list.mc_version, download_folder: dl_folder })
    }
}

pub async fn io(config: Cfg, input: Input) -> MLE<()> {
    
    match input.clone().io_options.unwrap() {
        IoOptions::Export => { export(config, input)? },
        IoOptions::Import => { import(config, input).await? },
    }

    Ok(())
}

fn export(config: Cfg, input: Input) -> MLE<()> {
    let mut list_ids: Vec<String> = vec![];
    if input.all_lists {
        list_ids = lists_get_all_ids(config.clone())?;
    } else {
        list_ids.push(lists_get(config.clone(), input.list.unwrap().id)?.id);
    }
    let mut lists: Vec<ExportList> = vec![];
    for list_id in list_ids {
        lists.push(ExportList::from(config.clone(), list_id, true)?);
    }
    
    let toml = toml::to_string( &Export { lists } )?;

    let filestr = dirs::home_dir().unwrap().join("mlexport.toml");

    let mut file = File::create(devdir(filestr.into_os_string().into_string().unwrap().as_str()))?;
    file.write_all(toml.as_bytes())?;

    Ok(())
}

async fn import(config: Cfg, input: Input) -> MLE<()> {
    
    let filestr: String = match input.file {
        Some(args) => args,
        None => devdir(dirs::home_dir().unwrap().join("mlexport.toml").into_os_string().into_string().unwrap().as_str()),
    };

    let mut file = File::open(filestr)?;
    let mut content = String::new();
    file.read_to_string(&mut content)?;
    let export: Export = toml::from_str(&content)?;

    for exportlist in export.lists {
        let list = List { id: exportlist.id, mc_version: exportlist.mc_version, modloader: Modloader::from(&exportlist.launcher)?, download_folder: exportlist.download_folder.ok_or("NO_DL").unwrap() };
        lists_insert(config.clone(), list.id.clone(), list.mc_version.clone(), list.modloader.clone(), String::from(&list.download_folder))?;
        let mods: Vec<&str> = exportlist.mods.split('|').collect();
        let mut mod_ids = vec![];
        for mod_id in mods {
            mod_ids.push(String::from(mod_id));
        };
        //TODO impl set_version and good direct download
        //TODO impl all at once, dafuck
        mods_add(config.clone(), mod_ids, list, input.direct_download, false).await?;
    }
    Ok(())
}