Add option gcloud.project_aliases (#3599)

* feat: gcloud.project_aliases

* feat: add option gcloud.project_aliases
This commit is contained in:
Kotaro Abe 2022-02-14 21:16:45 +09:00 committed by GitHub
parent 0ea16e2641
commit 32aca11c4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 7 deletions

View File

@ -1224,13 +1224,14 @@ This is based on the `~/.config/gcloud/active_config` file and the `~/.config/gc
### Options ### Options
| Option | Default | Description | | Option | Default | Description |
| ---------------- | -------------------------------------------------------- | --------------------------------------------------------------- | | ----------------- | -------------------------------------------------------- | ---------------------------------------------------------------- |
| `format` | `'on [$symbol$account(@$domain)(\($region\))]($style) '` | The format for the module. | | `format` | `'on [$symbol$account(@$domain)(\($region\))]($style) '` | The format for the module. |
| `symbol` | `"☁️ "` | The symbol used before displaying the current GCP profile. | | `symbol` | `"☁️ "` | The symbol used before displaying the current GCP profile. |
| `region_aliases` | | Table of region aliases to display in addition to the GCP name. | | `region_aliases` | | Table of region aliases to display in addition to the GCP name. |
| `style` | `"bold blue"` | The style for the module. | | `project_aliases` | | Table of project aliases to display in addition to the GCP name. |
| `disabled` | `false` | Disables the `gcloud` module. | | `style` | `"bold blue"` | The style for the module. |
| `disabled` | `false` | Disables the `gcloud` module. |
### Variables ### Variables
@ -1279,6 +1280,17 @@ us-central1 = "uc1"
asia-northeast1 = "an1" 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 ## Git Branch
The `git_branch` module shows the active branch of the repo in your current directory. The `git_branch` module shows the active branch of the repo in your current directory.

View File

@ -10,6 +10,7 @@ pub struct GcloudConfig<'a> {
pub style: &'a str, pub style: &'a str,
pub disabled: bool, pub disabled: bool,
pub region_aliases: HashMap<String, &'a str>, pub region_aliases: HashMap<String, &'a str>,
pub project_aliases: HashMap<String, &'a str>,
} }
impl<'a> Default for GcloudConfig<'a> { impl<'a> Default for GcloudConfig<'a> {
@ -20,6 +21,7 @@ impl<'a> Default for GcloudConfig<'a> {
style: "bold blue", style: "bold blue",
disabled: false, disabled: false,
region_aliases: HashMap::new(), region_aliases: HashMap::new(),
project_aliases: HashMap::new(),
} }
} }
} }

View File

@ -147,6 +147,12 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
"project" => context "project" => context
.get_env("CLOUDSDK_CORE_PROJECT") .get_env("CLOUDSDK_CORE_PROJECT")
.or_else(|| gcloud_context.get_project()) .or_else(|| gcloud_context.get_project())
.map(|project| {
config
.project_aliases
.get(&project)
.map_or(project, |alias| (*alias).to_owned())
})
.map(Ok), .map(Ok),
"active" => Some(Ok(gcloud_context.config_name.clone())), "active" => Some(Ok(gcloud_context.config_name.clone())),
_ => None, _ => None,
@ -385,6 +391,38 @@ project = abc
dir.close() 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] #[test]
fn region_not_set_with_display_region() -> io::Result<()> { fn region_not_set_with_display_region() -> io::Result<()> {
let dir = tempfile::tempdir()?; let dir = tempfile::tempdir()?;