summaryrefslogtreecommitdiff
path: root/src/commands
diff options
context:
space:
mode:
authorfxqnlr <[email protected]>2022-11-01 23:00:45 +0100
committerfxqnlr <[email protected]>2022-11-01 23:00:45 +0100
commitb125dfd03084fff47ab8e90d002c6699b762d998 (patch)
tree151677fb5e88105c06e1072c2fa309f55afaba36 /src/commands
parentd38f09a247b8bf1ed328c342f84b74581905317d (diff)
downloadmodlist-b125dfd03084fff47ab8e90d002c6699b762d998.tar
modlist-b125dfd03084fff47ab8e90d002c6699b762d998.tar.gz
modlist-b125dfd03084fff47ab8e90d002c6699b762d998.zip
added list stuff + beginnings of mods
Diffstat (limited to 'src/commands')
-rw-r--r--src/commands/add.rs28
-rw-r--r--src/commands/list.rs80
-rw-r--r--src/commands/mod.rs2
3 files changed, 105 insertions, 5 deletions
diff --git a/src/commands/add.rs b/src/commands/add.rs
index 67f63de..ed4a6d8 100644
--- a/src/commands/add.rs
+++ b/src/commands/add.rs
@@ -1,11 +1,28 @@
1use std::io::{Error, ErrorKind}; 1use std::io::{Error, ErrorKind};
2 2
3use crate::{modrinth::{project, versions}, config::Cfg, db::insert_mod, Modloader}; 3use crate::{modrinth::{project, versions}, config::Cfg, db::insert_mod, Modloader, input::Input};
4 4
5pub async fn add(config: Cfg, mc_mod: String) -> Result<(), Box<dyn std::error::Error>> { 5pub async fn modification(config: Cfg, args: Option<Vec<String>>) -> Result<(), Box<dyn std::error::Error>> {
6 println!("Adding");
7 6
8 let project = project(String::from(&config.apis.modrinth), &mc_mod).await; 7 if args.is_none() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))) }
8
9 let arguments = Input::from(args.unwrap().join(" "))?;
10
11 if arguments.args.is_none() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); };
12
13 match arguments.command.as_str() {
14 "add" => {
15 add(config, arguments.args.unwrap()).await
16 },
17 _ => Err(Box::new(Error::new(ErrorKind::InvalidInput, "UNKNOWN_SUBCOMMAND")))
18 }
19}
20
21pub async fn add(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
22
23 if args.len() < 1 { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); };
24
25 let project = project(String::from(&config.apis.modrinth), &args[0]).await;
9 26
10 dbg!(&project); 27 dbg!(&project);
11 28
@@ -14,7 +31,8 @@ pub async fn add(config: Cfg, mc_mod: String) -> Result<(), Box<dyn std::error::
14 if project.versions.is_empty() { panic!("This should never happen"); }; 31 if project.versions.is_empty() { panic!("This should never happen"); };
15 32
16 let current_version = get_current(config, String::from(&project.id)).await?; 33 let current_version = get_current(config, String::from(&project.id)).await?;
17 34
35 //add to current list and mod table
18 match insert_mod(project.id, project.title, current_version, project.versions, loader, String::from("1.19.2")) { 36 match insert_mod(project.id, project.title, current_version, project.versions, loader, String::from("1.19.2")) {
19 Err(err) => { Err(Box::new(err)) }, 37 Err(err) => { Err(Box::new(err)) },
20 Ok(()) => Ok(()), 38 Ok(()) => Ok(()),
diff --git a/src/commands/list.rs b/src/commands/list.rs
new file mode 100644
index 0000000..6c260ce
--- /dev/null
+++ b/src/commands/list.rs
@@ -0,0 +1,80 @@
1use std::io::{Error, ErrorKind};
2
3use crate::{db::{insert_list, remove_list, change_list, get_lists, get_current_list}, Modloader, config::Cfg, input::Input};
4
5pub fn list(config: Cfg, args: Option<Vec<String>>) -> Result<(), Box<dyn std::error::Error>> {
6
7 if args.is_none() {
8 let lists = get_lists(config.clone())?;
9 let current_list = get_current_list(config)?;
10 println!("Your lists:\n{}\n-----\nCurrently selected list: \"{}\"", lists.join(",\n"), current_list);
11 return Ok(());
12 }
13
14 let arguments = Input::from(args.unwrap().join(" "))?;
15
16 if arguments.args.is_none() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); };
17
18 match arguments.command.as_str() {
19 "add" => {
20 add(config, arguments.args.unwrap())
21 },
22 "change" => {
23 change(config, arguments.args.unwrap())
24 },
25 "remove" => {
26 remove(config, arguments.args.unwrap())
27 },
28 _ => Err(Box::new(Error::new(ErrorKind::InvalidInput, "UNKNOWN_SUBCOMMAND")))
29 }
30}
31
32fn add(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
33 match args.len() {
34 1 | 2 => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))),
35 3 => {
36 let id = String::from(&args[0]);
37 let mc_version = String::from(&args[1]);
38 let mod_loader = match args[2].as_str() {
39 "forge" => Modloader::Forge,
40 "fabric" => Modloader::Fabric,
41 _ => return Err(Box::new(Error::new(ErrorKind::InvalidInput, "UNKNOWN_MODLOADER")))
42 };
43 match insert_list(config, id, mc_version, mod_loader) {
44 Err(err) => { Err(Box::new(err)) },
45 Ok(()) => Ok(()),
46 }
47 },
48 5.. => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_MANY_ARGUMENTS"))),
49 _ => panic!("list arguments should never be zero or lower"),
50 }
51}
52
53fn change(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
54 let lists = get_lists(config.clone())?;
55 match args.len() {
56 1 => {
57 let list = String::from(&args[0]);
58 if !lists.contains(&list) { return Err(Box::new(Error::new(ErrorKind::NotFound, "LIST_DOESNT_EXIST"))); };
59 match change_list(config, list) {
60 Err(err) => { Err(Box::new(err)) },
61 Ok(()) => Ok(()),
62 }
63 },
64 2.. => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_MANY_ARGUMENTS"))),
65 _ => panic!("list arguments should never be zero or lower"),
66 }
67}
68
69fn remove(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
70 match args.len() {
71 1 => {
72 match remove_list(config, String::from(&args[0])) {
73 Err(err) => { Err(Box::new(err)) },
74 Ok(()) => Ok(()),
75 }
76 },
77 2.. => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_MANY_ARGUMENTS"))),
78 _ => panic!("list arguments should never be zero or lower"),
79 }
80}
diff --git a/src/commands/mod.rs b/src/commands/mod.rs
index 0cc183a..6432746 100644
--- a/src/commands/mod.rs
+++ b/src/commands/mod.rs
@@ -1,3 +1,5 @@
1pub mod add; 1pub mod add;
2pub mod list;
2 3
3pub use add::*; 4pub use add::*;
5pub use list::*;