diff options
author | fxqnlr <[email protected]> | 2023-04-24 19:00:04 +0200 |
---|---|---|
committer | fxqnlr <[email protected]> | 2023-04-24 19:00:04 +0200 |
commit | 96d400ca1275bf8444e5ad4dc6c8a06b01c3ea9d (patch) | |
tree | 740e6f16b78cfe9c8a1019580bef0de1a3491300 | |
parent | 99c84b5a81f395f4f094b157019e84bfc4459df6 (diff) | |
download | modlist-96d400ca1275bf8444e5ad4dc6c8a06b01c3ea9d.tar modlist-96d400ca1275bf8444e5ad4dc6c8a06b01c3ea9d.tar.gz modlist-96d400ca1275bf8444e5ad4dc6c8a06b01c3ea9d.zip |
add auto create dirs, database and default config
-rw-r--r-- | .gitignore | 4 | ||||
-rw-r--r-- | cache | bin | 435482 -> 0 bytes | |||
-rw-r--r-- | src/commands/download.rs | 2 | ||||
-rw-r--r-- | src/commands/mod.rs | 2 | ||||
-rw-r--r-- | src/commands/setup.rs | 70 | ||||
-rw-r--r-- | src/config.rs | 74 | ||||
-rw-r--r-- | src/db.rs | 6 | ||||
-rw-r--r-- | src/files.rs | 2 | ||||
-rw-r--r-- | src/main.rs | 1 |
9 files changed, 63 insertions, 98 deletions
@@ -7,7 +7,7 @@ | |||
7 | .planmodlist.autosave.xopp | 7 | .planmodlist.autosave.xopp |
8 | data.db.cp | 8 | data.db.cp |
9 | export.toml | 9 | export.toml |
10 | config.toml | 10 | modlist.toml |
11 | /dev | 11 | /dev |
12 | /.fleet | 12 | /.fleet |
13 | /.vscode \ No newline at end of file | 13 | /.vscode |
Binary files differ | |||
diff --git a/src/commands/download.rs b/src/commands/download.rs index 9434591..1a8eb8f 100644 --- a/src/commands/download.rs +++ b/src/commands/download.rs | |||
@@ -23,7 +23,7 @@ pub async fn download(config: Cfg, all_lists: bool, clean: bool, delete_old: boo | |||
23 | 23 | ||
24 | for current_list in liststack { | 24 | for current_list in liststack { |
25 | let downloaded_versions = get_downloaded_versions(current_list.clone())?; | 25 | let downloaded_versions = get_downloaded_versions(current_list.clone())?; |
26 | println!("To download: {:#?}", downloaded_versions); | 26 | // println!("To download: {:#?}", downloaded_versions); |
27 | let current_version_ids = match userlist_get_all_current_versions_with_mods( | 27 | let current_version_ids = match userlist_get_all_current_versions_with_mods( |
28 | config.clone(), | 28 | config.clone(), |
29 | String::from(¤t_list.id), | 29 | String::from(¤t_list.id), |
diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 1c7c012..0f13056 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs | |||
@@ -2,12 +2,10 @@ pub mod download; | |||
2 | pub mod io; | 2 | pub mod io; |
3 | pub mod list; | 3 | pub mod list; |
4 | pub mod modification; | 4 | pub mod modification; |
5 | pub mod setup; | ||
6 | pub mod update; | 5 | pub mod update; |
7 | 6 | ||
8 | pub use download::*; | 7 | pub use download::*; |
9 | pub use io::*; | 8 | pub use io::*; |
10 | pub use list::*; | 9 | pub use list::*; |
11 | pub use modification::*; | 10 | pub use modification::*; |
12 | pub use setup::*; | ||
13 | pub use update::*; | 11 | pub use update::*; |
diff --git a/src/commands/setup.rs b/src/commands/setup.rs deleted file mode 100644 index 34da2f8..0000000 --- a/src/commands/setup.rs +++ /dev/null | |||
@@ -1,70 +0,0 @@ | |||
1 | use std::{fs::File, path::Path}; | ||
2 | |||
3 | use crate::{config::Cfg, db::db_setup, error::MLE}; | ||
4 | |||
5 | pub async fn setup(config: Cfg) -> MLE<()> { | ||
6 | let db_file = format!("{}/data.db", config.data); | ||
7 | |||
8 | if !Path::new(&db_file).exists() { | ||
9 | create(config, db_file)?; | ||
10 | } | ||
11 | |||
12 | /* | ||
13 | match s_config_get_version(config.clone()) { | ||
14 | Ok(ver) => { | ||
15 | match ver.as_str() { | ||
16 | "0.2" => to_03(config)?, | ||
17 | "0.3" => to_04(config)?, | ||
18 | _ => return Err(MLError::new(ErrorType::Other, "UNKNOWN_VERSION")) | ||
19 | } | ||
20 | }, | ||
21 | Err(..) => to_02(config).await? | ||
22 | }; | ||
23 | */ | ||
24 | |||
25 | Ok(()) | ||
26 | } | ||
27 | |||
28 | fn create(config: Cfg, db_file: String) -> MLE<()> { | ||
29 | println!("Create database"); | ||
30 | |||
31 | File::create(db_file)?; | ||
32 | db_setup(config)?; | ||
33 | Ok(()) | ||
34 | } | ||
35 | |||
36 | //async fn to_02(config: Cfg) -> Result<(), Box<dyn std::error::Error>> { | ||
37 | // let lists = lists_get_all_ids(config.clone())?; | ||
38 | // | ||
39 | // for list in lists { | ||
40 | // println!("Updating {}", list); | ||
41 | // s_insert_column(config.clone(), String::from(&list), String::from("current_download"), String::from("TEXT"), None)?; | ||
42 | // | ||
43 | // let full_list = lists_get(config.clone(), String::from(&list))?; | ||
44 | // | ||
45 | // let versions = userlist_get_all_current_version_ids(config.clone(), full_list.clone().id)?; | ||
46 | // | ||
47 | // let raw_versions = get_raw_versions(String::from(&config.apis.modrinth), versions).await; | ||
48 | // | ||
49 | // for ver in raw_versions { | ||
50 | // println!("Adding link for {}", ver.project_id); | ||
51 | // let file = ver.files.into_iter().find(|f| f.primary).unwrap(); | ||
52 | // s_userlist_update_download(config.clone(), String::from(&full_list.id), ver.project_id, file.url)?; | ||
53 | // } | ||
54 | // }; | ||
55 | // s_config_create_version(config)?; | ||
56 | // | ||
57 | // Ok(()) | ||
58 | //} | ||
59 | // | ||
60 | //fn to_03(config: Cfg) -> Result<(), Box<dyn std::error::Error>> { | ||
61 | // s_insert_column(config.clone(), String::from("lists"), String::from("download_folder"), String::from("TEXT"), None)?; | ||
62 | // s_config_update_version(config, String::from("0.3")) | ||
63 | //} | ||
64 | // | ||
65 | //fn to_04(config: Cfg) -> Result<(), Box<dyn std::error::Error>> { | ||
66 | // for list_id in lists_get_all_ids(config.clone())? { | ||
67 | // s_insert_column(config.clone(), list_id, String::from("disabled_versions"), String::from("TEXT"), Some(String::from("NONE")))?; | ||
68 | // } | ||
69 | // s_config_update_version(config, String::from("0.4")) | ||
70 | //} | ||
diff --git a/src/config.rs b/src/config.rs index 23c7796..817d22b 100644 --- a/src/config.rs +++ b/src/config.rs | |||
@@ -1,11 +1,11 @@ | |||
1 | use std::{ | 1 | use std::{ |
2 | fs::File, | 2 | fs::{File, create_dir_all}, |
3 | io::{Read, Write}, | 3 | io::{Read, Write}, path::Path, |
4 | }; | 4 | }; |
5 | 5 | ||
6 | use serde::{Deserialize, Serialize}; | 6 | use serde::{Deserialize, Serialize}; |
7 | 7 | ||
8 | use crate::error::MLE; | 8 | use crate::{error::MLE, db::db_setup}; |
9 | 9 | ||
10 | #[derive(Debug, Clone, Serialize, Deserialize)] | 10 | #[derive(Debug, Clone, Serialize, Deserialize)] |
11 | pub struct Cfg { | 11 | pub struct Cfg { |
@@ -21,26 +21,16 @@ pub struct Apis { | |||
21 | 21 | ||
22 | impl Cfg { | 22 | impl Cfg { |
23 | pub fn init(path: Option<String>) -> MLE<Self> { | 23 | pub fn init(path: Option<String>) -> MLE<Self> { |
24 | let configfile = match path { | 24 | let configfile = match path.clone() { |
25 | Some(p) => String::from(p), | 25 | Some(p) => String::from(p), |
26 | None => dirs::config_dir().unwrap().join("modlist.toml").to_string_lossy().into(), | 26 | None => dirs::config_dir().unwrap().join("modlist.toml").to_string_lossy().to_string(), |
27 | }; | 27 | }; |
28 | 28 | ||
29 | let mut file = match File::open(&configfile) { | 29 | let mut file = match File::open(&configfile) { |
30 | Ok(file) => file, | 30 | Ok(file) => file, |
31 | Err(err) => { | 31 | Err(err) => { |
32 | if err.kind() == std::io::ErrorKind::NotFound { | 32 | if err.kind() == std::io::ErrorKind::NotFound && path.is_none() { |
33 | println!("No config file found, creating one"); | 33 | create_config(&configfile)?; |
34 | let default_cfg = Cfg { | ||
35 | data: String::from("~/.cache/modlist/"), | ||
36 | cache: String::from("~/.cache/modlist/cache"), | ||
37 | apis: Apis { | ||
38 | modrinth: String::from("https://api.modrinth.com/v2/"), | ||
39 | }, | ||
40 | }; | ||
41 | let mut file = File::create(&configfile)?; | ||
42 | println!("Created config file"); | ||
43 | file.write_all(toml::to_string(&default_cfg)?.as_bytes())?; | ||
44 | File::open(&configfile)? | 34 | File::open(&configfile)? |
45 | } else { | 35 | } else { |
46 | return Err(err.into()); | 36 | return Err(err.into()); |
@@ -50,6 +40,56 @@ impl Cfg { | |||
50 | let mut content = String::new(); | 40 | let mut content = String::new(); |
51 | file.read_to_string(&mut content)?; | 41 | file.read_to_string(&mut content)?; |
52 | let config = toml::from_str::<Cfg>(&content)?; | 42 | let config = toml::from_str::<Cfg>(&content)?; |
43 | //Check cache | ||
44 | if !Path::new(&config.cache).exists() { | ||
45 | create_cache(&config.cache)?; | ||
46 | }; | ||
47 | //Check database | ||
48 | //TODO check file | ||
49 | let datafile = format!("{}/data.db", config.data); | ||
50 | match File::open(&datafile) { | ||
51 | Ok(..) => (), | ||
52 | Err(..) => create_database(&datafile)?, | ||
53 | }; | ||
53 | Ok(config) | 54 | Ok(config) |
54 | } | 55 | } |
55 | } | 56 | } |
57 | |||
58 | fn create_config(path: &str) -> MLE<()> { | ||
59 | print!("No config file found, create default"); | ||
60 | //Force flush of stdout, else print! doesn't print instantly | ||
61 | std::io::stdout().flush()?; | ||
62 | let default_cfg = Cfg { | ||
63 | //TODO get home dir | ||
64 | data: String::from("$HOME/.cache/modlist/"), | ||
65 | cache: String::from("$HOME/.cache/modlist/cache"), | ||
66 | apis: Apis { | ||
67 | modrinth: String::from("https://api.modrinth.com/v2/"), | ||
68 | }, | ||
69 | }; | ||
70 | let mut file = File::create(path)?; | ||
71 | file.write_all(toml::to_string(&default_cfg)?.as_bytes())?; | ||
72 | println!(" ✓"); | ||
73 | Ok(()) | ||
74 | } | ||
75 | |||
76 | fn create_database(path: &str) -> MLE<()> { | ||
77 | print!("No database found, create base"); | ||
78 | //Force flush of stdout, else print! doesn't print instantly | ||
79 | std::io::stdout().flush()?; | ||
80 | |||
81 | File::create(path)?; | ||
82 | db_setup(path)?; | ||
83 | println!(" ✓"); | ||
84 | Ok(()) | ||
85 | } | ||
86 | |||
87 | fn create_cache(path: &str) -> MLE<()> { | ||
88 | print!("No cache direcory found, create one"); | ||
89 | //Force flush of stdout, else print! doesn't print instantly | ||
90 | std::io::stdout().flush()?; | ||
91 | |||
92 | create_dir_all(path)?; | ||
93 | println!(" ✓"); | ||
94 | Ok(()) | ||
95 | } | ||
@@ -701,11 +701,9 @@ pub fn s_insert_column( | |||
701 | Ok(()) | 701 | Ok(()) |
702 | } | 702 | } |
703 | 703 | ||
704 | pub fn db_setup(config: Cfg) -> MLE<()> { | 704 | pub fn db_setup(path: &str) -> MLE<()> { |
705 | println!("Initiating database"); | ||
706 | 705 | ||
707 | let data = format!("{}/data.db", config.data); | 706 | let connection = Connection::open(path)?; |
708 | let connection = Connection::open(data)?; | ||
709 | 707 | ||
710 | connection.execute_batch( | 708 | connection.execute_batch( |
711 | "CREATE TABLE 'user_config' ( 'id' TEXT, 'value' TEXT ); | 709 | "CREATE TABLE 'user_config' ( 'id' TEXT, 'value' TEXT ); |
diff --git a/src/files.rs b/src/files.rs index 0b5bc3f..a73fc18 100644 --- a/src/files.rs +++ b/src/files.rs | |||
@@ -32,7 +32,7 @@ pub async fn download_versions(list: List, config: Cfg, versions: Vec<Version>) | |||
32 | if c.is_some() { | 32 | if c.is_some() { |
33 | print!("\t└({})Get version {} from cache", project_info.title, ver.id); | 33 | print!("\t└({})Get version {} from cache", project_info.title, ver.id); |
34 | //Force flush of stdout, else print! doesn't print instantly | 34 | //Force flush of stdout, else print! doesn't print instantly |
35 | std::io::stdout().flush().unwrap(); | 35 | std::io::stdout().flush()?; |
36 | copy_cached_version(&c.unwrap(), &dl_path); | 36 | copy_cached_version(&c.unwrap(), &dl_path); |
37 | println!(" ✓"); | 37 | println!(" ✓"); |
38 | } else { | 38 | } else { |
diff --git a/src/main.rs b/src/main.rs index 957e7c9..30c4001 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -150,7 +150,6 @@ async fn main() { | |||
150 | let config = Cfg::init(cli.config).unwrap(); | 150 | let config = Cfg::init(cli.config).unwrap(); |
151 | println!("{:?}", config); | 151 | println!("{:?}", config); |
152 | 152 | ||
153 | //TODO setup? maybe setup on install | ||
154 | match cli.command { | 153 | match cli.command { |
155 | Commands::Mod { command } => { | 154 | Commands::Mod { command } => { |
156 | match command { | 155 | match command { |