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(()),
};
if !path.is_file() {
std::fs::File::create(path).unwrap();
}
Ok(())
}