back 0.1.1

A CLI tool to manage and run your backups.
use std::path::PathBuf;

pub struct Config {
    pub files: PathBuf,
    pub cache: PathBuf,
    pub remotes: PathBuf,
}

impl Config {
    pub fn new(prefix: &str) -> Config {
        let xdg_dirs = xdg::BaseDirectories::with_prefix(prefix).unwrap();

        let files = xdg_dirs
            .place_config_file("files")
            .expect("cannot create configuration directory");

        let remotes = xdg_dirs
            .place_config_file("remotes")
            .expect("cannot create configuration directory");

        let cache = xdg_dirs
            .place_cache_file("archive.tar.gz")
            .expect("cannot create cache directory");

        create_file(files.to_owned()).ok();
        create_file(remotes.to_owned()).ok();
        create_file(cache.to_owned()).ok();

        Config {
            files,
            remotes,
            cache,
        }
    }
}

fn create_file(path: PathBuf) -> Result<(), ()> {
    let directory = path.parent();

    match directory {
        Some(d) => {
            if !d.is_dir() {
                std::fs::create_dir_all(d).unwrap();
            }
        }
        None => return Err(()),
    };

    // Create files file if it doesn't exist
    if !path.is_file() {
        std::fs::File::create(path).unwrap();
    }

    Ok(())
}