From d07a8e3668838223aeeb94e810a0b29806e35f78 Mon Sep 17 00:00:00 2001 From: Denis Cornehl Date: Tue, 9 May 2023 07:55:49 +0200 Subject: [PATCH] feat(gcloud): add `detect_env_vars` option (#5166) * feat(gcloud): add `detect_env_vars` option * regenerate config schema --- .github/config-schema.json | 8 ++++++ docs/config/README.md | 5 ++++ src/configs/gcloud.rs | 2 ++ src/context.rs | 4 +++ src/modules/gcloud.rs | 53 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 72 insertions(+) diff --git a/.github/config-schema.json b/.github/config-schema.json index d7e18cf7..00e82b99 100644 --- a/.github/config-schema.json +++ b/.github/config-schema.json @@ -522,6 +522,7 @@ }, "gcloud": { "default": { + "detect_env_vars": [], "disabled": false, "format": "on [$symbol$account(@$domain)(\\($region\\))]($style) ", "project_aliases": {}, @@ -3097,6 +3098,13 @@ "additionalProperties": { "type": "string" } + }, + "detect_env_vars": { + "default": [], + "type": "array", + "items": { + "type": "string" + } } }, "additionalProperties": false diff --git a/docs/config/README.md b/docs/config/README.md index 510c51d5..33bc1ee2 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -1609,6 +1609,10 @@ truncation_symbol = '' The `gcloud` module shows the current configuration for [`gcloud`](https://cloud.google.com/sdk/gcloud) CLI. This is based on the `~/.config/gcloud/active_config` file and the `~/.config/gcloud/configurations/config_{CONFIG NAME}` file and the `CLOUDSDK_CONFIG` env var. +When the module is enabled it will always be active, unless `detect_env_vars` has +been set in which case the module will only be active be active when one of the +environment variables has been set. + ### Options | Option | Default | Description | @@ -1617,6 +1621,7 @@ This is based on the `~/.config/gcloud/active_config` file and the `~/.config/gc | `symbol` | `'☁️ '` | The symbol used before displaying the current GCP profile. | | `region_aliases` | `{}` | Table of region aliases to display in addition to the GCP name. | | `project_aliases` | `{}` | Table of project aliases to display in addition to the GCP name. | +| `detect_env_vars` | `[]` | Which environmental variables should trigger this module | | `style` | `'bold blue'` | The style for the module. | | `disabled` | `false` | Disables the `gcloud` module. | diff --git a/src/configs/gcloud.rs b/src/configs/gcloud.rs index bca0bb73..2969346a 100644 --- a/src/configs/gcloud.rs +++ b/src/configs/gcloud.rs @@ -15,6 +15,7 @@ pub struct GcloudConfig<'a> { pub disabled: bool, pub region_aliases: HashMap, pub project_aliases: HashMap, + pub detect_env_vars: Vec<&'a str>, } impl<'a> Default for GcloudConfig<'a> { @@ -26,6 +27,7 @@ impl<'a> Default for GcloudConfig<'a> { disabled: false, region_aliases: HashMap::new(), project_aliases: HashMap::new(), + detect_env_vars: vec![], } } } diff --git a/src/context.rs b/src/context.rs index a825dbaf..54a2310a 100644 --- a/src/context.rs +++ b/src/context.rs @@ -235,6 +235,10 @@ impl<'a> Context<'a> { disabled == Some(true) } + pub fn detect_env_vars(&'a self, env_vars: &'a [&'a str]) -> bool { + env_vars.is_empty() || (env_vars.iter().any(|e| self.get_env(e).is_some())) + } + // returns a new ScanDir struct with reference to current dir_files of context // see ScanDir for methods pub fn try_begin_scan(&'a self) -> Option> { diff --git a/src/modules/gcloud.rs b/src/modules/gcloud.rs index 786f5129..692504c3 100644 --- a/src/modules/gcloud.rs +++ b/src/modules/gcloud.rs @@ -84,6 +84,10 @@ pub fn module<'a>(context: &'a Context) -> Option> { let mut module = context.new_module("gcloud"); let config: GcloudConfig = GcloudConfig::try_load(module.config); + if !(context.detect_env_vars(&config.detect_env_vars)) { + return None; + } + let (config_name, config_path) = get_current_config(context)?; let gcloud_context = GcloudContext::new(&config_name, &config_path); let account: Lazy>, _> = Lazy::new(|| gcloud_context.get_account()); @@ -150,6 +154,55 @@ mod tests { use crate::test::ModuleRenderer; + #[test] + fn account_set_but_not_shown_because_of_detect_env_vars() -> io::Result<()> { + let dir = tempfile::tempdir()?; + let active_config_path = dir.path().join("active_config"); + let mut active_config_file = File::create(active_config_path)?; + active_config_file.write_all(b"default")?; + + // check if this config would lead to the module being rendered + assert_eq!( + ModuleRenderer::new("gcloud") + .env("CLOUDSDK_CONFIG", dir.path().to_string_lossy()) + .config(toml::toml! { + [gcloud] + format = "$active" + }) + .collect(), + Some("default".into()) + ); + + // when we set `detect_env_vars` now, the module is empty + assert_eq!( + ModuleRenderer::new("gcloud") + .env("CLOUDSDK_CONFIG", dir.path().to_string_lossy()) + .config(toml::toml! { + [gcloud] + format = "$active" + detect_env_vars = ["SOME_TEST_VAR"] + }) + .collect(), + None + ); + + // and when the environment variable has a value, the module is shown + assert_eq!( + ModuleRenderer::new("gcloud") + .env("CLOUDSDK_CONFIG", dir.path().to_string_lossy()) + .env("SOME_TEST_VAR", "1") + .config(toml::toml! { + [gcloud] + format = "$active" + detect_env_vars = ["SOME_TEST_VAR"] + }) + .collect(), + Some("default".into()) + ); + + dir.close() + } + #[test] fn account_set() -> io::Result<()> { let dir = tempfile::tempdir()?;