summaryrefslogtreecommitdiff
path: root/src/config.rs
diff options
context:
space:
mode:
authorfx <[email protected]>2023-05-29 18:02:08 +0200
committerfx <[email protected]>2023-05-29 18:02:08 +0200
commitd3870a2efa74e68c643dfb4aef32edc2536503b0 (patch)
tree116075aaa57c35afca2749719d450c3cb473ab3e /src/config.rs
parent5a2ea0755b29a8811aeeec1c73679c5783082628 (diff)
parentc7ecf3019a75dc0ab1a0aefeb9b880899fc8a231 (diff)
downloadmodlist-d3870a2efa74e68c643dfb4aef32edc2536503b0.tar
modlist-d3870a2efa74e68c643dfb4aef32edc2536503b0.tar.gz
modlist-d3870a2efa74e68c643dfb4aef32edc2536503b0.zip
Merge pull request 'multithreaded' (#6) from multithreaded into master
Reviewed-on: http://raspberrypi.fritz.box:7920/fx/modlist/pulls/6
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs51
1 files changed, 28 insertions, 23 deletions
diff --git a/src/config.rs b/src/config.rs
index e1049d1..f0eb8f7 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -4,9 +4,12 @@ use std::{
4 path::Path, 4 path::Path,
5}; 5};
6 6
7use indicatif::{ProgressBar, ProgressStyle};
7use serde::{Deserialize, Serialize}; 8use serde::{Deserialize, Serialize};
8 9
9use crate::{db::db_setup, error::MLE, Modloader, VersionLevel, check_game_versions}; 10use crate::{
11 check_game_versions, db::db_setup, error::MLE, Modloader, VersionLevel,
12};
10 13
11#[derive(Debug, Clone, Serialize, Deserialize)] 14#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct Cfg { 15pub struct Cfg {
@@ -31,7 +34,7 @@ pub struct Defaults {
31impl Cfg { 34impl Cfg {
32 pub async fn init(path: Option<String>) -> MLE<Self> { 35 pub async fn init(path: Option<String>) -> MLE<Self> {
33 let configfile = match path.clone() { 36 let configfile = match path.clone() {
34 Some(p) => String::from(p), 37 Some(p) => p,
35 None => dirs::config_dir() 38 None => dirs::config_dir()
36 .unwrap() 39 .unwrap()
37 .join("modlist") 40 .join("modlist")
@@ -43,7 +46,8 @@ impl Cfg {
43 let mut file = match File::open(&configfile) { 46 let mut file = match File::open(&configfile) {
44 Ok(file) => file, 47 Ok(file) => file,
45 Err(err) => { 48 Err(err) => {
46 if err.kind() == std::io::ErrorKind::NotFound && path.is_none() { 49 if err.kind() == std::io::ErrorKind::NotFound && path.is_none()
50 {
47 create_config(&configfile)?; 51 create_config(&configfile)?;
48 File::open(&configfile)? 52 File::open(&configfile)?
49 } else { 53 } else {
@@ -71,16 +75,18 @@ impl Cfg {
71 Err(..) => { 75 Err(..) => {
72 create_versions_dummy(&versionfile).await?; 76 create_versions_dummy(&versionfile).await?;
73 check_game_versions(&versionfile, true).await?; 77 check_game_versions(&versionfile, true).await?;
74 }, 78 }
75 } 79 }
80
76 Ok(config) 81 Ok(config)
77 } 82 }
78} 83}
79 84
80fn create_config(path: &str) -> MLE<()> { 85fn create_config(path: &str) -> MLE<()> {
81 print!("No config file found, create default"); 86 let p = ProgressBar::new(1);
82 //Force flush of stdout, else print! doesn't print instantly 87 p.set_style(ProgressStyle::with_template("{wide_msg}").unwrap());
83 std::io::stdout().flush()?; 88 p.set_message("Create default config");
89
84 let cache_dir = dirs::cache_dir() 90 let cache_dir = dirs::cache_dir()
85 .unwrap() 91 .unwrap()
86 .join("modlist") 92 .join("modlist")
@@ -89,10 +95,10 @@ fn create_config(path: &str) -> MLE<()> {
89 let default_cfg = Cfg { 95 let default_cfg = Cfg {
90 data: cache_dir.clone(), 96 data: cache_dir.clone(),
91 cache: format!("{}/cache", cache_dir), 97 cache: format!("{}/cache", cache_dir),
92 versions: cache_dir.clone(), 98 versions: cache_dir,
93 defaults: Defaults { 99 defaults: Defaults {
94 modloader: Modloader::Fabric, 100 modloader: Modloader::Fabric,
95 version: VersionLevel::Release 101 version: VersionLevel::Release,
96 }, 102 },
97 apis: Apis { 103 apis: Apis {
98 modrinth: String::from("https://api.modrinth.com/v2/"), 104 modrinth: String::from("https://api.modrinth.com/v2/"),
@@ -101,37 +107,36 @@ fn create_config(path: &str) -> MLE<()> {
101 create_dir_all(path.split("config.toml").collect::<Vec<&str>>()[0])?; 107 create_dir_all(path.split("config.toml").collect::<Vec<&str>>()[0])?;
102 let mut file = File::create(path)?; 108 let mut file = File::create(path)?;
103 file.write_all(toml::to_string(&default_cfg)?.as_bytes())?; 109 file.write_all(toml::to_string(&default_cfg)?.as_bytes())?;
104 println!(" "); 110 p.finish_with_message(format!("Created default config ({})", path));
105 Ok(()) 111 Ok(())
106} 112}
107 113
108fn create_database(path: &str) -> MLE<()> { 114fn create_database(path: &str) -> MLE<()> {
109 print!("No database found, create base"); 115 let p = ProgressBar::new(1);
110 //Force flush of stdout, else print! doesn't print instantly 116 p.set_style(ProgressStyle::with_template("{wide_msg}").unwrap());
111 std::io::stdout().flush()?; 117 p.set_message("Create database");
112 118
113 File::create(path)?; 119 File::create(path)?;
114 db_setup(path)?; 120 db_setup(path)?;
115 println!(" "); 121 p.finish_with_message(format!("Created database ({})", path));
116 Ok(()) 122 Ok(())
117} 123}
118 124
119fn create_cache(path: &str) -> MLE<()> { 125fn create_cache(path: &str) -> MLE<()> {
120 print!("No cache direcory found, create one"); 126 let p = ProgressBar::new(1);
121 //Force flush of stdout, else print! doesn't print instantly 127 p.set_style(ProgressStyle::with_template("{wide_msg}").unwrap());
122 std::io::stdout().flush()?; 128 p.set_message("Create cache");
123 129
124 create_dir_all(path)?; 130 create_dir_all(path)?;
125 println!(" "); 131 p.finish_with_message(format!("Created cache ({})", path));
126 Ok(()) 132 Ok(())
127} 133}
128 134
129async fn create_versions_dummy(path: &str) -> MLE<()> { 135async fn create_versions_dummy(path: &str) -> MLE<()> {
130 print!("No version file found, create dummy"); 136 let p = ProgressBar::new(1);
131 //Force flush of stdout, else print! doesn't print instantly 137 p.set_style(ProgressStyle::with_template("{wide_msg}").unwrap());
132 std::io::stdout().flush()?; 138 p.set_message("Create version file");
133
134 File::create(path)?; 139 File::create(path)?;
135 println!(" "); 140 p.finish_with_message(format!("Created version file ({})", path));
136 Ok(()) 141 Ok(())
137} 142}