Skip to main content

envmgr/config/
environment.rs

1use std::path::Path;
2
3use config::Config;
4
5use super::envmgr_config_dir;
6use crate::error::EnvMgrResult;
7
8#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
9pub struct EnvironmentConfig {
10    pub name: String,
11    #[serde(default)]
12    pub env_vars: Vec<EnvVarsConfig>,
13    pub op_ssh: Option<crate::integrations::one_password_ssh_agent::OnePasswordSSHAgentConfig>,
14    pub gh_cli: Option<crate::integrations::gh_cli::GhCliConfig>,
15    pub tailscale: Option<crate::integrations::tailscale::TailscaleConfig>,
16}
17
18const ENVS_DIR_NAME: &str = "environments";
19const ENV_CONFIG_FILE_NAME: &str = "config.yaml";
20pub const BASE_ENV_NAME: &str = "base";
21
22impl EnvironmentConfig {
23    /// Get the directory path for the base environment
24    /// e.g., ~/.config/envmgr/base
25    pub fn get_base_env_dir() -> std::path::PathBuf {
26        envmgr_config_dir().join(BASE_ENV_NAME)
27    }
28    /// Get the directory path for a specific environment by its key
29    /// e.g., ~/.config/envmgr/environments/<key>
30    pub fn get_env_dir_by_key(key: &str) -> std::path::PathBuf {
31        Self::get_all_envs_dir().join(key)
32    }
33    /// Get the directory path where all environments are stored
34    /// e.g., ~/.config/envmgr/environments
35    pub fn get_all_envs_dir() -> std::path::PathBuf {
36        envmgr_config_dir().join(ENVS_DIR_NAME)
37    }
38
39    fn load_from_file(config_dir: &Path) -> EnvMgrResult<Self> {
40        let config: Self = Config::builder()
41            .add_source(config::File::from(config_dir.join(ENV_CONFIG_FILE_NAME)))
42            .build()?
43            .try_deserialize()?;
44        Ok(config)
45    }
46
47    pub fn load_base_config() -> EnvMgrResult<Self> {
48        let base_env_path = Self::get_base_env_dir();
49        Self::load_from_file(&base_env_path)
50    }
51
52    pub fn load_env_config_by_key(key: &str) -> EnvMgrResult<Self> {
53        let env_path = Self::get_env_dir_by_key(key);
54        Self::load_from_file(&env_path)
55    }
56}
57
58#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
59pub struct EnvVarsConfig {
60    pub key: String,
61    pub value: String,
62}