diff options
author | Kotaro Abe <abekoh.bcky@gmail.com> | 2022-02-14 21:16:45 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-14 13:16:45 +0100 |
commit | 32aca11c4a9dab0d6d37c7659c3fdb7dbb3ae983 (patch) | |
tree | 4512835e93dcbe414d0564b369ad736ac0b981a3 /src | |
parent | 0ea16e2641b39450071b1f22efeed526e9420932 (diff) |
Add option gcloud.project_aliases (#3599)
* feat: gcloud.project_aliases
* feat: add option gcloud.project_aliases
Diffstat (limited to 'src')
-rw-r--r-- | src/configs/gcloud.rs | 2 | ||||
-rw-r--r-- | src/modules/gcloud.rs | 38 |
2 files changed, 40 insertions, 0 deletions
diff --git a/src/configs/gcloud.rs b/src/configs/gcloud.rs index 50aa04a68..853cd0148 100644 --- a/src/configs/gcloud.rs +++ b/src/configs/gcloud.rs @@ -10,6 +10,7 @@ pub struct GcloudConfig<'a> { pub style: &'a str, pub disabled: bool, pub region_aliases: HashMap<String, &'a str>, + pub project_aliases: HashMap<String, &'a str>, } impl<'a> Default for GcloudConfig<'a> { @@ -20,6 +21,7 @@ impl<'a> Default for GcloudConfig<'a> { style: "bold blue", disabled: false, region_aliases: HashMap::new(), + project_aliases: HashMap::new(), } } } diff --git a/src/modules/gcloud.rs b/src/modules/gcloud.rs index 9e3a0fd7e..153aa5ccb 100644 --- a/src/modules/gcloud.rs +++ b/src/modules/gcloud.rs @@ -147,6 +147,12 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { "project" => context .get_env("CLOUDSDK_CORE_PROJECT") .or_else(|| gcloud_context.get_project()) + .map(|project| { + config + .project_aliases + .get(&project) + .map_or(project, |alias| (*alias).to_owned()) + }) .map(Ok), "active" => Some(Ok(gcloud_context.config_name.clone())), _ => None, @@ -386,6 +392,38 @@ project = abc } #[test] + fn project_set_with_alias() -> 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")?; + + create_dir(dir.path().join("configurations"))?; + let config_default_path = dir.path().join("configurations").join("config_default"); + let mut config_default_file = File::create(&config_default_path)?; + config_default_file.write_all( + b"\ +[core] +project = very-long-project-name +", + )?; + + let actual = ModuleRenderer::new("gcloud") + .env("CLOUDSDK_CONFIG", dir.path().to_string_lossy()) + .config(toml::toml! { + [gcloud] + format = "on [$symbol$project]($style) " + [gcloud.project_aliases] + very-long-project-name = "vlpn" + }) + .collect(); + let expected = Some(format!("on {} ", Color::Blue.bold().paint("☁️ vlpn"))); + + assert_eq!(actual, expected); + dir.close() + } + + #[test] fn region_not_set_with_display_region() -> io::Result<()> { let dir = tempfile::tempdir()?; let actual = ModuleRenderer::new("gcloud") |