diff options
Diffstat (limited to 'src/config.rs')
-rw-r--r-- | src/config.rs | 74 |
1 files changed, 57 insertions, 17 deletions
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 | } | ||