parent
647a95c1e9
commit
3aa4d9da67
@ -1,58 +1,89 @@
|
|||||||
#![allow(clippy::module_name_repetitions)]
|
#![allow(clippy::module_name_repetitions)]
|
||||||
|
|
||||||
use serde::Deserialize;
|
use config::FileFormat;
|
||||||
use std::{env, fs, path::PathBuf};
|
use lazy_static::lazy_static;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
use super::utils::get_config_dir;
|
||||||
|
|
||||||
|
#[derive(Default, Debug, Deserialize, Serialize)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub base: ConfigBase,
|
pub base: ConfigBase,
|
||||||
pub extra: ConfigExtra,
|
pub extra: ConfigExtra,
|
||||||
pub bin: ConfigBin,
|
pub bin: ConfigBin,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
pub struct ConfigBase {
|
pub struct ConfigBase {
|
||||||
pub pacdiff_warn: bool,
|
pub pacdiff_warn: bool,
|
||||||
pub highlight_optdepends: bool,
|
pub highlight_optdepends: bool,
|
||||||
pub powerpill: bool,
|
pub powerpill: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
pub struct ConfigExtra {
|
pub struct ConfigExtra {
|
||||||
pub uwu: Option<bool>,
|
pub uwu: Option<bool>,
|
||||||
pub uwu_debug: Option<bool>,
|
pub uwu_debug: Option<bool>,
|
||||||
pub review_user_shell: bool,
|
pub review_user_shell: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
pub struct ConfigBin {
|
pub struct ConfigBin {
|
||||||
pub sudo: Option<String>,
|
pub sudo: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Config {
|
impl Default for ConfigBase {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
pacdiff_warn: true,
|
||||||
|
highlight_optdepends: true,
|
||||||
|
powerpill: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for ConfigExtra {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
uwu: None,
|
||||||
|
uwu_debug: None,
|
||||||
|
review_user_shell: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for ConfigBin {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
base: ConfigBase {
|
sudo: Some("sudo".to_string()),
|
||||||
pacdiff_warn: true,
|
|
||||||
highlight_optdepends: true,
|
|
||||||
powerpill: false,
|
|
||||||
},
|
|
||||||
extra: ConfigExtra {
|
|
||||||
uwu: None,
|
|
||||||
uwu_debug: None,
|
|
||||||
review_user_shell: false,
|
|
||||||
},
|
|
||||||
bin: ConfigBin {
|
|
||||||
sudo: Some("sudo".to_string()),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read() -> Config {
|
impl Config {
|
||||||
let config_path = PathBuf::from(env::var("HOME").unwrap()).join(".config/ame/config.toml");
|
pub fn read() -> Self {
|
||||||
match fs::read_to_string(config_path) {
|
let config_path = get_config_dir().join("config.toml");
|
||||||
Ok(contents) => toml::from_str(&contents).expect("Could not parse the config file"),
|
if config_path.exists() {
|
||||||
Err(_) => Config::default(),
|
let builder = config::Config::builder()
|
||||||
|
.add_source(config::File::from_str(
|
||||||
|
&toml::ser::to_string(&Config::default()).unwrap(),
|
||||||
|
FileFormat::Toml,
|
||||||
|
))
|
||||||
|
.add_source(config::File::with_name(config_path.to_str().unwrap()));
|
||||||
|
let conf = builder.build().unwrap();
|
||||||
|
conf.try_deserialize().unwrap()
|
||||||
|
} else {
|
||||||
|
let default_conf = Config::default();
|
||||||
|
let toml_string = toml::ser::to_string_pretty(&default_conf).unwrap();
|
||||||
|
fs::write(config_path, toml_string).unwrap();
|
||||||
|
default_conf
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn get() -> &'static Config {
|
||||||
|
lazy_static! {
|
||||||
|
static ref CONFIG: Config = Config::read();
|
||||||
|
}
|
||||||
|
&*CONFIG
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue