dialtone_ccli_util 0.1.0

Dialtone Client CLI Utilities
Documentation
use std::fs;
use std::path::PathBuf;

use directories::ProjectDirs;
use lazy_static::lazy_static;

use super::{DialtoneConfig, DIALTONE_CONFIG_FILE};

pub const QUALIFIER: &str = "dev";
pub const ORGANIZATION: &str = "dialtone";
pub const APPLICATION: &str = "dialtone";

const USERS: &str = "users";
const ACTORS: &str = "actors";

lazy_static! {
    pub static ref PROJECT_DIRS: ProjectDirs =
        ProjectDirs::from(QUALIFIER, ORGANIZATION, APPLICATION).unwrap();
    pub static ref USERS_PATH: PathBuf = {
        let mut path = PathBuf::new();
        path.push(PROJECT_DIRS.data_dir());
        path.push(USERS);
        path
    };
    pub static ref ACTORS_PATH: PathBuf = {
        let mut path = PathBuf::new();
        path.push(PROJECT_DIRS.data_dir());
        path.push(ACTORS);
        path
    };
}

pub fn init(default_config_files: &[(&str, &str)]) -> std::io::Result<()> {
    fs::create_dir_all(PROJECT_DIRS.config_dir())?;
    fs::create_dir_all(PROJECT_DIRS.data_dir())?;
    fs::create_dir_all(USERS_PATH.as_path())?;
    fs::create_dir_all(ACTORS_PATH.as_path())?;
    for config_set in default_config_files {
        let path = config_path(config_set.0);
        fs::write(path, config_set.1).unwrap();
    }
    fs::write(
        config_path(DIALTONE_CONFIG_FILE),
        DialtoneConfig::default().to_toml_string(),
    )
    .unwrap();
    Ok(())
}

pub fn config_path(config_file: &str) -> PathBuf {
    let mut path = PathBuf::new();
    path.push(PROJECT_DIRS.config_dir());
    path.push(config_file);
    path
}