icann_rdap_cli/dirs/
project.rs

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
use std::{
    fs::{create_dir_all, remove_dir_all, write},
    path::PathBuf,
};

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

pub const QUALIFIER: &str = "org";
pub const ORGANIZATION: &str = "ICANN";
pub const APPLICATION: &str = "rdap";

pub const ENV_FILE_NAME: &str = "rdap.env";
pub const RDAP_CACHE_NAME: &str = "rdap_cache";
pub const BOOTSTRAP_CACHE_NAME: &str = "bootstrap_cache";

lazy_static! {
    pub(crate) static ref PROJECT_DIRS: ProjectDirs =
        ProjectDirs::from(QUALIFIER, ORGANIZATION, APPLICATION)
            .expect("unable to formulate project directories");
}

/// Initializes the directories to be used.
pub fn init() -> Result<(), std::io::Error> {
    create_dir_all(PROJECT_DIRS.config_dir())?;
    create_dir_all(PROJECT_DIRS.cache_dir())?;
    create_dir_all(rdap_cache_path())?;
    create_dir_all(bootstrap_cache_path())?;

    // create default config file
    if !config_path().exists() {
        let example_config = include_str!("rdap.env");
        write(config_path(), example_config)?;
    }
    Ok(())
}

/// Reset the directories.
pub fn reset() -> Result<(), std::io::Error> {
    remove_dir_all(PROJECT_DIRS.config_dir())?;
    remove_dir_all(PROJECT_DIRS.cache_dir())?;
    init()
}

/// Returns a [PathBuf] to the configuration file.
pub fn config_path() -> PathBuf {
    PROJECT_DIRS.config_dir().join(ENV_FILE_NAME)
}

/// Returns a [PathBuf] to the cache directory for RDAP responses.
pub fn rdap_cache_path() -> PathBuf {
    PROJECT_DIRS.cache_dir().join(RDAP_CACHE_NAME)
}

/// Returns a [PathBuf] to the cache directory for bootstrap files.
pub fn bootstrap_cache_path() -> PathBuf {
    PROJECT_DIRS.cache_dir().join(BOOTSTRAP_CACHE_NAME)
}