Changed variable names and fixed program not creating directories if they do not exist
This commit is contained in:
68
src/config/mod.rs
Normal file
68
src/config/mod.rs
Normal file
@@ -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<Dotfile>,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl Config {
|
||||||
|
pub fn parse(config: PathBuf) -> Result<Self, Box<dyn Error>> {
|
||||||
|
|
||||||
|
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<Dotfile> = 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})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,14 +5,14 @@ use std::fs;
|
|||||||
|
|
||||||
use crate::copy_directory;
|
use crate::copy_directory;
|
||||||
|
|
||||||
pub struct ConfigFile {
|
pub struct Dotfile {
|
||||||
pub manager_path: PathBuf,
|
pub manager_path: PathBuf,
|
||||||
pub system_path: PathBuf,
|
pub system_path: PathBuf,
|
||||||
is_dir: bool,
|
is_dir: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl ConfigFile {
|
impl Dotfile {
|
||||||
pub fn new(rel_git_location: PathBuf, sys_location: PathBuf) -> Result<Self, Box<dyn Error>> {
|
pub fn new(rel_git_location: PathBuf, sys_location: PathBuf) -> Result<Self, Box<dyn Error>> {
|
||||||
|
|
||||||
let home_dir = PathBuf::from(env::var("HOME").expect("$HOME not set"));
|
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) {
|
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), Ok(sys_data)) => manager_data.is_dir() && sys_data.is_dir(),
|
||||||
(Ok(manager_data), Err(_)) => manager_data.is_dir(),
|
(Ok(manager_data), Err(_)) => {
|
||||||
(Err(_), Ok(sys_data)) => sys_data.is_dir(),
|
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),
|
(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 })
|
Ok(Self { manager_path, system_path, is_dir })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn copy_config(&self, to_sys: bool) -> Result<(), Box<dyn Error>> {
|
pub fn copy_dotfile(&self, to_sys: bool) -> Result<(), Box<dyn Error>> {
|
||||||
|
|
||||||
let (curr, dest) = if to_sys {
|
let (curr, dest) = if to_sys {
|
||||||
(&self.manager_path, &self.system_path)
|
(&self.manager_path, &self.system_path)
|
||||||
17
src/lib.rs
17
src/lib.rs
@@ -3,11 +3,11 @@ use std::path::{self, PathBuf};
|
|||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use clap::ArgMatches;
|
use clap::ArgMatches;
|
||||||
|
|
||||||
pub mod config_file;
|
pub mod config;
|
||||||
pub mod program_config;
|
pub mod dotfile;
|
||||||
pub mod args;
|
pub mod args;
|
||||||
|
|
||||||
use program_config::ProgramConfig;
|
use config::Config;
|
||||||
|
|
||||||
fn copy_directory(dir_path: &PathBuf, dest_path: &PathBuf) -> Result<(), Box<dyn Error>> {
|
fn copy_directory(dir_path: &PathBuf, dest_path: &PathBuf) -> Result<(), Box<dyn Error>> {
|
||||||
|
|
||||||
@@ -50,24 +50,23 @@ fn copy_directory(dir_path: &PathBuf, dest_path: &PathBuf) -> Result<(), Box<dyn
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
pub fn run(args: ArgMatches, program_config: ProgramConfig) -> Result<(), Box<dyn Error>> {
|
pub fn run(args: ArgMatches, config: Config) -> Result<(), Box<dyn Error>> {
|
||||||
|
|
||||||
let configs = program_config.configs;
|
let dotfiles = config.dotfiles;
|
||||||
|
|
||||||
let copy_to_sys = args.get_flag("from-git");
|
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| {
|
copy_results.for_each(|result| {
|
||||||
if let Err(e) = result.0 {
|
if let Err(e) = result.0 {
|
||||||
let failed_config = result.1;
|
let failed_dotfile = result.1;
|
||||||
|
|
||||||
if copy_to_sys {
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use dotfiles_manager::args;
|
use dotfiles_manager::args;
|
||||||
use dotfiles_manager::program_config::ProgramConfig;
|
use dotfiles_manager::config::Config;
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args = args::parse_args();
|
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()) {
|
if let Err(e) = dotfiles_manager::run(args, program_config.unwrap()) {
|
||||||
panic!("Error: {}", e)
|
panic!("Error: {}", e)
|
||||||
|
|||||||
@@ -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<ConfigFile>,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
impl ProgramConfig {
|
|
||||||
pub fn parse(config: PathBuf) -> Result<Self, Box<dyn Error>> {
|
|
||||||
|
|
||||||
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<ConfigFile> = 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})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user