diff options
| author | ottjk <joshott16@gmail.com> | 2023-12-30 19:40:27 -0500 |
|---|---|---|
| committer | ottjk <joshott16@gmail.com> | 2023-12-30 19:40:27 -0500 |
| commit | 6536f3aae95e266f80a5a73288e181ff10ce650b (patch) | |
| tree | 2fa309c246ab4ad860da8673a82dcb4ead924919 /src/main.rs | |
| download | dfa-6536f3aae95e266f80a5a73288e181ff10ce650b.tar.gz dfa-6536f3aae95e266f80a5a73288e181ff10ce650b.zip | |
initial commit
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 60 |
1 files changed, 60 insertions, 0 deletions
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<String, Entry>; + +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(()) +} |