onion_vault_cli/config/
default.rs

1use crate::{
2    common::constants::{
3    APPLICATION_NAME,
4    ORGANIZATION,
5    },
6    re_export::std_anyhow::*,
7};
8
9
10use lazy_static::lazy_static;
11use directories::ProjectDirs;
12
13
14use std::path::PathBuf;
15use std::env;
16
17
18
19lazy_static! {
20    // pub static ref CLI_ARGS: RwLock<Cli> = RwLock::new(Cli::parse());
21    // pub static ref ProjectDir: Option<ProjectDirs> = ProjectDirs::from("onion", ORGANIZATION, APPLICATION_NAME);
22    pub static ref PROJECTDIR: ProjectDir = ProjectDirBuilder::default()
23        .build()
24        .unwrap();
25}
26
27
28
29// #[derive(Debug, Serialize, Deserialize, Clone, Builder)]
30#[derive(Debug, Builder)]
31pub struct ProjectDir {
32    #[builder(default = "ProjectDirs::from(\"onion\", ORGANIZATION, APPLICATION_NAME)")]
33    project_dirs: Option<ProjectDirs>
34}
35
36impl ProjectDir {
37    pub fn pwd(&self) -> PathBuf {
38        env::current_dir().unwrap()
39    }
40
41    pub fn data_dir(&self) -> String {
42        let pwd = self.pwd();
43        let data_dir = match &self.project_dirs {
44            Some(project_dirs) => {
45                project_dirs.data_dir()
46            },
47            None => &pwd.as_path(),
48        };
49
50        data_dir.to_owned().to_string_lossy().into_owned()
51    }
52
53    pub fn config_file(&self) -> String {
54        let pwd = self.pwd();
55        let config_dir = match &self.project_dirs {
56            Some(project_dirs) => {
57                project_dirs.config_dir()
58            },
59            None => &pwd.as_path(),
60        };
61
62        config_dir.join("config.json").to_string_lossy().into_owned()
63    }
64}