diff options
Diffstat (limited to 'src/conf')
-rw-r--r-- | src/conf/conf.rs | 23 | ||||
-rw-r--r-- | src/conf/default.rs | 34 | ||||
-rw-r--r-- | src/conf/mod.rs | 8 |
3 files changed, 43 insertions, 22 deletions
diff --git a/src/conf/conf.rs b/src/conf/conf.rs index 1fe2df0..4ccd302 100644 --- a/src/conf/conf.rs +++ b/src/conf/conf.rs @@ -19,10 +19,7 @@ use { crossterm::style::Attribute, fnv::FnvHashMap, serde::Deserialize, - std::{ - fs, io, - path::{Path, PathBuf}, - }, + std::path::PathBuf, }; macro_rules! overwrite { @@ -127,29 +124,23 @@ impl Conf { /// read the configuration file from the default OS specific location. /// Create it if it doesn't exist pub fn from_default_location() -> Result<Conf, ProgramError> { + let conf_dir = super::dir(); let conf_filepath = Conf::default_location(); if !conf_filepath.exists() { - Conf::write_sample(&conf_filepath)?; + write_default_conf_in(conf_dir)?; println!( - "New Configuration file written in {}{:?}{}.", + "New Configuration files written in {}{:?}{}.", Attribute::Bold, - &conf_filepath, + &conf_dir, Attribute::Reset, ); - println!("You should have a look at it."); + println!("You should have a look at them."); } let mut conf = Conf::default(); - conf.read_file(conf_filepath.clone())?; + conf.read_file(conf_filepath)?; Ok(conf) } - /// assume the file doesn't yet exist - pub fn write_sample(filepath: &Path) -> Result<(), io::Error> { - fs::create_dir_all(filepath.parent().unwrap())?; - fs::write(filepath, DEFAULT_CONF_FILE)?; - Ok(()) - } - pub fn solve_conf_path(&self, path: &str) -> Option<PathBuf> { if path.ends_with(".toml") || path.ends_with(".hjson") { for conf_file in self.files.iter().rev() { diff --git a/src/conf/default.rs b/src/conf/default.rs new file mode 100644 index 0000000..93a87e2 --- /dev/null +++ b/src/conf/default.rs @@ -0,0 +1,34 @@ +use { + include_dir::{Dir, include_dir}, + std::{ + fs, + io, + path::Path, + }, +}; + +static DEFAULT_CONF_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/resources/default-conf"); + +pub fn write_default_conf_in(dir: &Path) -> Result<(), io::Error> { + info!("writing default conf in {:?}", dir); + if dir.exists() { + if !dir.is_dir() { + return Err(io::Error::new( + io::ErrorKind::Other, + format!("{:?} isn't a directory", dir), + )); + } + } else { + fs::create_dir_all(dir)?; + } + for file in DEFAULT_CONF_DIR.files() { + let dest_path = dir.join(file.path()); + if dest_path.exists() { + warn!("not overwriting {:?}", dest_path); + } else { + info!("writing file {:?}", file.path()); + fs::write(dest_path, file.contents())?; + } + } + Ok(()) +} diff --git a/src/conf/mod.rs b/src/conf/mod.rs index 8a9f1f6..1b8db40 100644 --- a/src/conf/mod.rs +++ b/src/conf/mod.rs @@ -4,12 +4,14 @@ use { }; mod conf; +mod default; mod format; mod import; mod verb_conf; pub use { conf::Conf, + default::write_default_conf_in, format::*, import::*, once_cell::sync::Lazy, @@ -17,12 +19,6 @@ pub use { }; -/// the content of the conf.hjson file broot creates when there's none. -/// -/// It features some default configuration and many sections the user -/// can uncomment then edit. -pub const DEFAULT_CONF_FILE: &str = include_str!("../../resources/default-conf.hjson"); - /// return the instance of ProjectDirs holding broot's specific paths pub fn app_dirs() -> directories::ProjectDirs { directories::ProjectDirs::from("org", "dystroy", "broot") |