summaryrefslogtreecommitdiff
path: root/src/commands/io.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/commands/io.rs')
-rw-r--r--src/commands/io.rs79
1 files changed, 74 insertions, 5 deletions
diff --git a/src/commands/io.rs b/src/commands/io.rs
index dc1f408..47991c5 100644
--- a/src/commands/io.rs
+++ b/src/commands/io.rs
@@ -1,12 +1,81 @@
1use crate::{input::{Input, Subcmd}, config::Cfg}; 1use std::fs::File;
2use std::io::prelude::*;
3use serde::{Serialize, Deserialize};
2 4
3pub fn io(_config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { 5use crate::{input::{Input, Subcmd}, db::{lists_get, userlist_get_all_ids, lists_get_all_ids, lists_insert}, config::Cfg, Modloader, mod_add, List};
6
7#[derive(Debug, Serialize, Deserialize)]
8struct Export {
9 lists: Vec<ExportList>
10}
11
12#[derive(Debug, Serialize, Deserialize)]
13struct ExportList {
14 id: String,
15 mods: String,
16 launcher: String,
17 mc_version: String,
18 download_folder: Option<String>,
19}
20
21impl ExportList {
22 pub fn from(config: Cfg, list_id: String, download: bool) -> Result<Self, Box<dyn std::error::Error>> {
23
24 let list = lists_get(config.clone(), String::from(&list_id))?;
25
26 let mut dl_folder = None;
27 if download == true { dl_folder = Some(list.download_folder) };
28
29 let mods = userlist_get_all_ids(config, list_id)?.join("|");
30
31 Ok(Self { id: list.id, mods, launcher: list.modloader.stringify(), mc_version: list.mc_version, download_folder: dl_folder })
32 }
33}
34
35pub async fn io(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> {
4 36
5 match input.subcommand.ok_or("INVALID_INPUT")? { 37 match input.subcommand.ok_or("INVALID_INPUT")? {
6 Subcmd::Export => {}, 38 Subcmd::Export => { export(config, input.args)? },
7 Subcmd::Import => {}, 39 Subcmd::Import => { import(config).await? },
8 _ => {}, 40 _ => { },
41 }
42
43 Ok(())
44}
45
46fn export(config: Cfg, _args: Option<Vec<String>>) -> Result<(), Box<dyn std::error::Error>> {
47 let list_ids = lists_get_all_ids(config.clone())?;
48 let mut lists: Vec<ExportList> = vec![];
49 for list_id in list_ids {
50 lists.push(ExportList::from(config.clone(), String::from(list_id), true)?);
9 } 51 }
52
53 let toml = toml::to_string( &Export { lists } )?;
54
55 let mut file = File::create("export.toml")?;
56 file.write_all(&toml.as_bytes())?;
10 57
11 Ok(()) 58 Ok(())
12} 59}
60
61async fn import(config: Cfg) -> Result<(), Box<dyn std::error::Error>> {
62
63 let mut file = File::open("export.toml")?;
64 let mut content = String::new();
65 file.read_to_string(&mut content)?;
66 let export: Export = toml::from_str(&content)?;
67
68 println!("{:#?}", export);
69
70 for exportlist in export.lists {
71 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")? };
72 lists_insert(config.clone(), list.id.clone(), list.mc_version.clone(), list.modloader.clone(), String::from(&list.download_folder))?;
73 //TODO currently workaround, too many requests
74 let mods: Vec<&str> = exportlist.mods.split("|").collect();
75 for mod_id in mods {
76 println!("Adding {}", mod_id);
77 mod_add(config.clone(), mod_id, list.clone(), false).await?;
78 }
79 }
80 Ok(())
81}