Added global config directory

+ Should be platform independent thanks to platform-dirs
+ All config files in the directory are used
+ Some optimizations could be made so now all config files are loaded.
+ clippy + fmt
This commit is contained in:
2022-05-03 20:11:54 -06:00
parent ace4b9aa10
commit 5a98f84063
5 changed files with 179 additions and 28 deletions
+73 -21
View File
@@ -2,14 +2,15 @@ pub mod format;
mod printers;
use crate::formatter::format::Format;
use platform_dirs::AppDirs;
use rust_embed::RustEmbed;
use serde::Deserialize;
use std::error::Error;
use std::fmt::{Display, Formatter};
use std::fs::File;
use std::fs::{read_dir, File};
use std::io::Read;
use std::path::PathBuf;
use std::str;
use std::{fs, str};
#[derive(Debug)]
#[allow(clippy::enum_variant_names)]
@@ -63,21 +64,17 @@ pub struct FormatConfig {
}
impl FormatConfig {
pub fn new(config_path: &Option<PathBuf>) -> Result<Self, FormatConfigError> {
let mut config_str = String::new();
let mut config: FormatConfig = if let Some(config_path) = config_path {
let mut config = File::open(config_path)?;
config.read_to_string(&mut config_str)?;
toml::from_str(&config_str).map_err(|e| FormatConfigError::TomlError {
file_name: config_path.to_str().unwrap().to_string(),
err: e,
})?
} else {
FormatConfig::default()
};
fn parse_config_file(
src_file_name: &str,
config_data: &str,
) -> Result<Self, FormatConfigError> {
toml::from_str(config_data).map_err(|e| FormatConfigError::TomlError {
file_name: src_file_name.to_string(),
err: e,
})
}
fn get_built_in_config(&mut self) -> Result<(), FormatConfigError> {
for format_file_path in BuiltInFormats::iter() {
if format_file_path.ends_with("md") {
continue;
@@ -88,14 +85,69 @@ impl FormatConfig {
let config_str = str::from_utf8(&format_file.data).unwrap();
let mut built_in: FormatConfig =
toml::from_str(config_str).map_err(|e| FormatConfigError::TomlError {
file_name: format_file_path.to_string(),
err: e,
})?;
Self::parse_config_file(&format_file_path, config_str)?;
config.formats.append(&mut built_in.formats);
self.formats.append(&mut built_in.formats);
}
Ok(())
}
fn get_file_config(&mut self, config_path: &Option<PathBuf>) -> Result<(), FormatConfigError> {
if let Some(config_path) = config_path {
let mut config_file = File::open(config_path)?;
let mut config_data = String::new();
config_file.read_to_string(&mut config_data)?;
let mut arg_config =
Self::parse_config_file(config_path.to_str().unwrap(), &config_data)?;
self.formats.append(&mut arg_config.formats);
}
Ok(())
}
fn parse_directory(
&mut self,
directory_path: &Option<PathBuf>,
) -> Result<(), FormatConfigError> {
if let Some(directory_path) = directory_path {
for path in read_dir(directory_path)? {
let file_name = path?.file_name();
let config_file_path = directory_path.join(file_name);
self.get_file_config(&Some(config_file_path))?;
}
}
Ok(())
}
pub fn new(
config_path: &Option<PathBuf>,
global_config_path: &Option<PathBuf>,
) -> Result<Self, FormatConfigError> {
let mut config = FormatConfig::default();
config.get_built_in_config()?;
config.get_file_config(config_path)?;
let global_dir = match global_config_path {
Some(g) => g.clone(),
None => {
let app_dirs = AppDirs::new(Some("formaty"), true).unwrap();
if !app_dirs.config_dir.exists() {
fs::create_dir(&app_dirs.config_dir)?;
}
app_dirs.config_dir
}
};
config.parse_directory(&Some(global_dir))?;
Ok(config)
}
+10 -1
View File
@@ -22,6 +22,15 @@ pub struct Args {
)]
config: Option<PathBuf>,
#[structopt(
short = "g",
long = "global_config",
env = "FORMATY_GLOBAL_CONFIG",
parse(from_os_str),
help = "Path to the global config directory"
)]
global_config: Option<PathBuf>,
#[structopt(
help = "Input data type",
short = "i",
@@ -43,7 +52,7 @@ pub struct Args {
fn init() -> Result<(Vec<u8>, Format), FormatyError> {
let args: Args = Args::from_args();
let config = FormatConfig::new(&args.config)?;
let config = FormatConfig::new(&args.config, &args.global_config)?;
let format = config.get_format(&args.format)?;