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.rs78
1 files changed, 44 insertions, 34 deletions
diff --git a/src/commands/io.rs b/src/commands/io.rs
index a3d056f..7f03eec 100644
--- a/src/commands/io.rs
+++ b/src/commands/io.rs
@@ -1,12 +1,18 @@
1use serde::{Deserialize, Serialize};
1use std::fs::File; 2use std::fs::File;
2use std::io::prelude::*; 3use std::io::prelude::*;
3use serde::{Serialize, Deserialize};
4 4
5use 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, IDSelector}; 5use crate::{
6 config::Cfg,
7 db::{lists_get, lists_get_all_ids, lists_insert, userlist_get_all_ids},
8 devdir,
9 error::MLE,
10 mod_add, IDSelector, List, Modloader,
11};
6 12
7#[derive(Debug, Serialize, Deserialize)] 13#[derive(Debug, Serialize, Deserialize)]
8struct Export { 14struct Export {
9 lists: Vec<ExportList> 15 lists: Vec<ExportList>,
10} 16}
11 17
12#[derive(Debug, Serialize, Deserialize)] 18#[derive(Debug, Serialize, Deserialize)]
@@ -20,73 +26,77 @@ struct ExportList {
20 26
21impl ExportList { 27impl ExportList {
22 pub fn from(config: Cfg, list_id: String, download: bool) -> MLE<Self> { 28 pub fn from(config: Cfg, list_id: String, download: bool) -> MLE<Self> {
23
24 let list = lists_get(config.clone(), String::from(&list_id))?; 29 let list = lists_get(config.clone(), String::from(&list_id))?;
25 30
26 let mut dl_folder = None; 31 let mut dl_folder = None;
27 if download{ dl_folder = Some(list.download_folder) }; 32 if download {
33 dl_folder = Some(list.download_folder)
34 };
28 35
29 let mods = userlist_get_all_ids(config, list_id)?.join("|"); 36 let mods = userlist_get_all_ids(config, list_id)?.join("|");
30 37
31 Ok(Self { id: list.id, mods, launcher: list.modloader.to_string(), mc_version: list.mc_version, download_folder: dl_folder }) 38 Ok(Self {
39 id: list.id,
40 mods,
41 launcher: list.modloader.to_string(),
42 mc_version: list.mc_version,
43 download_folder: dl_folder,
44 })
32 } 45 }
33} 46}
34 47
35pub async fn io(config: Cfg, input: Input) -> MLE<()> { 48pub fn export(config: Cfg, list: Option<String>) -> MLE<()> {
36
37 match input.clone().io_options.unwrap() {
38 IoOptions::Export => { export(config, input)? },
39 IoOptions::Import => { import(config, input).await? },
40 }
41
42 Ok(())
43}
44
45fn export(config: Cfg, input: Input) -> MLE<()> {
46 let mut list_ids: Vec<String> = vec![]; 49 let mut list_ids: Vec<String> = vec![];
47 if input.all_lists { 50 if list.is_none() {
48 list_ids = lists_get_all_ids(config.clone())?; 51 list_ids = lists_get_all_ids(config.clone())?;
49 } else { 52 } else {
50 list_ids.push(lists_get(config.clone(), input.list.unwrap().id)?.id); 53 list_ids.push(lists_get(config.clone(), list.unwrap())?.id);
51 } 54 }
52 let mut lists: Vec<ExportList> = vec![]; 55 let mut lists: Vec<ExportList> = vec![];
53 for list_id in list_ids { 56 for list_id in list_ids {
54 lists.push(ExportList::from(config.clone(), list_id, true)?); 57 lists.push(ExportList::from(config.clone(), list_id, true)?);
55 } 58 }
56 59
57 let toml = toml::to_string( &Export { lists } )?; 60 let toml = toml::to_string(&Export { lists })?;
58 61
59 let filestr = dirs::home_dir().unwrap().join("mlexport.toml"); 62 let filestr = dirs::home_dir().unwrap().join("mlexport.toml");
60 63
61 let mut file = File::create(devdir(filestr.into_os_string().into_string().unwrap().as_str()))?; 64 let mut file = File::create(devdir(
65 filestr.into_os_string().into_string().unwrap().as_str(),
66 ))?;
62 file.write_all(toml.as_bytes())?; 67 file.write_all(toml.as_bytes())?;
63 68
64 Ok(()) 69 Ok(())
65} 70}
66 71
67async fn import(config: Cfg, input: Input) -> MLE<()> { 72pub async fn import(config: Cfg, file_str: String, direct_download: bool) -> MLE<()> {
68 73 let mut file = File::open(file_str)?;
69 let filestr: String = match input.file {
70 Some(args) => args,
71 None => devdir(dirs::home_dir().unwrap().join("mlexport.toml").into_os_string().into_string().unwrap().as_str()),
72 };
73
74 let mut file = File::open(filestr)?;
75 let mut content = String::new(); 74 let mut content = String::new();
76 file.read_to_string(&mut content)?; 75 file.read_to_string(&mut content)?;
77 let export: Export = toml::from_str(&content)?; 76 let export: Export = toml::from_str(&content)?;
78 77
79 for exportlist in export.lists { 78 for exportlist in export.lists {
80 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() }; 79 let list = List {
81 lists_insert(config.clone(), list.id.clone(), list.mc_version.clone(), list.modloader.clone(), String::from(&list.download_folder))?; 80 id: exportlist.id,
81 mc_version: exportlist.mc_version,
82 modloader: Modloader::from(&exportlist.launcher)?,
83 download_folder: exportlist.download_folder.ok_or("NO_DL").unwrap(),
84 };
85 lists_insert(
86 config.clone(),
87 list.id.clone(),
88 list.mc_version.clone(),
89 list.modloader.clone(),
90 String::from(&list.download_folder),
91 )?;
82 let mods: Vec<&str> = exportlist.mods.split('|').collect(); 92 let mods: Vec<&str> = exportlist.mods.split('|').collect();
83 let mut mod_ids = vec![]; 93 let mut mod_ids = vec![];
84 for mod_id in mods { 94 for mod_id in mods {
85 mod_ids.push(IDSelector::ModificationID(String::from(mod_id))); 95 mod_ids.push(IDSelector::ModificationID(String::from(mod_id)));
86 }; 96 }
87 //TODO impl set_version and good direct download 97 //TODO impl set_version and good direct download
88 //TODO impl all at once, dafuck 98 //TODO impl all at once, dafuck
89 mods_add(config.clone(), mod_ids, list, input.direct_download, false).await?; 99 mod_add(config.clone(), mod_ids, list, direct_download, false).await?;
90 } 100 }
91 Ok(()) 101 Ok(())
92} 102}