diff options
Diffstat (limited to 'src/commands/io.rs')
-rw-r--r-- | src/commands/io.rs | 72 |
1 files changed, 48 insertions, 24 deletions
diff --git a/src/commands/io.rs b/src/commands/io.rs index dd294bc..8e44b2b 100644 --- a/src/commands/io.rs +++ b/src/commands/io.rs | |||
@@ -1,12 +1,16 @@ | |||
1 | use indicatif::{ProgressBar, ProgressStyle}; | ||
1 | use serde::{Deserialize, Serialize}; | 2 | use serde::{Deserialize, Serialize}; |
2 | use std::fs::File; | 3 | use std::fs::File; |
3 | use std::io::prelude::*; | 4 | use std::io::prelude::*; |
4 | 5 | ||
5 | use crate::{ | 6 | use crate::{ |
6 | config::Cfg, | 7 | config::Cfg, |
7 | db::{lists_get, lists_get_all_ids, lists_insert, userlist_get_set_version, userlist_get_all_ids, userlist_get_current_version}, | 8 | db::{ |
9 | lists_get, lists_get_all_ids, lists_insert, userlist_get_all_ids, | ||
10 | userlist_get_current_version, userlist_get_set_version, | ||
11 | }, | ||
8 | error::MLE, | 12 | error::MLE, |
9 | mod_add, IDSelector, List, Modloader, AddMod, | 13 | mod_add, AddMod, IDSelector, List, Modloader, STYLE_OPERATION, |
10 | }; | 14 | }; |
11 | 15 | ||
12 | #[derive(Debug, Serialize, Deserialize)] | 16 | #[derive(Debug, Serialize, Deserialize)] |
@@ -17,14 +21,14 @@ struct Export { | |||
17 | #[derive(Debug, Serialize, Deserialize)] | 21 | #[derive(Debug, Serialize, Deserialize)] |
18 | struct ExportVersion { | 22 | struct ExportVersion { |
19 | version: String, | 23 | version: String, |
20 | set: bool | 24 | set: bool, |
21 | } | 25 | } |
22 | 26 | ||
23 | impl ExportVersion { | 27 | impl ExportVersion { |
24 | fn from(config: Cfg, list_id: &str, mod_id: &str) -> MLE<Self> { | 28 | fn from(config: &Cfg, list_id: &str, mod_id: &str) -> MLE<Self> { |
25 | Ok(Self { | 29 | Ok(Self { |
26 | version: userlist_get_current_version(config.clone(), list_id, mod_id)?, | 30 | version: userlist_get_current_version(config, list_id, mod_id)?, |
27 | set: userlist_get_set_version(config.clone(), list_id, mod_id)? | 31 | set: userlist_get_set_version(config, list_id, mod_id)?, |
28 | }) | 32 | }) |
29 | } | 33 | } |
30 | } | 34 | } |
@@ -39,18 +43,18 @@ struct ExportList { | |||
39 | } | 43 | } |
40 | 44 | ||
41 | impl ExportList { | 45 | impl ExportList { |
42 | pub fn from(config: Cfg, list_id: String, download: bool) -> MLE<Self> { | 46 | pub fn from(config: &Cfg, list_id: &str, download: bool) -> MLE<Self> { |
43 | let list = lists_get(config.clone(), String::from(&list_id))?; | 47 | let list = lists_get(config, list_id)?; |
44 | 48 | ||
45 | let mut dl_folder = None; | 49 | let mut dl_folder = None; |
46 | if download { | 50 | if download { |
47 | dl_folder = Some(list.download_folder) | 51 | dl_folder = Some(list.download_folder) |
48 | }; | 52 | }; |
49 | 53 | ||
50 | let mods = userlist_get_all_ids(config.clone(), &list_id)?; | 54 | let mods = userlist_get_all_ids(config, list_id)?; |
51 | let mut versions = vec![]; | 55 | let mut versions = vec![]; |
52 | for m in mods { | 56 | for m in mods { |
53 | versions.push(ExportVersion::from(config.clone(), &list_id, &m)?) | 57 | versions.push(ExportVersion::from(config, list_id, &m)?) |
54 | } | 58 | } |
55 | 59 | ||
56 | Ok(Self { | 60 | Ok(Self { |
@@ -63,29 +67,46 @@ impl ExportList { | |||
63 | } | 67 | } |
64 | } | 68 | } |
65 | 69 | ||
66 | pub fn export(config: Cfg, list: Option<String>) -> MLE<()> { | 70 | pub fn export(config: &Cfg, list: Option<String>) -> MLE<()> { |
71 | let progress = ProgressBar::new_spinner(); | ||
72 | progress.set_style(ProgressStyle::with_template(STYLE_OPERATION).unwrap()); | ||
73 | |||
67 | let mut list_ids: Vec<String> = vec![]; | 74 | let mut list_ids: Vec<String> = vec![]; |
68 | if list.is_none() { | 75 | if list.is_none() { |
69 | list_ids = lists_get_all_ids(config.clone())?; | 76 | list_ids = lists_get_all_ids(config)?; |
70 | } else { | 77 | } else { |
71 | list_ids.push(lists_get(config.clone(), list.unwrap())?.id); | 78 | list_ids.push(lists_get(config, &list.unwrap())?.id); |
72 | } | 79 | } |
80 | |||
73 | let mut lists: Vec<ExportList> = vec![]; | 81 | let mut lists: Vec<ExportList> = vec![]; |
74 | for list_id in list_ids { | 82 | for list_id in list_ids { |
75 | lists.push(ExportList::from(config.clone(), list_id, true)?); | 83 | progress.set_message(format!("Export {}", list_id)); |
84 | //TODO download option/ new download on import | ||
85 | lists.push(ExportList::from(config, &list_id, true)?); | ||
76 | } | 86 | } |
77 | 87 | ||
78 | let toml = toml::to_string(&Export { lists })?; | 88 | let toml = toml::to_string(&Export { lists })?; |
79 | 89 | ||
80 | let filestr = dirs::home_dir().unwrap().join("mlexport.toml"); | 90 | let filestr = dirs::home_dir() |
91 | .unwrap() | ||
92 | .join("mlexport.toml") | ||
93 | .into_os_string() | ||
94 | .into_string() | ||
95 | .unwrap(); | ||
81 | 96 | ||
82 | let mut file = File::create(filestr.into_os_string().into_string().unwrap().as_str())?; | 97 | progress.set_message("Create file"); |
98 | let mut file = File::create(&filestr)?; | ||
83 | file.write_all(toml.as_bytes())?; | 99 | file.write_all(toml.as_bytes())?; |
100 | progress.finish_with_message(format!("Exported to {}", filestr)); | ||
84 | 101 | ||
85 | Ok(()) | 102 | Ok(()) |
86 | } | 103 | } |
87 | 104 | ||
88 | pub async fn import(config: Cfg, file_str: String, direct_download: bool) -> MLE<()> { | 105 | pub async fn import( |
106 | config: &Cfg, | ||
107 | file_str: &str, | ||
108 | direct_download: bool, | ||
109 | ) -> MLE<()> { | ||
89 | let mut file = File::open(file_str)?; | 110 | let mut file = File::open(file_str)?; |
90 | let mut content = String::new(); | 111 | let mut content = String::new(); |
91 | file.read_to_string(&mut content)?; | 112 | file.read_to_string(&mut content)?; |
@@ -99,18 +120,21 @@ pub async fn import(config: Cfg, file_str: String, direct_download: bool) -> MLE | |||
99 | download_folder: exportlist.download_folder.ok_or("NO_DL").unwrap(), | 120 | download_folder: exportlist.download_folder.ok_or("NO_DL").unwrap(), |
100 | }; | 121 | }; |
101 | lists_insert( | 122 | lists_insert( |
102 | config.clone(), | 123 | config, |
103 | list.id.clone(), | 124 | &list.id, |
104 | list.mc_version.clone(), | 125 | &list.mc_version, |
105 | list.modloader.clone(), | 126 | &list.modloader, |
106 | String::from(&list.download_folder), | 127 | &list.download_folder, |
107 | )?; | 128 | )?; |
108 | 129 | ||
109 | let mut ver_ids = vec![]; | 130 | let mut ver_ids = vec![]; |
110 | for id in exportlist.versions { | 131 | for id in exportlist.versions { |
111 | ver_ids.push(AddMod { id: IDSelector::VersionID(id.version), set_version: id.set} ); | 132 | ver_ids.push(AddMod { |
133 | id: IDSelector::VersionID(id.version), | ||
134 | set_version: id.set, | ||
135 | }); | ||
112 | } | 136 | } |
113 | mod_add(config.clone(), ver_ids, list, direct_download).await?; | 137 | mod_add(config, ver_ids, list, direct_download).await?; |
114 | } | 138 | } |
115 | Ok(()) | 139 | Ok(()) |
116 | } | 140 | } |