mirror of
https://github.com/MedzikUser/imgurs
synced 2024-08-15 00:03:19 +00:00
59 lines
1.8 KiB
Rust
59 lines
1.8 KiB
Rust
use super::Config;
|
|
|
|
use colored::Colorize;
|
|
use dirs::config_dir;
|
|
use log::warn;
|
|
use std::{
|
|
fs::{create_dir_all, read_to_string, File},
|
|
io::{self, Write as _},
|
|
path::Path,
|
|
};
|
|
use toml::from_str as toml_from_str;
|
|
|
|
const CONFIG_DIR: &str = "/imgurs/config.toml";
|
|
|
|
pub fn parse() -> Config {
|
|
toml().unwrap_or_else(|err| {
|
|
let mut stdout = std::io::stdout();
|
|
|
|
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();
|
|
|
|
let mut value = String::new();
|
|
io::stdin()
|
|
.read_line(&mut value)
|
|
.expect("failed to read line");
|
|
|
|
if value.to_lowercase() != "n\n" {
|
|
warn!("Parse toml config: {err}! Creating config file...");
|
|
|
|
let default_config = include_str!(concat!("../../config.toml"));
|
|
|
|
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);
|
|
|
|
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!")
|
|
}
|
|
})
|
|
}
|
|
|
|
fn toml() -> anyhow::Result<Config> {
|
|
let config_dir = config_dir().unwrap();
|
|
let file_dir = format!("{}{CONFIG_DIR}", config_dir.to_string_lossy());
|
|
let toml_str = read_to_string(file_dir)?;
|
|
let decode = toml_from_str(&toml_str)?;
|
|
|
|
Ok(decode)
|
|
}
|