From 6536f3aae95e266f80a5a73288e181ff10ce650b Mon Sep 17 00:00:00 2001 From: ottjk Date: Sat, 30 Dec 2023 19:40:27 -0500 Subject: initial commit --- src/main.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/main.rs (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..59f3a3c --- /dev/null +++ b/src/main.rs @@ -0,0 +1,60 @@ +use std::{ + fs, + io::Result, + collections::HashMap, + path::PathBuf, +}; +use serde::{Deserialize,Serialize}; +use clap::Parser; + +mod commands; +use commands::*; + +mod arguments; +use arguments::*; + +#[derive(Deserialize, Serialize)] +pub struct Entry { + file: PathBuf, + parent: PathBuf, +} + +pub type Config = HashMap; + +pub fn resolve_home(path: &mut PathBuf) { + if path.starts_with("~") { + let temp = path.strip_prefix("~").unwrap(); + *path = dirs::home_dir().expect("Could not resolve home directory.") + .join(temp); + } +} + +fn read_config(path: &PathBuf) -> Config { + let file = fs::read_to_string(path).unwrap_or(String::new()); + toml::from_str(&file).unwrap() +} + +fn main() -> Result<()> { + let cli = Cli::parse(); + + let mut configs_path = cli.config + .unwrap_or(dirs::config_dir() + .expect("Could not resolve config directory.") + .join("dotfiles")); + resolve_home(&mut configs_path); + + if !configs_path.try_exists().unwrap() { + fs::create_dir_all(&configs_path).unwrap(); + } + + let mut config = read_config(&configs_path.join("configs.toml")); + + match cli.command { + Commands::Modify(args) => modify_command(&mut config, args, &configs_path)?, + Commands::Remove(args) => remove_command(&mut config, args, &configs_path)?, + Commands::Sync(args) => sync_command(&config, args, &configs_path), + Commands::List {} => list_command(&config), + }; + + Ok(()) +} -- cgit v1.3