summaryrefslogtreecommitdiff
path: root/src/config.rs
diff options
context:
space:
mode:
authorfx <[email protected]>2023-04-27 10:10:03 +0200
committerfx <[email protected]>2023-04-27 10:10:03 +0200
commit43ca5fec20933fc31dfe7d7dbd1f1b9258612219 (patch)
treeeef58fecfadad90386b38af581abab8cc3d3cfc0 /src/config.rs
parent4300ad2eb05dddfa4274e04b204f2ad28c87da05 (diff)
parent4e6466af1329f7b9e341df2e76ab696d11f80c93 (diff)
downloadmodlist-43ca5fec20933fc31dfe7d7dbd1f1b9258612219.tar
modlist-43ca5fec20933fc31dfe7d7dbd1f1b9258612219.tar.gz
modlist-43ca5fec20933fc31dfe7d7dbd1f1b9258612219.zip
Merge pull request 'cache' (#3) from cache into master
Reviewed-on: http://raspberrypi.fritz.box:7920/fx/modlist/pulls/3
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs84
1 files changed, 67 insertions, 17 deletions
diff --git a/src/config.rs b/src/config.rs
index 1b54d5f..61db1c7 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -1,15 +1,17 @@
1use std::{ 1use std::{
2 fs::File, 2 fs::{create_dir_all, File},
3 io::{Read, Write}, 3 io::{Read, Write},
4 path::Path,
4}; 5};
5 6
6use serde::{Deserialize, Serialize}; 7use serde::{Deserialize, Serialize};
7 8
8use crate::{devdir, error::MLE}; 9use crate::{db::db_setup, error::MLE};
9 10
10#[derive(Debug, Clone, Serialize, Deserialize)] 11#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct Cfg { 12pub struct Cfg {
12 pub data: String, 13 pub data: String,
14 pub cache: String,
13 pub apis: Apis, 15 pub apis: Apis,
14} 16}
15 17
@@ -19,24 +21,22 @@ pub struct Apis {
19} 21}
20 22
21impl Cfg { 23impl Cfg {
22 pub fn init(filename: &str) -> MLE<Self> { 24 pub fn init(path: Option<String>) -> MLE<Self> {
23 let configfile = dirs::config_dir().unwrap().join(filename); 25 let configfile = match path.clone() {
26 Some(p) => String::from(p),
27 None => dirs::config_dir()
28 .unwrap()
29 .join("modlist.toml")
30 .to_string_lossy()
31 .to_string(),
32 };
24 33
25 let mut file = match File::open(devdir(configfile.to_str().unwrap())) { 34 let mut file = match File::open(&configfile) {
26 Ok(file) => file, 35 Ok(file) => file,
27 Err(err) => { 36 Err(err) => {
28 if err.kind() == std::io::ErrorKind::NotFound { 37 if err.kind() == std::io::ErrorKind::NotFound && path.is_none() {
29 println!("No config file found, creating one"); 38 create_config(&configfile)?;
30 let default_cfg = Cfg { 39 File::open(&configfile)?
31 data: String::from("./"),
32 apis: Apis {
33 modrinth: String::from("https://api.modrinth.com/v2/"),
34 },
35 };
36 let mut file = File::create(devdir(configfile.to_str().unwrap()))?;
37 println!("Created config file");
38 file.write_all(toml::to_string(&default_cfg)?.as_bytes())?;
39 File::open(devdir(configfile.to_str().unwrap()))?
40 } else { 40 } else {
41 return Err(err.into()); 41 return Err(err.into());
42 } 42 }
@@ -45,6 +45,56 @@ impl Cfg {
45 let mut content = String::new(); 45 let mut content = String::new();
46 file.read_to_string(&mut content)?; 46 file.read_to_string(&mut content)?;
47 let config = toml::from_str::<Cfg>(&content)?; 47 let config = toml::from_str::<Cfg>(&content)?;
48 //Check cache
49 if !Path::new(&config.cache).exists() {
50 create_cache(&config.cache)?;
51 };
52 //Check database
53 //TODO check file
54 let datafile = format!("{}/data.db", config.data);
55 match File::open(&datafile) {
56 Ok(..) => (),
57 Err(..) => create_database(&datafile)?,
58 };
48 Ok(config) 59 Ok(config)
49 } 60 }
50} 61}
62
63fn create_config(path: &str) -> MLE<()> {
64 print!("No config file found, create default");
65 //Force flush of stdout, else print! doesn't print instantly
66 std::io::stdout().flush()?;
67 let default_cfg = Cfg {
68 //TODO get home dir
69 data: String::from("$HOME/.cache/modlist/"),
70 cache: String::from("$HOME/.cache/modlist/cache"),
71 apis: Apis {
72 modrinth: String::from("https://api.modrinth.com/v2/"),
73 },
74 };
75 let mut file = File::create(path)?;
76 file.write_all(toml::to_string(&default_cfg)?.as_bytes())?;
77 println!(" ✓");
78 Ok(())
79}
80
81fn create_database(path: &str) -> MLE<()> {
82 print!("No database found, create base");
83 //Force flush of stdout, else print! doesn't print instantly
84 std::io::stdout().flush()?;
85
86 File::create(path)?;
87 db_setup(path)?;
88 println!(" ✓");
89 Ok(())
90}
91
92fn create_cache(path: &str) -> MLE<()> {
93 print!("No cache direcory found, create one");
94 //Force flush of stdout, else print! doesn't print instantly
95 std::io::stdout().flush()?;
96
97 create_dir_all(path)?;
98 println!(" ✓");
99 Ok(())
100}