summaryrefslogtreecommitdiff
path: root/src/commands/io.rs
blob: 2edb95d0020aa5422e4f35103ff9c6ab8cfcfa8f (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
use std::fs::File;
use std::io::prelude::*;
use serde::{Serialize, Deserialize};

use crate::{input::{Input, Subcmd}, db::{lists_get, userlist_get_all_ids, lists_get_all_ids, lists_insert}, config::Cfg, Modloader, mod_add, List};

#[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) -> Result<Self, Box<dyn std::error::Error>> {

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

        let mut dl_folder = None;
        if download == true { 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.stringify(), mc_version: list.mc_version, download_folder: dl_folder })
    }
}

pub async fn io(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> {
    
    match input.subcommand.clone().ok_or("INVALID_INPUT")? {
        Subcmd::Export => { export(config, input)? },
        Subcmd::Import => { import(config, input.args).await? },
        _ => { },
    }

    Ok(())
}

fn export(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> {
    let mut list_ids: Vec<String> = vec![];
    if input.all_lists {
        list_ids = lists_get_all_ids(config.clone())?;
    } else {
        let args = input.args.ok_or("NO_ARGS")?;
        for arg in args {
            list_ids.push(lists_get(config.clone(), arg)?.id);
        }
    }
    let mut lists: Vec<ExportList> = vec![];
    for list_id in list_ids {
        lists.push(ExportList::from(config.clone(), String::from(list_id), true)?);
    }
    
    let toml = toml::to_string( &Export { lists } )?;

    let mut file = File::create("export.toml")?;
    file.write_all(&toml.as_bytes())?;

    Ok(())
}

async fn import(config: Cfg, args: Option<Vec<String>>) -> Result<(), Box<dyn std::error::Error>> {
    
    let filestr: String = match args {
        Some(args) => String::from(&args[0]),
        None => String::from("export.toml"),
    };

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

    println!("{:#?}", export);

    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")? };
        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));
        };
        mod_add(config.clone(), mod_ids, list.clone(), false).await?;
    }
    Ok(())
}