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
| 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.

View File

@ -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(),
}
}
}

View File

@ -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,
@ -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()?;