summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDenis Cornehl <denis@cornehl.org>2023-05-09 07:55:49 +0200
committerGitHub <noreply@github.com>2023-05-09 07:55:49 +0200
commitd07a8e3668838223aeeb94e810a0b29806e35f78 (patch)
tree1b2b0adc69eb7d87d02fe06343eb8f9fb71635e7
parent297176b0b8b9da34176d7b278837f77f960799b1 (diff)
feat(gcloud): add `detect_env_vars` option (#5166)
* feat(gcloud): add `detect_env_vars` option * regenerate config schema
-rw-r--r--.github/config-schema.json8
-rw-r--r--docs/config/README.md5
-rw-r--r--src/configs/gcloud.rs2
-rw-r--r--src/context.rs4
-rw-r--r--src/modules/gcloud.rs53
5 files changed, 72 insertions, 0 deletions
diff --git a/.github/config-schema.json b/.github/config-schema.json
index d7e18cf72..00e82b991 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 510c51d5a..33bc1ee22 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 bca0bb737..2969346af 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<String, &'a str>,
pub project_aliases: HashMap<String, &'a str>,
+ 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 a825dbaf2..54a2310a4 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<ScanDir<'a>> {
diff --git a/src/modules/gcloud.rs b/src/modules/gcloud.rs
index 786f51294..692504c34 100644
--- a/src/modules/gcloud.rs
+++ b/src/modules/gcloud.rs
@@ -84,6 +84,10 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
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<Option<Account<'_>>, _> = Lazy::new(|| gcloud_context.get_account());
@@ -151,6 +155,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()?;
let active_config_path = dir.path().join("active_config");