From 32aca11c4a9dab0d6d37c7659c3fdb7dbb3ae983 Mon Sep 17 00:00:00 2001 From: Kotaro Abe Date: Mon, 14 Feb 2022 21:16:45 +0900 Subject: [PATCH] Add option gcloud.project_aliases (#3599) * feat: gcloud.project_aliases * feat: add option gcloud.project_aliases --- docs/config/README.md | 26 +++++++++++++++++++------- src/configs/gcloud.rs | 2 ++ src/modules/gcloud.rs | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 7 deletions(-) diff --git a/docs/config/README.md b/docs/config/README.md index bfba86a0..f1de7c0b 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -1224,13 +1224,14 @@ This is based on the `~/.config/gcloud/active_config` file and the `~/.config/gc ### Options -| Option | Default | Description | -| ---------------- | -------------------------------------------------------- | --------------------------------------------------------------- | -| `format` | `'on [$symbol$account(@$domain)(\($region\))]($style) '` | The format for the module. | -| `symbol` | `"☁️ "` | The symbol used before displaying the current GCP profile. | -| `region_aliases` | | Table of region aliases to display in addition to the GCP name. | -| `style` | `"bold blue"` | The style for the module. | -| `disabled` | `false` | Disables the `gcloud` module. | +| Option | Default | Description | +| ----------------- | -------------------------------------------------------- | ---------------------------------------------------------------- | +| `format` | `'on [$symbol$account(@$domain)(\($region\))]($style) '` | The format for the module. | +| `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. | +| `style` | `"bold blue"` | The style for the module. | +| `disabled` | `false` | Disables the `gcloud` module. | ### Variables @@ -1279,6 +1280,17 @@ us-central1 = "uc1" asia-northeast1 = "an1" ``` +#### Display account and aliased project + +```toml +# ~/.config/starship.toml + +[gcloud] +format = 'on [$symbol$account(@$domain)(\($project\))]($style) ' +[gcloud.project_aliases] +very-long-project-name = "vlpn" +``` + ## Git Branch The `git_branch` module shows the active branch of the repo in your current directory. diff --git a/src/configs/gcloud.rs b/src/configs/gcloud.rs index 50aa04a6..853cd014 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, + pub project_aliases: HashMap, } 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 9e3a0fd7..153aa5cc 100644 --- a/src/modules/gcloud.rs +++ b/src/modules/gcloud.rs @@ -147,6 +147,12 @@ pub fn module<'a>(context: &'a Context) -> Option> { "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, @@ -385,6 +391,38 @@ project = abc dir.close() } + #[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()?;