summaryrefslogtreecommitdiff
path: root/src/commands
diff options
context:
space:
mode:
authorfxqnlr <[email protected]>2022-11-07 22:29:35 +0100
committerfxqnlr <[email protected]>2022-11-07 22:29:35 +0100
commit889dc4f87b05d838b25428478a8c42dac454a5cf (patch)
treec4b09ae53a74a7f0afc1e095a4b66a1b00e67244 /src/commands
parentea50af892c4268ae06f6df40ee435eadd076228d (diff)
downloadmodlist-889dc4f87b05d838b25428478a8c42dac454a5cf.tar
modlist-889dc4f87b05d838b25428478a8c42dac454a5cf.tar.gz
modlist-889dc4f87b05d838b25428478a8c42dac454a5cf.zip
finished rusqlite; added all db tests
Diffstat (limited to 'src/commands')
-rw-r--r--src/commands/download.rs4
-rw-r--r--src/commands/list.rs27
-rw-r--r--src/commands/modification.rs2
-rw-r--r--src/commands/setup.rs17
-rw-r--r--src/commands/update.rs13
5 files changed, 31 insertions, 32 deletions
diff --git a/src/commands/download.rs b/src/commands/download.rs
index 05c54cb..993294b 100644
--- a/src/commands/download.rs
+++ b/src/commands/download.rs
@@ -4,12 +4,12 @@ use reqwest::Client;
4 4
5use futures_util::StreamExt; 5use futures_util::StreamExt;
6 6
7use crate::{get_current_list, config::Cfg, db::get_dl_links}; 7use crate::{get_current_list, config::Cfg, db::userlist_get_all_downloads};
8 8
9pub async fn download(config: Cfg) -> Result<(), Box<dyn std::error::Error>> { 9pub async fn download(config: Cfg) -> Result<(), Box<dyn std::error::Error>> {
10 let list = get_current_list(config.clone())?; 10 let list = get_current_list(config.clone())?;
11 11
12 let links = get_dl_links(config.clone(), list)?; 12 let links = userlist_get_all_downloads(config.clone(), list.id)?;
13 13
14 download_links(config, links).await?; 14 download_links(config, links).await?;
15 15
diff --git a/src/commands/list.rs b/src/commands/list.rs
index ffa5926..6c80e4e 100644
--- a/src/commands/list.rs
+++ b/src/commands/list.rs
@@ -1,8 +1,8 @@
1use std::io::{Error, ErrorKind}; 1use std::io::{Error, ErrorKind};
2 2
3use crate::{db::{lists_insert, remove_list, change_list, get_lists, get_current_list_id, get_list}, Modloader, config::Cfg, input::Input}; 3use crate::{db::{lists_insert, lists_remove, config_change_current_list, lists_get_all_ids, config_get_current_list, lists_get}, Modloader, config::Cfg, input::Input};
4 4
5#[derive(Clone)] 5#[derive(Debug, Clone, PartialEq, Eq)]
6pub struct List { 6pub struct List {
7 pub id: String, 7 pub id: String,
8 pub mc_version: String, 8 pub mc_version: String,
@@ -12,8 +12,8 @@ pub struct List {
12pub fn list(config: Cfg, args: Option<Vec<String>>) -> Result<(), Box<dyn std::error::Error>> { 12pub fn list(config: Cfg, args: Option<Vec<String>>) -> Result<(), Box<dyn std::error::Error>> {
13 13
14 if args.is_none() { 14 if args.is_none() {
15 let lists = get_lists(config.clone())?; 15 let lists = lists_get_all_ids(config.clone())?;
16 let current_list = get_current_list_id(config)?; 16 let current_list = config_get_current_list(config)?;
17 println!("Your lists:\n{}\n-----\nCurrently selected list: \"{}\"", lists.join(",\n"), current_list); 17 println!("Your lists:\n{}\n-----\nCurrently selected list: \"{}\"", lists.join(",\n"), current_list);
18 return Ok(()); 18 return Ok(());
19 } 19 }
@@ -37,8 +37,8 @@ pub fn list(config: Cfg, args: Option<Vec<String>>) -> Result<(), Box<dyn std::e
37} 37}
38 38
39pub fn get_current_list(config: Cfg) -> Result<List, Box<dyn std::error::Error>> { 39pub fn get_current_list(config: Cfg) -> Result<List, Box<dyn std::error::Error>> {
40 let id = get_current_list_id(config.clone())?; 40 let id = config_get_current_list(config.clone())?;
41 get_list(config, id) 41 lists_get(config, id)
42} 42}
43 43
44fn add(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> { 44fn add(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
@@ -52,10 +52,7 @@ fn add(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>>
52 "fabric" => Modloader::Fabric, 52 "fabric" => Modloader::Fabric,
53 _ => return Err(Box::new(Error::new(ErrorKind::InvalidInput, "UNKNOWN_MODLOADER"))) 53 _ => return Err(Box::new(Error::new(ErrorKind::InvalidInput, "UNKNOWN_MODLOADER")))
54 }; 54 };
55 match lists_insert(config, id, mc_version, mod_loader) { 55 lists_insert(config, id, mc_version, mod_loader)
56 Err(err) => { Err(Box::new(Error::new(ErrorKind::Other, "TBD"))) },
57 Ok(()) => Ok(()),
58 }
59 }, 56 },
60 5.. => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_MANY_ARGUMENTS"))), 57 5.. => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_MANY_ARGUMENTS"))),
61 _ => panic!("list arguments should never be zero or lower"), 58 _ => panic!("list arguments should never be zero or lower"),
@@ -63,13 +60,13 @@ fn add(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>>
63} 60}
64 61
65fn change(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> { 62fn change(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
66 let lists = get_lists(config.clone())?; 63 let lists = lists_get_all_ids(config.clone())?;
67 match args.len() { 64 match args.len() {
68 1 => { 65 1 => {
69 let list = String::from(&args[0]); 66 let list = String::from(&args[0]);
70 if !lists.contains(&list) { return Err(Box::new(Error::new(ErrorKind::NotFound, "LIST_DOESNT_EXIST"))); }; 67 if !lists.contains(&list) { return Err(Box::new(Error::new(ErrorKind::NotFound, "LIST_DOESNT_EXIST"))); };
71 match change_list(config, list) { 68 match config_change_current_list(config, list) {
72 Err(err) => { Err(Box::new(Error::new(ErrorKind::Other, "TBD"))) }, 69 Err(..) => { Err(Box::new(Error::new(ErrorKind::Other, "72"))) },
73 Ok(()) => Ok(()), 70 Ok(()) => Ok(()),
74 } 71 }
75 }, 72 },
@@ -81,8 +78,8 @@ fn change(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Erro
81fn remove(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> { 78fn remove(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
82 match args.len() { 79 match args.len() {
83 1 => { 80 1 => {
84 match remove_list(config, String::from(&args[0])) { 81 match lists_remove(config, String::from(&args[0])) {
85 Err(err) => { Err(Box::new(Error::new(ErrorKind::Other, "TBD"))) }, 82 Err(..) => { Err(Box::new(Error::new(ErrorKind::Other, "85"))) },
86 Ok(()) => Ok(()), 83 Ok(()) => Ok(()),
87 } 84 }
88 }, 85 },
diff --git a/src/commands/modification.rs b/src/commands/modification.rs
index 7836735..595b677 100644
--- a/src/commands/modification.rs
+++ b/src/commands/modification.rs
@@ -22,7 +22,7 @@ pub async fn modification(config: Cfg, args: Option<Vec<String>>) -> Result<(),
22} 22}
23 23
24async fn add(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> { 24async fn add(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
25 25 //TODO! DO NOT PANIC IF MOD IS ALREADY IN MODS DB
26 if args.is_empty() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); }; 26 if args.is_empty() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); };
27 27
28 let current_list = get_current_list(config.clone())?; 28 let current_list = get_current_list(config.clone())?;
diff --git a/src/commands/setup.rs b/src/commands/setup.rs
index 0940959..8c0fcfd 100644
--- a/src/commands/setup.rs
+++ b/src/commands/setup.rs
@@ -1,6 +1,6 @@
1use std::{fs::File, path::Path, io::{Error, ErrorKind}}; 1use std::{fs::File, path::Path, io::{Error, ErrorKind}};
2 2
3use crate::{config::Cfg, db::{db_setup, user_dbversion, create_dbversion, insert_column, get_lists, get_list, get_current_versions, insert_dl_link}, modrinth::get_raw_versions}; 3use crate::{config::Cfg, db::{db_setup, s_config_get_version, s_config_create_version, s_insert_column, lists_get_all_ids, lists_get, userlist_get_all_current_version_ids, s_userlist_update_download}, modrinth::get_raw_versions};
4 4
5pub async fn setup(config: Cfg) -> Result<(), Box<dyn std::error::Error>> { 5pub async fn setup(config: Cfg) -> Result<(), Box<dyn std::error::Error>> {
6 6
@@ -10,7 +10,7 @@ pub async fn setup(config: Cfg) -> Result<(), Box<dyn std::error::Error>> {
10 return create(config, db_file); 10 return create(config, db_file);
11 } 11 }
12 12
13 match user_dbversion(config.clone()) { 13 match s_config_get_version(config.clone()) {
14 Ok(ver) => { 14 Ok(ver) => {
15 match ver.as_str() { 15 match ver.as_str() {
16 _ => return Err(Box::new(Error::new(ErrorKind::Other, "UNKNOWN_VERSION"))) 16 _ => return Err(Box::new(Error::new(ErrorKind::Other, "UNKNOWN_VERSION")))
@@ -29,26 +29,25 @@ fn create(config: Cfg, db_file: String) -> Result<(), Box<dyn std::error::Error>
29} 29}
30 30
31async fn to_02(config: Cfg) -> Result<(), Box<dyn std::error::Error>> { 31async fn to_02(config: Cfg) -> Result<(), Box<dyn std::error::Error>> {
32 let lists = get_lists(config.clone())?; 32 let lists = lists_get_all_ids(config.clone())?;
33 33
34 for list in lists { 34 for list in lists {
35 println!("Updating {}", list); 35 println!("Updating {}", list);
36 insert_column(config.clone(), String::from(&list), String::from("current_download"), String::new())?; 36 s_insert_column(config.clone(), String::from(&list), String::from("current_download"), String::new())?;
37 37
38 let full_list = get_list(config.clone(), String::from(&list))?; 38 let full_list = lists_get(config.clone(), String::from(&list))?;
39 39
40 let versions = get_current_versions(config.clone(), full_list.clone())?; 40 let versions = userlist_get_all_current_version_ids(config.clone(), full_list.clone().id)?;
41 41
42 let raw_versions = get_raw_versions(String::from(&config.apis.modrinth), versions).await; 42 let raw_versions = get_raw_versions(String::from(&config.apis.modrinth), versions).await;
43 43
44 for ver in raw_versions { 44 for ver in raw_versions {
45 println!("Adding link for {}", ver.project_id); 45 println!("Adding link for {}", ver.project_id);
46 let file = ver.files.into_iter().find(|f| f.primary).unwrap(); 46 let file = ver.files.into_iter().find(|f| f.primary).unwrap();
47 insert_dl_link(config.clone(), full_list.clone(), ver.project_id, file.url)?; 47 s_userlist_update_download(config.clone(), String::from(&full_list.id), ver.project_id, file.url)?;
48 } 48 }
49 }; 49 };
50 create_dbversion(config)?; 50 s_config_create_version(config)?;
51
52 51
53 Ok(()) 52 Ok(())
54} 53}
diff --git a/src/commands/update.rs b/src/commands/update.rs
index d278a78..e383eae 100644
--- a/src/commands/update.rs
+++ b/src/commands/update.rs
@@ -4,7 +4,7 @@ use reqwest::Client;
4 4
5use futures_util::StreamExt; 5use futures_util::StreamExt;
6 6
7use crate::{config::Cfg, modrinth::{projects, Project, versions, extract_current_version, Version}, get_current_list, db::{userlist_get_all_ids, get_versions, get_list_version, change_list_versions}, List}; 7use crate::{config::Cfg, modrinth::{projects, Project, versions, extract_current_version, Version}, get_current_list, db::{userlist_get_all_ids, mods_get_versions, userlist_get_applicable_versions, userlist_change_versions}, List};
8 8
9pub async fn update(config: Cfg) -> Result<(), Box<dyn std::error::Error>> { 9pub async fn update(config: Cfg) -> Result<(), Box<dyn std::error::Error>> {
10 10
@@ -12,7 +12,7 @@ pub async fn update(config: Cfg) -> Result<(), Box<dyn std::error::Error>> {
12 12
13 let mods = userlist_get_all_ids(config.clone(), current_list.clone().id)?; 13 let mods = userlist_get_all_ids(config.clone(), current_list.clone().id)?;
14 14
15 let mut versions = get_versions(config.clone(), mods.clone())?; 15 let mut versions = mods_get_versions(config.clone(), mods.clone())?;
16 versions.sort_by_key(|ver| ver.mod_id.clone()); 16 versions.sort_by_key(|ver| ver.mod_id.clone());
17 17
18 let mut projects = projects(String::from(&config.apis.modrinth), mods).await; 18 let mut projects = projects(String::from(&config.apis.modrinth), mods).await;
@@ -54,12 +54,15 @@ async fn specific_update(config: Cfg, list: List, project: Project) -> Result<Ve
54 } 54 }
55 55
56 let mut current: Vec<Version> = vec![]; 56 let mut current: Vec<Version> = vec![];
57 if versions.join("|") != get_list_version(config.clone(), list.clone(), String::from(&project.id))? { 57 if versions.join("|") != userlist_get_applicable_versions(config.clone(), String::from(&list.id), String::from(&project.id))? {
58 //get new versions 58 //get new versions
59 print!(" | getting new version"); 59 print!(" | getting new version");
60 let current_str = extract_current_version(applicable_versions.clone())?; 60 let current_str = extract_current_version(applicable_versions.clone())?;
61 current.push(applicable_versions.into_iter().find(|ver| ver.id == current_str).unwrap()); 61 let current_ver = applicable_versions.into_iter().find(|ver| ver.id == current_str).ok_or("")?;
62 change_list_versions(config, list, current_str, versions, project.id)?; 62 current.push(current_ver.clone());
63
64 let link = current_ver.files.into_iter().find(|f| f.primary).ok_or("")?.url;
65 userlist_change_versions(config, list.id, current_str, versions.join("|"), link, project.id)?;
63 } 66 }
64 67
65 if current.is_empty() { return Err(Box::new(Error::new(ErrorKind::NotFound, "NO_UPDATE_AVAILABLE"))) }; 68 if current.is_empty() { return Err(Box::new(Error::new(ErrorKind::NotFound, "NO_UPDATE_AVAILABLE"))) };