From d0afcd5de50e240726a9ec704d9a5db85ff9946a Mon Sep 17 00:00:00 2001 From: Ethan Simmons Date: Fri, 2 Feb 2024 00:18:32 -0600 Subject: [PATCH] Changed variable names and fixed program not creating directories if they do not exist --- src/config/mod.rs | 68 +++++++++++++++++++++++++++++ src/{config_file => dotfile}/mod.rs | 26 ++++++++--- src/lib.rs | 17 ++++---- src/main.rs | 4 +- src/program_config/mod.rs | 57 ------------------------ 5 files changed, 99 insertions(+), 73 deletions(-) create mode 100644 src/config/mod.rs rename src/{config_file => dotfile}/mod.rs (65%) delete mode 100644 src/program_config/mod.rs diff --git a/src/config/mod.rs b/src/config/mod.rs new file mode 100644 index 0000000..7e0a0b8 --- /dev/null +++ b/src/config/mod.rs @@ -0,0 +1,68 @@ +use std::fs; +use std::str; +use std::path::PathBuf; +use std::error::Error; +use toml::Table; + +use crate::dotfile::Dotfile; + + +pub struct Config { + pub manager_dir: PathBuf, + pub dotfiles: Vec, +} + + +impl Config { + pub fn parse(config: PathBuf) -> Result> { + + let config_file: Table = str::from_utf8(&fs::read(config)?)?.parse()?; + + let read_dotfiles = config_file.get("dotfiles").expect("No dotfiles section in config"); + + let dotfile_array = read_dotfiles.as_array().expect("Invalid config file format").iter(); + + let dotfiles = dotfile_array.map(|dotfile| { + + let dotfile_table = dotfile.as_table().unwrap(); + + let manager_path = PathBuf::from( + match dotfile_table.get("manager_path") { + Some(path) => path.as_str().expect("Invalid character in dotfile path"), + None => return None, + } + ); + + let system_path = PathBuf::from( + match dotfile_table.get("system_path") { + Some(path) => path.as_str().expect("Invalid character in dotfile path"), + None => return None, + } + ); + + match Dotfile::new(manager_path, system_path) { + Ok(dotfile) => Some(dotfile), + Err(e) => { + println!("Failed to read dotfile: {}", e); + None + } + } + }); + + let valid_dotfiles: Vec = dotfiles.filter_map(|dotfile| match dotfile { + Some(dotfile) => Some(dotfile), + None => { + println!("Failed to parse config"); + None + }, + }).collect(); + + let manager_dir = if config_file.contains_key("manager_directory") { + PathBuf::from(config_file.get("manager_directory").unwrap().as_str().unwrap()) + } else { + PathBuf::from("$HOME/.dotfiles") + }; + + Ok(Config{manager_dir, dotfiles: valid_dotfiles}) + } +} diff --git a/src/config_file/mod.rs b/src/dotfile/mod.rs similarity index 65% rename from src/config_file/mod.rs rename to src/dotfile/mod.rs index a1c4c42..3e5799d 100644 --- a/src/config_file/mod.rs +++ b/src/dotfile/mod.rs @@ -5,14 +5,14 @@ use std::fs; use crate::copy_directory; -pub struct ConfigFile { +pub struct Dotfile { pub manager_path: PathBuf, pub system_path: PathBuf, is_dir: bool, } -impl ConfigFile { +impl Dotfile { pub fn new(rel_git_location: PathBuf, sys_location: PathBuf) -> Result> { let home_dir = PathBuf::from(env::var("HOME").expect("$HOME not set")); @@ -26,15 +26,31 @@ impl ConfigFile { let is_dir = match (manager_path_data, sys_path_data) { (Ok(manager_data), Ok(sys_data)) => manager_data.is_dir() && sys_data.is_dir(), - (Ok(manager_data), Err(_)) => manager_data.is_dir(), - (Err(_), Ok(sys_data)) => sys_data.is_dir(), + (Ok(manager_data), Err(_)) => { + if manager_data.is_dir() { + let _ = fs::create_dir_all(&system_path); + true + } else { + let _ = fs::create_dir_all(&system_path.parent().unwrap()); + false + } + }, + (Err(_), Ok(sys_data)) => { + if sys_data.is_dir() { + let _ = fs::create_dir_all(&manager_path); + true + } else { + let _ = fs::create_dir_all(&manager_path.parent().unwrap()); + false + } + }, (Err(e1), Err(e2)) => panic!("Neither {} nor {} exists or is readable: {}, {}", manager_path.to_str().unwrap(), system_path.to_str().unwrap(), e1, e2), }; Ok(Self { manager_path, system_path, is_dir }) } - pub fn copy_config(&self, to_sys: bool) -> Result<(), Box> { + pub fn copy_dotfile(&self, to_sys: bool) -> Result<(), Box> { let (curr, dest) = if to_sys { (&self.manager_path, &self.system_path) diff --git a/src/lib.rs b/src/lib.rs index a61b0bf..5ced9ba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,11 +3,11 @@ use std::path::{self, PathBuf}; use std::error::Error; use clap::ArgMatches; -pub mod config_file; -pub mod program_config; +pub mod config; +pub mod dotfile; pub mod args; -use program_config::ProgramConfig; +use config::Config; fn copy_directory(dir_path: &PathBuf, dest_path: &PathBuf) -> Result<(), Box> { @@ -50,24 +50,23 @@ fn copy_directory(dir_path: &PathBuf, dest_path: &PathBuf) -> Result<(), Box Result<(), Box> { +pub fn run(args: ArgMatches, config: Config) -> Result<(), Box> { - let configs = program_config.configs; + let dotfiles = config.dotfiles; let copy_to_sys = args.get_flag("from-git"); - let copy_results = configs.iter().map(|config| (config.copy_config(copy_to_sys), config)); + let copy_results = dotfiles.iter().map(|dotfile| (dotfile.copy_dotfile(copy_to_sys), dotfile)); copy_results.for_each(|result| { if let Err(e) = result.0 { - let failed_config = result.1; + let failed_dotfile = result.1; if copy_to_sys { - println!("Faled to copy {}, with error: {}", failed_config.manager_path.to_str().expect("Error printing error"), e); + println!("Faled to copy {}, with error: {}", failed_dotfile.manager_path.to_str().expect("Error printing error"), e); } } }); Ok(()) } - diff --git a/src/main.rs b/src/main.rs index a95b2fc..b582aee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,13 @@ use std::path::PathBuf; use dotfiles_manager::args; -use dotfiles_manager::program_config::ProgramConfig; +use dotfiles_manager::config::Config; fn main() { let args = args::parse_args(); - let program_config = ProgramConfig::parse(PathBuf::from("/home/eesim/.config/dotfiles/config")); + let program_config = Config::parse(PathBuf::from("/home/eesim/.config/dotfiles/config")); if let Err(e) = dotfiles_manager::run(args, program_config.unwrap()) { panic!("Error: {}", e) diff --git a/src/program_config/mod.rs b/src/program_config/mod.rs deleted file mode 100644 index 12117a1..0000000 --- a/src/program_config/mod.rs +++ /dev/null @@ -1,57 +0,0 @@ -use std::fs; -use std::str; -use std::path::PathBuf; -use std::error::Error; -use toml::Table; - -use crate::config_file::ConfigFile; - - -pub struct ProgramConfig { - pub manager_dir: PathBuf, - pub configs: Vec, -} - - -impl ProgramConfig { - pub fn parse(config: PathBuf) -> Result> { - - let config_file: Table = str::from_utf8(&fs::read(config)?)?.parse()?; - - let configs = { - let read_configs = config_file.get("configs").unwrap(); - - let configs = read_configs.as_array().unwrap().iter(); - - configs.map(|config| { - - let table = config.as_table().unwrap(); - let manager_path = PathBuf::from(table.get("manager_path").unwrap().as_str().unwrap()); - let system_path = PathBuf::from(table.get("system_path").unwrap().as_str().unwrap()); - - println!("Manager Path: {}", table.get("manager_path").unwrap().as_str().unwrap()); - println!("System Path: {}", table.get("system_path").unwrap().as_str().unwrap()); - - Some(ConfigFile::new(manager_path, system_path).unwrap()) - }) - - - }; - - let valid_configs: Vec = configs.filter_map(|config| match config { - Some(config) => Some(config), - None => { - println!("Failed to parse config"); - None - }, - }).collect(); - - let manager_dir = if config_file.contains_key("manager_directory") { - PathBuf::from(config_file.get("manager_directory").unwrap().as_str().unwrap()) - } else { - PathBuf::from("$HOME/.dotfiles") - }; - - Ok(ProgramConfig{manager_dir, configs: valid_configs}) - } -}