2022-01-24 22:15:55 +00:00
|
|
|
use super::Config;
|
2022-01-22 21:02:51 +00:00
|
|
|
|
2022-01-26 19:57:51 +00:00
|
|
|
use anyhow::Error;
|
2022-03-10 21:02:03 +00:00
|
|
|
use colored::Colorize;
|
2022-01-26 18:55:08 +00:00
|
|
|
use dirs::config_dir;
|
2022-01-27 13:14:46 +00:00
|
|
|
use log::warn;
|
2022-01-24 22:15:55 +00:00
|
|
|
use std::{
|
|
|
|
fs::{create_dir_all, read_to_string, File},
|
2022-03-10 21:08:01 +00:00
|
|
|
io::{self, Write as _},
|
2022-01-24 22:15:55 +00:00
|
|
|
path::Path,
|
|
|
|
};
|
2022-01-27 13:14:46 +00:00
|
|
|
use toml::from_str as toml_from_str;
|
2022-01-24 22:15:55 +00:00
|
|
|
|
2022-01-26 19:57:51 +00:00
|
|
|
const CONFIG_DIR: &str = "/imgurs/config.toml";
|
|
|
|
|
2022-01-24 22:15:55 +00:00
|
|
|
pub fn parse() -> Config {
|
2022-03-10 21:02:03 +00:00
|
|
|
toml().unwrap_or_else(|err| {
|
|
|
|
let mut stdout = std::io::stdout();
|
2022-01-24 22:15:55 +00:00
|
|
|
|
2022-03-10 21:02:03 +00:00
|
|
|
write!(stdout, "{}", "The configuration file could not be opened. Do you want to create/overwrite with DEFAULT values? (Y/n): ".yellow()).unwrap();
|
|
|
|
stdout.flush().unwrap();
|
2022-01-24 22:15:55 +00:00
|
|
|
|
2022-03-10 21:02:03 +00:00
|
|
|
let mut value = String::new();
|
|
|
|
io::stdin()
|
|
|
|
.read_line(&mut value)
|
|
|
|
.expect("failed to read line");
|
2022-01-22 21:02:51 +00:00
|
|
|
|
2022-03-10 21:02:03 +00:00
|
|
|
if value.to_lowercase() != "n\n" {
|
|
|
|
warn!("Parse toml config: {err}! Creating config file...");
|
2022-01-24 22:15:55 +00:00
|
|
|
|
2022-03-10 21:02:03 +00:00
|
|
|
let default_config = include_str!(concat!("../../config.toml"));
|
2022-01-24 22:15:55 +00:00
|
|
|
|
2022-03-10 21:02:03 +00:00
|
|
|
let sys_config_dir = config_dir().expect("find config dir");
|
|
|
|
let config_dir = format!("{}{CONFIG_DIR}", sys_config_dir.to_string_lossy());
|
|
|
|
let config_path = Path::new(&config_dir);
|
2022-01-24 22:15:55 +00:00
|
|
|
|
2022-03-10 21:02:03 +00:00
|
|
|
let prefix = config_path.parent().unwrap();
|
|
|
|
create_dir_all(prefix).expect("create config dir");
|
|
|
|
|
|
|
|
let mut file_ref = File::create(config_path).expect("create failed");
|
|
|
|
|
|
|
|
file_ref
|
|
|
|
.write_all(default_config.as_bytes())
|
|
|
|
.expect("failed write default config to file");
|
|
|
|
|
|
|
|
toml().expect("parse toml config")
|
|
|
|
} else {
|
|
|
|
panic!("New config creation cancelled!")
|
|
|
|
}
|
2022-01-24 22:15:55 +00:00
|
|
|
})
|
2022-01-24 19:48:24 +00:00
|
|
|
}
|
|
|
|
|
2022-01-26 19:57:51 +00:00
|
|
|
fn toml() -> Result<Config, Error> {
|
2022-01-22 21:02:51 +00:00
|
|
|
let config_dir = config_dir().unwrap();
|
2022-01-27 13:14:46 +00:00
|
|
|
let file_dir = format!("{}{CONFIG_DIR}", config_dir.to_string_lossy());
|
2022-01-27 10:03:37 +00:00
|
|
|
let toml_str = read_to_string(file_dir).map_err(Error::new)?;
|
2022-01-27 13:14:46 +00:00
|
|
|
let decode = toml_from_str(&toml_str).map_err(Error::new)?;
|
2022-01-22 21:02:51 +00:00
|
|
|
|
|
|
|
Ok(decode)
|
|
|
|
}
|