mirror of
https://github.com/Llewellynvdm/starship.git
synced 2024-12-25 11:20:01 +00:00
feat(gcloud): add detect_env_vars
option (#5166)
* feat(gcloud): add `detect_env_vars` option * regenerate config schema
This commit is contained in:
parent
297176b0b8
commit
d07a8e3668
8
.github/config-schema.json
vendored
8
.github/config-schema.json
vendored
@ -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
|
||||
|
@ -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. |
|
||||
|
||||
|
@ -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![],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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>> {
|
||||
|
@ -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());
|
||||
@ -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()?;
|
||||
|
Loading…
Reference in New Issue
Block a user