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