aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 59f3a3c954e7f784cecb1dc4fcfc39cb12a9f7db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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(())
}