summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorfx <[email protected]>2023-04-23 14:13:03 +0200
committerfx <[email protected]>2023-04-23 14:13:03 +0200
commit4300ad2eb05dddfa4274e04b204f2ad28c87da05 (patch)
treea2fd059e3aefff812d0d25e23fc85203dc9d122a /src/main.rs
parent2711f05669e353fbf452156d54855e9ba454f4a8 (diff)
parent64958cc9ff0858dbf068625e35b8d5dae249d4a4 (diff)
downloadmodlist-4300ad2eb05dddfa4274e04b204f2ad28c87da05.tar
modlist-4300ad2eb05dddfa4274e04b204f2ad28c87da05.tar.gz
modlist-4300ad2eb05dddfa4274e04b204f2ad28c87da05.zip
Merge pull request 'clap' (#1) from clap into master
Reviewed-on: http://raspberrypi.fritz.box:7920/fx/modlist/pulls/1
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs312
1 files changed, 258 insertions, 54 deletions
diff --git a/src/main.rs b/src/main.rs
index 32727c7..2006856 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,65 +1,269 @@
1use std::{env, process}; 1use clap::{Parser, Subcommand};
2use modlist::{
3 config::Cfg,
4 db::{config_get_current_list, lists_get, lists_get_all_ids},
5 devdir, download, export, get_current_list, import, list_add, list_change, list_remove,
6 list_version, mod_add, mod_remove, update, IDSelector, List, Modloader,
7};
2 8
3use modlist::{config::Cfg, input::{get_input, Cmd}, update, download, list, io, modification, setup}; 9//TODO implement remote sql db
10
11//TODO make default list optional
12#[derive(Parser)]
13#[command(author, version, about)]
14struct Cli {
15 #[command(subcommand)]
16 command: Commands,
17}
18
19#[derive(Subcommand)]
20enum Commands {
21 r#Mod {
22 #[command(subcommand)]
23 command: ModCommands,
24 },
25 List {
26 #[command(subcommand)]
27 command: ListCommands,
28 },
29 Download {
30 /// download all lists
31 #[arg(short, long)]
32 all: bool,
33
34 /// clean all mods before downloading them
35 #[arg(short, long)]
36 clean: bool,
37
38 /// remove disabled versions
39 #[arg(short, long)]
40 remove: bool,
41 },
42 Update {
43 /// download all lists
44 #[arg(short, long)]
45 all: bool,
46
47 /// directly download updated mods
48 #[arg(short, long)]
49 download: bool,
50
51 /// clean all mods before downloading them
52 #[arg(short, long)]
53 clean: bool,
54
55 /// delete disabled versions
56 #[arg(short, long)]
57 remove: bool,
58 },
59 Import {
60 #[arg(short, long)]
61 file: Option<String>,
62
63 /// directly download imported mods
64 #[arg(short, long)]
65 download: bool,
66 },
67 Export {
68 /// the list you want to export
69 list: Option<String>,
70 },
71}
72
73#[derive(Subcommand)]
74enum ModCommands {
75 Add {
76 /// id of the mod/version
77 id: String,
78
79 /// set id mode to version
80 #[arg(short, long)]
81 version: bool,
82
83 /// directly download the mod
84 #[arg(short, long)]
85 download: bool,
86
87 /// lock the version added
88 #[arg(/* short , */long)]
89 lock: bool,
90
91 /// optional List selection, else default list will be used
92 #[arg(short, long)]
93 list: Option<String>,
94 },
95 Remove {
96 /// id, name or title of the mod
97 id: String,
98
99 /// optional List selection, else default list will be used
100 #[arg(short, long)]
101 list: Option<String>,
102 },
103}
104
105#[derive(Subcommand)]
106enum ListCommands {
107 Add {
108 /// list id
109 id: String,
110
111 directory: String,
112
113 modloader: Option<String>,
114
115 version: Option<String>,
116 },
117 Remove {
118 /// id, name or title of the list
119 id: String,
120 },
121 List,
122 Change {
123 /// id of the list to change to
124 id: String,
125 },
126 Version {
127 /// list id
128 id: String,
129 /// desired minecraft version
130 version: String,
131
132 /// directly download updated mods
133 #[arg(long, short)]
134 download: bool,
135
136 /// delete disabled versions
137 #[arg(short, long)]
138 remove: bool,
139 },
140}
4 141
5#[tokio::main] 142#[tokio::main]
6async fn main() { 143async fn main() {
144 let cli = Cli::parse();
145
7 let config = Cfg::init("modlist.toml").unwrap(); 146 let config = Cfg::init("modlist.toml").unwrap();
8 147 println!("{:?}", config);
9 let mut args: Vec<String> = env::args().collect(); 148
10 args.reverse(); 149 //TODO setup? maybe setup on install
11 args.pop(); 150 match cli.command {
12 args.reverse(); 151 Commands::Mod { command } => {
13 152 match command {
14 if args.is_empty() { 153 #[allow(unused_variables)]
15 println!("Please enter an argument"); 154 ModCommands::Add {
16 process::exit(1); 155 id,
17 }; 156 version,
18 157 list,
19 let input = match get_input(config.clone(), args).await { 158 download,
20 Ok(i) => i, 159 lock,
21 Err(e) => { 160 } => {
22 println!("{}", e); 161 let listf = match list {
23 process::exit(1); 162 Some(list) => lists_get(config.clone(), list).unwrap(),
163 None => lists_get(
164 config.clone(),
165 config_get_current_list(config.clone()).unwrap(),
166 )
167 .unwrap(),
168 };
169
170 let marked_id = match version {
171 true => IDSelector::VersionID(id),
172 false => IDSelector::ModificationID(id),
173 };
174
175 mod_add(config, vec![marked_id], listf, download, lock).await
176 }
177 ModCommands::Remove { id, list } => {
178 //TODO add output
179 //TODO add success even if no file found
180 let listf = match list {
181 Some(list) => lists_get(config.clone(), list).unwrap(),
182 None => lists_get(
183 config.clone(),
184 config_get_current_list(config.clone()).unwrap(),
185 )
186 .unwrap(),
187 };
188 mod_remove(config, &id, listf)
189 }
190 }
24 } 191 }
25 }; 192 Commands::List { command } => {
26 193 match command {
27 match input.clone().command.unwrap() { 194 ListCommands::Add {
28 Cmd::Mod => { 195 id,
29 modification(config, input).await 196 directory,
30 }, 197 modloader,
31 Cmd::List => { 198 version,
32 list(config, input).await 199 } => {
33 }, 200 let ml = match modloader {
34 Cmd::Update => { 201 Some(ml) => Modloader::from(&ml).unwrap(),
35 update(config, input).await 202 //TODO add default modloader to config
36 }, 203 None => Modloader::Fabric,
37 Cmd::Download => { 204 };
38 download(config, input).await
39 },
40 Cmd::Io => {
41 io(config, input).await
42 },
43 Cmd::Version => {
44 show_version();
45 Ok(())
46 },
47 Cmd::Setup => {
48 setup(config).await
49 },
50 }.unwrap()
51}
52 205
53fn show_version() { 206 let ver = match version {
54 match std::env::var("DEV") { 207 Some(ver) => ver,
55 Ok(dev) => { 208 //TODO get latest version
56 let devint = dev.parse::<i32>().unwrap(); 209 //TODO impl config for specific version or latest or latest snap
57 if devint >= 1 { 210 None => "1.19.4".to_string(),
58 println!("Modlist by FxQnLr v{} (DEV)", env!("CARGO_PKG_VERSION")); 211 };
212
213 list_add(config, id, ver, ml, directory)
214 }
215 ListCommands::Remove { id } => list_remove(config, id),
216 ListCommands::List => {
217 todo!()
218 }
219 ListCommands::Change { id } => list_change(config, id),
220 ListCommands::Version {
221 id,
222 version,
223 download,
224 remove,
225 } => list_version(config, id, version, download, remove).await,
226 }
227 }
228 //TODO a add specific list
229 Commands::Update {
230 all,
231 download,
232 clean,
233 remove,
234 } => {
235 let mut liststack: Vec<List> = vec![];
236 if all {
237 let list_ids = lists_get_all_ids(config.clone()).unwrap();
238 for id in list_ids {
239 liststack.push(lists_get(config.clone(), id).unwrap());
240 }
59 } else { 241 } else {
60 println!("Modlist by FxQnLr v{}", env!("CARGO_PKG_VERSION")); 242 let current = get_current_list(config.clone()).unwrap();
243 println!("Update list {}:", current.id);
244 liststack.push(current)
61 } 245 }
62 }, 246 update(config, liststack, clean, download, remove).await
63 Err(..) => println!("Modlist by FxQnLr v{}", env!("CARGO_PKG_VERSION")), 247 }
248 //TODO add specific list
249 Commands::Download { all, clean, remove } => download(config, all, clean, remove).await,
250 Commands::Import { file, download } => {
251 let filestr: String = match file {
252 Some(args) => args,
253 None => devdir(
254 dirs::home_dir()
255 .unwrap()
256 .join("mlexport.toml")
257 .into_os_string()
258 .into_string()
259 .unwrap()
260 .as_str(),
261 ),
262 };
263
264 import(config, filestr, download).await
265 }
266 Commands::Export { list } => export(config, list),
64 } 267 }
268 .unwrap();
65} 269}