diff options
author | fxqnlr <[email protected]> | 2023-01-19 18:37:42 +0100 |
---|---|---|
committer | fxqnlr <[email protected]> | 2023-01-19 18:37:42 +0100 |
commit | f7a6d2e9c67c1fdf8fc17fa0461a201fd2720537 (patch) | |
tree | 21d0c7356b55e9b45517fd9ca874dd3270b6bc3a /src/input.rs | |
parent | be9f74f4fb82b25abd99f7024e0f5eea2691f34f (diff) | |
download | modlist-f7a6d2e9c67c1fdf8fc17fa0461a201fd2720537.tar modlist-f7a6d2e9c67c1fdf8fc17fa0461a201fd2720537.tar.gz modlist-f7a6d2e9c67c1fdf8fc17fa0461a201fd2720537.zip |
input mostly inplemented, mods missing
Diffstat (limited to 'src/input.rs')
-rw-r--r-- | src/input.rs | 115 |
1 files changed, 66 insertions, 49 deletions
diff --git a/src/input.rs b/src/input.rs index 4e59c50..be24660 100644 --- a/src/input.rs +++ b/src/input.rs | |||
@@ -7,6 +7,10 @@ pub struct Input { | |||
7 | pub mod_id: Option<String>, | 7 | pub mod_id: Option<String>, |
8 | pub mod_version: Option<String>, | 8 | pub mod_version: Option<String>, |
9 | pub set_version: bool, | 9 | pub set_version: bool, |
10 | pub all_lists: bool, | ||
11 | pub clean: bool, | ||
12 | pub direct_download: bool, | ||
13 | pub delete_old: bool, | ||
10 | pub list: Option<List>, | 14 | pub list: Option<List>, |
11 | pub list_options: Option<ListOptions>, | 15 | pub list_options: Option<ListOptions>, |
12 | pub list_id: Option<String>, | 16 | pub list_id: Option<String>, |
@@ -24,6 +28,7 @@ pub enum Cmd { | |||
24 | Update, | 28 | Update, |
25 | Download, | 29 | Download, |
26 | Io, | 30 | Io, |
31 | Version, | ||
27 | } | 32 | } |
28 | 33 | ||
29 | #[derive(Debug, Clone, PartialEq, Eq)] | 34 | #[derive(Debug, Clone, PartialEq, Eq)] |
@@ -59,6 +64,10 @@ impl Input { | |||
59 | let mut mod_id: Option<String> = None; | 64 | let mut mod_id: Option<String> = None; |
60 | let mut mod_version: Option<String> = None; | 65 | let mut mod_version: Option<String> = None; |
61 | let mut set_version = false; | 66 | let mut set_version = false; |
67 | let mut all_lists = false; | ||
68 | let mut clean = false; | ||
69 | let mut direct_download = true; | ||
70 | let mut delete_old = false; | ||
62 | let mut list: Option<List> = None; | 71 | let mut list: Option<List> = None; |
63 | let mut list_options: Option<ListOptions> = None; | 72 | let mut list_options: Option<ListOptions> = None; |
64 | let mut list_id: Option<String> = None; | 73 | let mut list_id: Option<String> = None; |
@@ -71,7 +80,9 @@ impl Input { | |||
71 | for arg in args { | 80 | for arg in args { |
72 | let arg_split: Vec<&str> = arg.trim().split(" ").collect(); | 81 | let arg_split: Vec<&str> = arg.trim().split(" ").collect(); |
73 | match arg_split[0] { | 82 | match arg_split[0] { |
74 | "v" | "version" => { show_version(); }, | 83 | "v" | "version" => { |
84 | command = Some(Cmd::Version); | ||
85 | }, | ||
75 | "d" | "download" => { | 86 | "d" | "download" => { |
76 | command = Some(Cmd::Download); | 87 | command = Some(Cmd::Download); |
77 | }, | 88 | }, |
@@ -81,25 +92,39 @@ impl Input { | |||
81 | "ma" => { | 92 | "ma" => { |
82 | command = Some(Cmd::Mod); | 93 | command = Some(Cmd::Mod); |
83 | mod_options = Some(ModOptions::Add); | 94 | mod_options = Some(ModOptions::Add); |
84 | if arg_split.len() != 1 { | 95 | if arg_split.len() == 2 { |
85 | mod_id = Some(String::from(arg_split[1])); | 96 | mod_id = Some(String::from(arg_split[1])); |
86 | } | 97 | } |
87 | }, | 98 | }, |
99 | "mv" => { | ||
100 | command = Some(Cmd::Mod); | ||
101 | mod_options = Some(ModOptions::Add); | ||
102 | if arg_split.len() == 2 { | ||
103 | mod_version = Some(String::from(arg_split[1])); | ||
104 | }; | ||
105 | }, | ||
88 | "mr" => { | 106 | "mr" => { |
89 | command = Some(Cmd::Mod); | 107 | command = Some(Cmd::Mod); |
90 | mod_options = Some(ModOptions::Remove); | 108 | mod_options = Some(ModOptions::Remove); |
91 | if arg_split.len() != 1 { | 109 | if arg_split.len() == 2 { |
92 | mod_id = Some(String::from(arg_split[1])); | 110 | mod_id = Some(String::from(arg_split[1])); |
93 | } | 111 | } |
94 | }, | 112 | }, |
95 | "mv" => { | 113 | "set_version" => { |
96 | if arg_split.len() != 1 { | ||
97 | mod_version = Some(String::from(arg_split[1])); | ||
98 | }; | ||
99 | }, | ||
100 | "set-version" => { | ||
101 | set_version = true; | 114 | set_version = true; |
102 | }, | 115 | }, |
116 | "all_lists" => { | ||
117 | all_lists = true; | ||
118 | }, | ||
119 | "clean" => { | ||
120 | clean = true; | ||
121 | }, | ||
122 | "direct-download" => { | ||
123 | direct_download = true; | ||
124 | }, | ||
125 | "delete_old" => { | ||
126 | delete_old = true; | ||
127 | }, | ||
103 | "l" => { | 128 | "l" => { |
104 | list = Some(lists_get(config.clone(), String::from(arg_split[1]))?); | 129 | list = Some(lists_get(config.clone(), String::from(arg_split[1]))?); |
105 | } | 130 | } |
@@ -111,7 +136,10 @@ impl Input { | |||
111 | "lr" => { | 136 | "lr" => { |
112 | command = Some(Cmd::List); | 137 | command = Some(Cmd::List); |
113 | list_options = Some(ListOptions::Remove); | 138 | list_options = Some(ListOptions::Remove); |
114 | list_id = Some(String::from(arg_split[1])); | 139 | if arg_split.len() == 2 { |
140 | list_id = Some(String::from(arg_split[1])); | ||
141 | list = Some(lists_get(config.clone(), list_id.clone().unwrap())?) | ||
142 | } | ||
115 | }, | 143 | }, |
116 | "lc" => { | 144 | "lc" => { |
117 | command = Some(Cmd::List); | 145 | command = Some(Cmd::List); |
@@ -138,7 +166,7 @@ impl Input { | |||
138 | "f" => { | 166 | "f" => { |
139 | file = Some(String::from(arg_split[1])); | 167 | file = Some(String::from(arg_split[1])); |
140 | }, | 168 | }, |
141 | _ => return Err(MLError::new(ErrorType::ArgumentError, "UnknownArgument")), | 169 | _ => return Err(MLError::new(ErrorType::ArgumentError, format!("Unknown Argument ({})", arg_split[0]).as_str())), |
142 | } | 170 | } |
143 | } | 171 | } |
144 | 172 | ||
@@ -148,6 +176,10 @@ impl Input { | |||
148 | mod_id, | 176 | mod_id, |
149 | mod_version, | 177 | mod_version, |
150 | set_version, | 178 | set_version, |
179 | all_lists, | ||
180 | clean, | ||
181 | direct_download, | ||
182 | delete_old, | ||
151 | list, | 183 | list, |
152 | list_options, | 184 | list_options, |
153 | list_id, | 185 | list_id, |
@@ -160,47 +192,32 @@ impl Input { | |||
160 | } | 192 | } |
161 | } | 193 | } |
162 | 194 | ||
163 | fn show_version() { | ||
164 | match std::env::var("DEV") { | ||
165 | Ok(dev) => { | ||
166 | let devint = dev.parse::<i32>().unwrap(); | ||
167 | if devint >= 1 { | ||
168 | println!("Modlist by FxQnLr v{} (DEV)", env!("CARGO_PKG_VERSION")); | ||
169 | } else { | ||
170 | println!("Modlist by FxQnLr v{}", env!("CARGO_PKG_VERSION")); | ||
171 | } | ||
172 | }, | ||
173 | Err(..) => println!("Modlist by FxQnLr v{}", env!("CARGO_PKG_VERSION")), | ||
174 | } | ||
175 | std::process::exit(0); | ||
176 | } | ||
177 | |||
178 | pub async fn get_input(config: Cfg, args: Vec<String>) -> MLE<Input> { | 195 | pub async fn get_input(config: Cfg, args: Vec<String>) -> MLE<Input> { |
179 | let input = Input::from(config.clone(), args)?; | 196 | let input = Input::from(config.clone(), args)?; |
180 | 197 | ||
198 | if input.command.is_none() { return Err(MLError::new(ErrorType::ArgumentError, "No command specified")); }; | ||
199 | |||
181 | match input.clone().command.unwrap() { | 200 | match input.clone().command.unwrap() { |
182 | Cmd::Mod => check_mod(input, config), | 201 | Cmd::Mod => check_mod(input, config), |
183 | Cmd::List => check_list(input), | 202 | Cmd::List => check_list(input), |
184 | Cmd::Update => check_update(input), | 203 | _ => Ok(input), |
185 | Cmd::Download => check_download(input), | ||
186 | Cmd::Io => check_io(input), | ||
187 | } | 204 | } |
188 | } | 205 | } |
189 | 206 | ||
190 | //Move checks to commands? translate to variables there? | 207 | //Move checks to commands? translate to variables there? |
191 | fn check_mod(mut input: Input, config: Cfg) -> MLE<Input> { | 208 | fn check_mod(mut input: Input, config: Cfg) -> MLE<Input> { |
192 | if input.mod_options.is_none() { | 209 | if input.mod_options.is_none() { |
193 | return Err(MLError::new(ErrorType::ArgumentError, "NO_MOD_ARGUMENT")); | 210 | return Err(MLError::new(ErrorType::ArgumentError, "No mod option")); |
194 | }; | 211 | }; |
195 | match input.clone().mod_options.unwrap() { | 212 | match input.clone().mod_options.unwrap() { |
196 | //Check for MV if no mod-id on both | 213 | //Check for MV if no mod-id on both |
197 | ModOptions::Add => { | 214 | ModOptions::Add => { |
198 | if input.mod_id.is_none() && input.mod_version.is_none() { return Err(MLError::new(ErrorType::ArgumentError, "MODS_NO_MODID_MODVERSION")); }; | 215 | if input.mod_id.is_none() && input.mod_version.is_none() { return Err(MLError::new(ErrorType::ArgumentError, "No mod id/slug or version id")); }; |
199 | if input.list_id.is_none() { println!("NOLIST"); input.list = Some(get_current_list(config.clone())?); }; | 216 | if input.list_id.is_none() { input.list = Some(get_current_list(config.clone())?); }; |
200 | Ok(input) | 217 | Ok(input) |
201 | }, | 218 | }, |
202 | ModOptions::Remove => { | 219 | ModOptions::Remove => { |
203 | if input.mod_id.is_none() && input.mod_version.is_none() { return Err(MLError::new(ErrorType::ArgumentError, "MODS_NO_MODID_MODVERSION")); }; | 220 | if input.mod_id.is_none() { return Err(MLError::new(ErrorType::ArgumentError, "MODS_NO_MODID")); }; |
204 | Ok(input) | 221 | Ok(input) |
205 | }, | 222 | }, |
206 | } | 223 | } |
@@ -212,9 +229,13 @@ fn check_list(mut input: Input) -> MLE<Input> { | |||
212 | }; | 229 | }; |
213 | match input.clone().list_options.unwrap() { | 230 | match input.clone().list_options.unwrap() { |
214 | ListOptions::Add => { | 231 | ListOptions::Add => { |
215 | if input.list_id.is_none() { return Err(MLError::new(ErrorType::ArgumentError, "LISTS_NO_ID")); }; | 232 | if input.list_id.is_none() { return Err(MLError::new(ErrorType::ArgumentError, "no list id specified")); }; |
216 | if input.list_mcversion.is_none() { /*TODO Get latest version */ input.list_mcversion = Some(String::from("1.19.3")) }; | 233 | if input.list_mcversion.is_none() { |
217 | if input.modloader.is_none() { return Err(MLError::new(ErrorType::ArgumentError, "LISTS_NO_MODLOADER")); }; | 234 | println!("No Minecraft Version specified, defaulting to latest release"); |
235 | //TODO Get latest version | ||
236 | input.list_mcversion = Some(String::from("1.19.3")); | ||
237 | }; | ||
238 | if input.modloader.is_none() { return Err(MLError::new(ErrorType::ArgumentError, "no modloader specified")); }; | ||
218 | if input.directory.is_none() { input.directory = Some(format!("./downloads/{}", input.clone().list_id.expect("earlier if failed"))) }; | 239 | if input.directory.is_none() { input.directory = Some(format!("./downloads/{}", input.clone().list_id.expect("earlier if failed"))) }; |
219 | Ok(input) | 240 | Ok(input) |
220 | }, | 241 | }, |
@@ -230,18 +251,6 @@ fn check_list(mut input: Input) -> MLE<Input> { | |||
230 | } | 251 | } |
231 | } | 252 | } |
232 | 253 | ||
233 | fn check_update(input: Input) -> MLE<Input> { | ||
234 | Ok(input) | ||
235 | } | ||
236 | |||
237 | fn check_download(input: Input) -> MLE<Input> { | ||
238 | Ok(input) | ||
239 | } | ||
240 | |||
241 | fn check_io(input: Input) -> MLE<Input> { | ||
242 | Ok(input) | ||
243 | } | ||
244 | |||
245 | #[test] | 254 | #[test] |
246 | fn input_from() { | 255 | fn input_from() { |
247 | let config = Cfg::init("modlist.toml").unwrap(); | 256 | let config = Cfg::init("modlist.toml").unwrap(); |
@@ -253,6 +262,10 @@ fn input_from() { | |||
253 | mod_id: None, | 262 | mod_id: None, |
254 | mod_version: None, | 263 | mod_version: None, |
255 | set_version: false, | 264 | set_version: false, |
265 | all_lists: false, | ||
266 | clean: false, | ||
267 | direct_download: false, | ||
268 | delete_old: false, | ||
256 | list: None, | 269 | list: None, |
257 | list_options: Some(ListOptions::Add), | 270 | list_options: Some(ListOptions::Add), |
258 | list_id: Some(String::from("test")), | 271 | list_id: Some(String::from("test")), |
@@ -277,6 +290,10 @@ async fn get_input_test() { | |||
277 | mod_id: Some(String::from("test")), | 290 | mod_id: Some(String::from("test")), |
278 | mod_version: None, | 291 | mod_version: None, |
279 | set_version: false, | 292 | set_version: false, |
293 | all_lists: false, | ||
294 | clean: false, | ||
295 | direct_download: false, | ||
296 | delete_old: false, | ||
280 | list: Some(lists_get(config.clone(), String::from("one")).unwrap()), | 297 | list: Some(lists_get(config.clone(), String::from("one")).unwrap()), |
281 | list_options: None, | 298 | list_options: None, |
282 | list_id: None, | 299 | list_id: None, |