diff options
Diffstat (limited to 'src/commands/list.rs')
-rw-r--r-- | src/commands/list.rs | 193 |
1 files changed, 87 insertions, 106 deletions
diff --git a/src/commands/list.rs b/src/commands/list.rs index 148bd16..23a9f0f 100644 --- a/src/commands/list.rs +++ b/src/commands/list.rs | |||
@@ -1,124 +1,105 @@ | |||
1 | use indicatif::{ProgressBar, ProgressStyle}; | 1 | use indicatif::{ProgressBar, ProgressStyle}; |
2 | 2 | ||
3 | use crate::{ | 3 | use crate::{ |
4 | config::Cfg, | 4 | config::Cfg, data::modloader::Modloader, db::{ |
5 | db::{ | 5 | config_change_current_list, lists_get, |
6 | config_change_current_list, config_get_current_list, lists_get, | ||
7 | lists_get_all_ids, lists_insert, lists_remove, lists_version, | 6 | lists_get_all_ids, lists_insert, lists_remove, lists_version, |
8 | }, | 7 | }, error::{EType, MLErr, MLE}, update, STYLE_OPERATION |
9 | error::{EType, MLErr, MLE}, | ||
10 | update, Modloader, STYLE_OPERATION, | ||
11 | }; | 8 | }; |
12 | 9 | ||
13 | #[derive(Debug, Clone, PartialEq, Eq)] | 10 | /// # Errors |
14 | pub struct List { | 11 | pub fn add( |
15 | pub id: String, | 12 | config: &Cfg, |
16 | pub mc_version: String, | 13 | id: &str, |
17 | pub modloader: Modloader, | 14 | mc_version: &str, |
18 | pub download_folder: String, | 15 | modloader: &Modloader, |
16 | directory: &str, | ||
17 | ) -> MLE<()> { | ||
18 | let p = ProgressBar::new_spinner(); | ||
19 | p.set_style( | ||
20 | ProgressStyle::with_template(STYLE_OPERATION).map_err(|_| { | ||
21 | MLErr::new(EType::LibIndicatif, "template error") | ||
22 | })?, | ||
23 | ); | ||
24 | p.set_message(format!("Create {id}")); | ||
25 | lists_insert(config, id, mc_version, modloader, directory)?; | ||
26 | p.finish_with_message(format!("Created {id}")); | ||
27 | Ok(()) | ||
19 | } | 28 | } |
20 | 29 | ||
21 | impl List { | 30 | /// # Errors |
22 | /// # Errors | 31 | pub fn change(config: &Cfg, id: &str) -> MLE<()> { |
23 | pub fn get_current_list(config: &Cfg) -> MLE<List> { | 32 | let p = ProgressBar::new_spinner(); |
24 | let id = config_get_current_list(config)?; | 33 | p.set_style( |
25 | lists_get(config, &id) | 34 | ProgressStyle::with_template(STYLE_OPERATION).map_err(|_| { |
26 | } | 35 | MLErr::new(EType::LibIndicatif, "template error") |
27 | 36 | })?, | |
28 | /// # Errors | 37 | ); |
29 | pub fn add( | 38 | p.set_message(format!("Change default list to {id}")); |
30 | config: &Cfg, | ||
31 | id: &str, | ||
32 | mc_version: &str, | ||
33 | modloader: &Modloader, | ||
34 | directory: &str, | ||
35 | ) -> MLE<()> { | ||
36 | let p = ProgressBar::new_spinner(); | ||
37 | p.set_style( | ||
38 | ProgressStyle::with_template(STYLE_OPERATION).map_err(|_| { | ||
39 | MLErr::new(EType::LibIndicatif, "template error") | ||
40 | })?, | ||
41 | ); | ||
42 | p.set_message(format!("Create {id}")); | ||
43 | lists_insert(config, id, mc_version, modloader, directory)?; | ||
44 | p.finish_with_message(format!("Created {id}")); | ||
45 | Ok(()) | ||
46 | } | ||
47 | 39 | ||
48 | /// # Errors | 40 | if !lists_get_all_ids(config)?.into_iter().any(|l| l == id) { |
49 | pub fn change(config: &Cfg, id: &str) -> MLE<()> { | 41 | return Err(MLErr::new(EType::ArgumentError, "List not found")); |
50 | let p = ProgressBar::new_spinner(); | 42 | }; |
51 | p.set_style( | 43 | config_change_current_list(config, id)?; |
52 | ProgressStyle::with_template(STYLE_OPERATION).map_err(|_| { | ||
53 | MLErr::new(EType::LibIndicatif, "template error") | ||
54 | })?, | ||
55 | ); | ||
56 | p.set_message(format!("Change default list to {id}")); | ||
57 | 44 | ||
58 | if !lists_get_all_ids(config)?.into_iter().any(|l| l == id) { | 45 | p.finish_with_message(format!("Changed default list to {id}")); |
59 | return Err(MLErr::new(EType::ArgumentError, "List not found")); | 46 | Ok(()) |
60 | }; | 47 | } |
61 | config_change_current_list(config, id)?; | ||
62 | |||
63 | p.finish_with_message(format!("Changed default list to {id}")); | ||
64 | Ok(()) | ||
65 | } | ||
66 | 48 | ||
67 | /// # Errors | 49 | /// # Errors |
68 | pub fn remove(config: &Cfg, id: &str) -> MLE<()> { | 50 | pub fn remove(config: &Cfg, id: &str) -> MLE<()> { |
69 | let p = ProgressBar::new_spinner(); | 51 | let p = ProgressBar::new_spinner(); |
70 | p.set_style( | 52 | p.set_style( |
71 | ProgressStyle::with_template(STYLE_OPERATION).map_err(|_| { | 53 | ProgressStyle::with_template(STYLE_OPERATION).map_err(|_| { |
72 | MLErr::new(EType::LibIndicatif, "template error") | 54 | MLErr::new(EType::LibIndicatif, "template error") |
73 | })?, | 55 | })?, |
74 | ); | 56 | ); |
75 | p.set_message(format!("Remove {id}")); | 57 | p.set_message(format!("Remove {id}")); |
76 | lists_remove(config, id)?; | 58 | lists_remove(config, id)?; |
77 | p.finish_with_message(format!("Removed {id}")); | 59 | p.finish_with_message(format!("Removed {id}")); |
78 | Ok(()) | 60 | Ok(()) |
79 | } | 61 | } |
80 | 62 | ||
81 | ///Changing the current lists version and updating it | 63 | ///Changing the current lists version and updating it |
82 | /// | 64 | /// |
83 | /// #Arguments | 65 | /// #Arguments |
84 | /// | 66 | /// |
85 | /// * `config` - The current config | 67 | /// * `config` - The current config |
86 | /// * `args` - All args, to extract the new version | 68 | /// * `args` - All args, to extract the new version |
87 | /// # Errors | 69 | /// # Errors |
88 | pub async fn version( | 70 | pub async fn version( |
89 | config: &Cfg, | 71 | config: &Cfg, |
90 | id: &str, | 72 | id: &str, |
91 | mc_version: String, | 73 | mc_version: String, |
92 | download: bool, | 74 | download: bool, |
93 | delete: bool, | 75 | delete: bool, |
94 | ) -> MLE<()> { | 76 | ) -> MLE<()> { |
95 | let p = ProgressBar::new_spinner(); | 77 | let p = ProgressBar::new_spinner(); |
96 | p.set_style( | 78 | p.set_style( |
97 | ProgressStyle::with_template(STYLE_OPERATION).map_err(|_| { | 79 | ProgressStyle::with_template(STYLE_OPERATION).map_err(|_| { |
98 | MLErr::new(EType::LibIndicatif, "template error") | 80 | MLErr::new(EType::LibIndicatif, "template error") |
99 | })?, | 81 | })?, |
100 | ); | 82 | ); |
101 | p.set_message(format!( | 83 | p.set_message(format!( |
102 | "Change version for list {id} to minecraft version: {mc_version}" | 84 | "Change version for list {id} to minecraft version: {mc_version}" |
103 | )); | 85 | )); |
104 | 86 | ||
105 | lists_version(config, id, &mc_version)?; | 87 | lists_version(config, id, &mc_version)?; |
106 | 88 | ||
107 | p.finish_with_message(format!( | 89 | p.finish_with_message(format!( |
108 | "Changed version for list {id} to minecraft version: {mc_version}" | 90 | "Changed version for list {id} to minecraft version: {mc_version}" |
109 | )); | 91 | )); |
110 | 92 | ||
111 | let list = lists_get(config, id)?; | 93 | let list = lists_get(config, id)?; |
112 | update(config, vec![list], true, download, delete).await | 94 | update(config, vec![list], true, download, delete).await |
113 | } | 95 | } |
114 | 96 | ||
115 | /// # Errors | 97 | /// # Errors |
116 | pub fn list(config: &Cfg) -> MLE<()> { | 98 | pub fn list(config: &Cfg) -> MLE<()> { |
117 | let lists = lists_get_all_ids(config)?; | 99 | let lists = lists_get_all_ids(config)?; |
118 | for list in lists { | 100 | for list in lists { |
119 | let l = lists_get(config, &list)?; | 101 | let l = lists_get(config, &list)?; |
120 | println!("{}: | {} | {}", l.id, l.mc_version, l.modloader); | 102 | println!("{}: | {} | {}", l.id, l.mc_version, l.modloader); |
121 | } | ||
122 | Ok(()) | ||
123 | } | 103 | } |
104 | Ok(()) | ||
124 | } | 105 | } |