aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 8fc22c0dcc7f08ab1c5843c28ce70dd8b84c6e4b (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
use std::env;
use std::os::unix::fs::symlink;
use std::path::PathBuf;
use std::process::exit;
use dirs::{home_dir};
use crate::config::{Config, ConfigLoadError, Dot};

mod config;

fn main() {
    let args = env::args().skip(1).collect::<Vec<String>>();

    if args.len() > 1 {
        eprintln!("usage: {} [config]", args[0]);
        exit(1);
    }

    if args.len() == 1 && (args[0] == "-h" || args[0] == "--help") {
        println!("Usage: {} [config]", args[0]);
        println!("options:");
        println!("  -h, --help        Print help information");
        println!("By default, dots will look for a bummsdots.toml file in the current directory");
        println!("This can be changed by passing the filename");
        exit(0);
    }

    let config = Config::load(args.get(0).map(|o| PathBuf::from(o))).map_err(|err|
        match err {
            ConfigLoadError::IOError(err) => {
                eprintln!("failed to load config file: {}", err);
                exit(1)
            }
            ConfigLoadError::DeserializationError(err) => {
                eprintln!("failed to deserialize config file: {}", err);
                exit(1)
            }
        }
    ).unwrap();

    let prepended_mappings = config.dots.iter().map(|m|
        Dot {
            source: config.dots_dir.join(&m.source),
            destination: prepend_user_dir(&m.destination)
        }
    );

    for dot in prepended_mappings {
        println!("linking from {} to {}", dot.source.display(), dot.destination.display());
        let _ = symlink(&dot.source, &dot.destination).map_err(|err|
            eprintln!("failed to symlink: {}", err.to_string())
        );
    }
}

fn prepend_user_dir(path: &PathBuf) -> PathBuf {
    home_dir().unwrap().join(path)
}