summaryrefslogtreecommitdiff
path: root/src/config.rs
diff options
context:
space:
mode:
authorfxqnlr <[email protected]>2024-09-04 13:48:32 +0200
committerfxqnlr <[email protected]>2024-09-04 13:48:32 +0200
commit7a85cf311c85ab45c75098dae58b5ebf5fef60bc (patch)
tree8f65f35da12add062df7fdf6d63d47c664542261 /src/config.rs
parent638dc58e86ba3bbe31d50e72788c9681d414e0ae (diff)
downloadmodlist-7a85cf311c85ab45c75098dae58b5ebf5fef60bc.tar
modlist-7a85cf311c85ab45c75098dae58b5ebf5fef60bc.tar.gz
modlist-7a85cf311c85ab45c75098dae58b5ebf5fef60bc.zip
add config crate, slight cleanup
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs103
1 files changed, 49 insertions, 54 deletions
diff --git a/src/config.rs b/src/config.rs
index 6280932..8ecdc69 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -1,14 +1,16 @@
1use std::{ 1use std::{
2 fs::{create_dir_all, File}, 2 fs::{create_dir_all, File},
3 io::{Read, Write}, 3 io::Write,
4 path::Path, 4 path::Path,
5}; 5};
6 6
7use indicatif::{ProgressBar, ProgressStyle};
8use serde::{Deserialize, Serialize}; 7use serde::{Deserialize, Serialize};
9 8
10use crate::{ 9use crate::{
11 check_game_versions, db::setup, error::{EType, MLErr, MLE}, Modloader, VersionLevel, 10 check_game_versions,
11 db::setup,
12 error::{EType, MLErr, MLE},
13 Modloader, VersionLevel,
12}; 14};
13 15
14#[derive(Debug, Clone, Serialize, Deserialize)] 16#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -20,6 +22,28 @@ pub struct Cfg {
20 pub apis: Apis, 22 pub apis: Apis,
21} 23}
22 24
25impl Default for Cfg {
26 fn default() -> Self {
27 let cache_dir = dirs::cache_dir()
28 .unwrap()
29 .join("modlist")
30 .to_string_lossy()
31 .to_string();
32 Self {
33 data: cache_dir.clone(),
34 cache: format!("{cache_dir}/cache"),
35 versions: cache_dir,
36 defaults: Defaults {
37 modloader: Modloader::Fabric,
38 version: VersionLevel::Release,
39 },
40 apis: Apis {
41 modrinth: String::from("https://api.modrinth.com/v2/"),
42 },
43 }
44 }
45}
46
23#[derive(Debug, Clone, Serialize, Deserialize)] 47#[derive(Debug, Clone, Serialize, Deserialize)]
24pub struct Apis { 48pub struct Apis {
25 pub modrinth: String, 49 pub modrinth: String,
@@ -44,21 +68,24 @@ impl Cfg {
44 .to_string(), 68 .to_string(),
45 }; 69 };
46 70
47 let mut file = match File::open(&configfile) { 71 if let Some(err) = File::open(&configfile).err() {
48 Ok(file) => file, 72 if err.kind() == std::io::ErrorKind::NotFound && path.is_none() {
49 Err(err) => { 73 create_config(&configfile)?;
50 if err.kind() == std::io::ErrorKind::NotFound && path.is_none() 74 } else {
51 { 75 return Err(err.into());
52 create_config(&configfile)?; 76 };
53 File::open(&configfile)?
54 } else {
55 return Err(err.into());
56 }
57 }
58 }; 77 };
59 let mut content = String::new(); 78
60 file.read_to_string(&mut content)?; 79 let config: Cfg = config::Config::builder()
61 let config = toml::from_str::<Cfg>(&content)?; 80 .add_source(config::File::with_name(&configfile).required(false))
81 .add_source(
82 config::Environment::with_prefix("MODLIST").separator("_"),
83 )
84 .build()
85 .unwrap()
86 .try_deserialize()
87 .unwrap();
88
62 //Check cache 89 //Check cache
63 if !Path::new(&config.cache).exists() { 90 if !Path::new(&config.cache).exists() {
64 create_cache(&config.cache)?; 91 create_cache(&config.cache)?;
@@ -81,60 +108,28 @@ impl Cfg {
81} 108}
82 109
83fn create_config(path: &str) -> MLE<()> { 110fn create_config(path: &str) -> MLE<()> {
84 let p = ProgressBar::new(1);
85 p.set_style(ProgressStyle::with_template("{wide_msg}").unwrap());
86 p.set_message("Create default config");
87
88 let cache_dir = dirs::cache_dir()
89 .unwrap()
90 .join("modlist")
91 .to_string_lossy()
92 .to_string();
93 let default_cfg = Cfg {
94 data: cache_dir.clone(),
95 cache: format!("{cache_dir}/cache"),
96 versions: cache_dir,
97 defaults: Defaults {
98 modloader: Modloader::Fabric,
99 version: VersionLevel::Release,
100 },
101 apis: Apis {
102 modrinth: String::from("https://api.modrinth.com/v2/"),
103 },
104 };
105 create_dir_all(path.split("config.toml").collect::<Vec<&str>>()[0])?; 111 create_dir_all(path.split("config.toml").collect::<Vec<&str>>()[0])?;
106 let mut file = File::create(path)?; 112 let mut file = File::create(path)?;
107 file.write_all(toml::to_string(&default_cfg)?.as_bytes())?; 113 file.write_all(toml::to_string(&Cfg::default())?.as_bytes())?;
108 p.finish_with_message(format!("Created default config ({path})")); 114 println!("Created default config ({path})");
109 Ok(()) 115 Ok(())
110} 116}
111 117
112fn create_database(path: &str) -> MLE<()> { 118fn create_database(path: &str) -> MLE<()> {
113 let p = ProgressBar::new(1);
114 p.set_style(ProgressStyle::with_template("{wide_msg}").unwrap());
115 p.set_message("Create database");
116
117 File::create(path)?; 119 File::create(path)?;
118 setup(path)?; 120 setup(path)?;
119 p.finish_with_message(format!("Created database ({path})")); 121 println!("Created database ({path})");
120 Ok(()) 122 Ok(())
121} 123}
122 124
123fn create_cache(path: &str) -> MLE<()> { 125fn create_cache(path: &str) -> MLE<()> {
124 let p = ProgressBar::new(1);
125 p.set_style(ProgressStyle::with_template("{wide_msg}").unwrap());
126 p.set_message("Create cache");
127
128 create_dir_all(path)?; 126 create_dir_all(path)?;
129 p.finish_with_message(format!("Created cache ({path})")); 127 println!("Created cache ({path})");
130 Ok(()) 128 Ok(())
131} 129}
132 130
133fn create_versions_dummy(path: &str) -> MLE<()> { 131fn create_versions_dummy(path: &str) -> MLE<()> {
134 let p = ProgressBar::new(1);
135 p.set_style(ProgressStyle::with_template("{wide_msg}").unwrap());
136 p.set_message("Create version file");
137 File::create(path)?; 132 File::create(path)?;
138 p.finish_with_message(format!("Created version file ({path})")); 133 println!("Created version file ({path})");
139 Ok(()) 134 Ok(())
140} 135}