icann_rdap_cli/dirs/
project.rs

1use std::{
2    fs::{create_dir_all, remove_dir_all, write},
3    path::PathBuf,
4};
5
6use directories::ProjectDirs;
7use lazy_static::lazy_static;
8
9pub const QUALIFIER: &str = "org";
10pub const ORGANIZATION: &str = "ICANN";
11pub const APPLICATION: &str = "rdap";
12
13pub const ENV_FILE_NAME: &str = "rdap.env";
14pub const RDAP_CACHE_NAME: &str = "rdap_cache";
15pub const BOOTSTRAP_CACHE_NAME: &str = "bootstrap_cache";
16
17lazy_static! {
18    pub(crate) static ref PROJECT_DIRS: ProjectDirs =
19        ProjectDirs::from(QUALIFIER, ORGANIZATION, APPLICATION)
20            .expect("unable to formulate project directories");
21}
22
23/// Initializes the directories to be used.
24pub fn init() -> Result<(), std::io::Error> {
25    create_dir_all(PROJECT_DIRS.config_dir())?;
26    create_dir_all(PROJECT_DIRS.cache_dir())?;
27    create_dir_all(rdap_cache_path())?;
28    create_dir_all(bootstrap_cache_path())?;
29
30    // create default config file
31    if !config_path().exists() {
32        let example_config = include_str!("rdap.env");
33        write(config_path(), example_config)?;
34    }
35    Ok(())
36}
37
38/// Reset the directories.
39pub fn reset() -> Result<(), std::io::Error> {
40    remove_dir_all(PROJECT_DIRS.config_dir())?;
41    remove_dir_all(PROJECT_DIRS.cache_dir())?;
42    init()
43}
44
45/// Returns a [PathBuf] to the configuration file.
46pub fn config_path() -> PathBuf {
47    PROJECT_DIRS.config_dir().join(ENV_FILE_NAME)
48}
49
50/// Returns a [PathBuf] to the cache directory for RDAP responses.
51pub fn rdap_cache_path() -> PathBuf {
52    PROJECT_DIRS.cache_dir().join(RDAP_CACHE_NAME)
53}
54
55/// Returns a [PathBuf] to the cache directory for bootstrap files.
56pub fn bootstrap_cache_path() -> PathBuf {
57    PROJECT_DIRS.cache_dir().join(BOOTSTRAP_CACHE_NAME)
58}