From bef3769caeaa0f6daa007f5b76c94129a2b93f2a Mon Sep 17 00:00:00 2001 From: Simmer505 Date: Tue, 30 Jan 2024 15:28:39 -0600 Subject: [PATCH] Refactored and added basic handling --- .gitignore | 2 +- src/lib.rs | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 100 ++-------------------------------------- 3 files changed, 134 insertions(+), 97 deletions(-) create mode 100644 src/lib.rs diff --git a/.gitignore b/.gitignore index a9d37c5..2c96eb1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -target +target/ Cargo.lock diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..1d4ddfa --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,129 @@ +use std::fs; +use std::path::PathBuf; +use std::env; +use std::error::Error; +use clap::{Arg, Command, ArgAction, ArgMatches}; + +pub struct Config { + git_location: PathBuf, + sys_location: PathBuf, + is_dir: bool, +} + +impl Config { + pub fn new(rel_git_location: &str, sys_location: &str) -> Result> { + + let home_dir = PathBuf::from(env::var("HOME").expect("$HOME not set")); + let manager_dir = home_dir.join(".dotfiles/"); + + let git_location = manager_dir.join(rel_git_location); + let sys_location = PathBuf::from(sys_location); + + let is_dir = fs::metadata(&git_location)?.is_dir(); + + Ok(Self { git_location, sys_location, is_dir }) + } + +} + + +pub fn parse_args() -> ArgMatches { + + let matches = Command::new("dotfiles") + .version("0.1") + .author("Ethan Simmons") + .about("Manages dotfiles") + .arg(Arg::new("from-git") + .short('g') + .long("from-git") + .action(ArgAction::SetTrue) + ) + .get_matches(); + + matches +} + + + fn copy_config(config: &Config, to_sys: bool) -> Result<(), Box> { + if !config.is_dir { + if to_sys { + let _ = fs::copy(&config.git_location, &config.sys_location)?; + Ok(()) + } else { + let _ = fs::copy(&config.sys_location, &config.git_location)?; + Ok(()) + } + } else { + let (dir, dest) = if to_sys { + (&config.git_location, &config.sys_location) + } else { + (&config.sys_location, &config.git_location) + }; + + println!("Starting Copy Dir"); + copy_directory(&dir, &dest) + } +} + + +fn copy_directory(dir_path: &PathBuf, dest_path: &PathBuf) -> Result<(), Box> { + + let dir = fs::read_dir(&dir_path).unwrap(); + + let entries: Vec<_> = dir.map(|entry| entry.unwrap()).collect(); + + let files = entries.iter().filter(|entry| entry.metadata().unwrap().is_file()); + let dirs = entries.iter().filter(|entry| entry.metadata().unwrap().is_dir()); + + files.for_each(|file| { + let file_path = dir_path.join(file.file_name()); + let dest_path = dest_path.join(file.file_name()); + + let _ = fs::copy(file_path, dest_path); + + println!("Copying file"); + }); + + dirs.for_each(|dir| { + let current_dir_path = dir_path.join(dir.file_name()); + let dest_path = dest_path.join(dir.file_name()); + + println!("Copying dir"); + let _ = copy_directory(¤t_dir_path, &dest_path); + + }); + + Ok(()) + +} + + +pub fn run(args: ArgMatches) -> Result<(), Box>{ + + let configs = vec![ + Config::new("desktop/sway/config", "/home/eesim/.config/sway/config")?, + Config::new("desktop/nvim/init.lua", "/home/eesim/.config/nvim/init.lua")?, + Config::new("desktop/nvim/lua/config", "/home/eesim/.config/nvim/lua/config")?, + Config::new("desktop/alacritty/alacritty.toml", "/home/eesim/.config/alacritty/alacritty.toml")?, + Config::new("desktop/rofi/config.rasi", "/home/eesim/.config/rofi/config.rasi")?, + ]; + + let copy_to_sys = args.get_flag("from-git"); + + let copy_results = configs.iter().map(|config| (copy_config(&config, copy_to_sys), config)); + + copy_results.for_each(|result| { + if let Err(e) = result.0 { + let failed_config = result.1; + + if copy_to_sys { + println!("Faled to copy {}, with error: {}", failed_config.git_location.to_str().unwrap(), e); + } + } + }); + + + Ok(()) + +} + diff --git a/src/main.rs b/src/main.rs index ed2c1c6..56f7a4b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,102 +1,10 @@ -use std::fs; -use std::path::PathBuf; -use std::env; -use clap::{Arg, Command, ArgAction}; +use dotfiles_manager; + fn main() { + let args = dotfiles_manager::parse_args(); - let matches = Command::new("dotfiles") - .version("0.1") - .author("Ethan Simmons") - .about("Manages dotfiles") - .arg(Arg::new("from-git") - .short('g') - .long("from-git") - .action(ArgAction::SetTrue) - ) - .get_matches(); + let _ = dotfiles_manager::run(args); - let configs = vec![ - Config::new("desktop/sway/config", "/home/eesim/.config/sway/config"), - Config::new("desktop/nvim/init.lua", "/home/eesim/.config/nvim/init.lua"), - Config::new("desktop/nvim/lua/config", "/home/eesim/.config/nvim/lua/config"), - Config::new("desktop/alacritty/alacritty.toml", "/home/eesim/.config/alacritty/alacritty.toml"), - Config::new("desktop/rofi/config.rasi", "/home/eesim/.config/rofi/config.rasi") - ]; - - - let copy_to_sys = matches.get_flag("from-git"); - - configs.iter().for_each(|config| copy_config(&config, copy_to_sys)); - -} - -struct Config { - git_location: PathBuf, - sys_location: PathBuf, - is_dir: bool, -} - -impl Config { - fn new(rel_git_location: &str, sys_location: &str) -> Self { - - let home_dir = PathBuf::from(env::var("HOME").expect("$HOME not set")); - let manager_dir = home_dir.join(".dotfiles/"); - - let git_location = manager_dir.join(rel_git_location); - let sys_location = PathBuf::from(sys_location); - - let is_dir = fs::metadata(&git_location).unwrap().is_dir(); - - Self { git_location, sys_location, is_dir } - } -} - - -fn copy_config(config: &Config, to_sys: bool) { - if !config.is_dir { - if to_sys { - let _ = fs::copy(&config.git_location, &config.sys_location); - } else { - let _ = fs::copy(&config.sys_location, &config.git_location); - } - } else { - let (dir, dest) = if to_sys { - (&config.git_location, &config.sys_location) - } else { - (&config.sys_location, &config.git_location) - }; - - println!("Starting Copy Dir"); - copy_directory(&dir, &dest) - } -} - -fn copy_directory(dir_path: &PathBuf, dest_path: &PathBuf) { - - let dir = fs::read_dir(&dir_path).unwrap(); - - let entries: Vec<_> = dir.map(|entry| entry.unwrap()).collect(); - - let files = entries.iter().filter(|entry| entry.metadata().unwrap().is_file()); - let dirs = entries.iter().filter(|entry| entry.metadata().unwrap().is_dir()); - - files.for_each(|file| { - let file_path = dir_path.join(file.file_name()); - let dest_path = dest_path.join(file.file_name()); - - let _ = fs::copy(file_path, dest_path); - - println!("Copying file"); - }); - - dirs.for_each(|dir| { - let current_dir_path = dir_path.join(dir.file_name()); - let dest_path = dest_path.join(dir.file_name()); - - println!("Copying dir"); - copy_directory(¤t_dir_path, &dest_path); - - }); }