feat(azure): subscription name aliases (#4949)

* From issue #4448, added `subscription_aliases`
as a field for the Azure module

Can be set in starship.toml with
[azure.subscription_aliases]

* Updated config file schema

* Added entry into documentation

* Update README.md

* Formatted with dprint
This commit is contained in:
marcybell 2023-04-14 03:03:14 +08:00 committed by GitHub
parent edb96cad58
commit 27ffa37cfd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 107 additions and 7 deletions

View File

@ -29,6 +29,7 @@
"disabled": true,
"format": "on [$symbol($subscription)]($style) ",
"style": "blue bold",
"subscription_aliases": {},
"symbol": "ﴃ "
},
"allOf": [
@ -1849,6 +1850,13 @@
"disabled": {
"default": true,
"type": "boolean"
},
"subscription_aliases": {
"default": {},
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"additionalProperties": false

View File

@ -448,12 +448,13 @@ The `azure` module shows the current Azure Subscription. This is based on showin
### Options
| Variable | Default | Description |
| ---------- | ---------------------------------------- | ------------------------------------------ |
| `format` | `'on [$symbol($subscription)]($style) '` | The format for the Azure module to render. |
| `symbol` | `'ﴃ '` | The symbol used in the format. |
| `style` | `'blue bold'` | The style used in the format. |
| `disabled` | `true` | Disables the `azure` module. |
| Variable | Default | Description |
| ---------------------- | ---------------------------------------- | ------------------------------------------------------------------------------------- |
| `format` | `'on [$symbol($subscription)]($style) '` | The format for the Azure module to render. |
| `symbol` | `'ﴃ '` | The symbol used in the format. |
| `style` | `'blue bold'` | The style used in the format. |
| `disabled` | `true` | Disables the `azure` module. |
| `subscription_aliases` | `{}` | Table of subscription name aliases to display in addition to Azure subscription name. |
### Examples
@ -481,6 +482,15 @@ symbol = "ﴃ "
style = "blue bold"
```
#### Display Subscription Name Alias
```toml
# ~/.config/starship.toml
[azure.subscription_aliases]
very-long-subscription-name = 'vlsn'
```
## Battery
The `battery` module shows how charged the device's battery is and its current charging status.

View File

@ -1,4 +1,5 @@
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Clone, Deserialize, Serialize)]
#[cfg_attr(
@ -12,6 +13,7 @@ pub struct AzureConfig<'a> {
pub symbol: &'a str,
pub style: &'a str,
pub disabled: bool,
pub subscription_aliases: HashMap<String, &'a str>,
}
impl<'a> Default for AzureConfig<'a> {
@ -21,6 +23,7 @@ impl<'a> Default for AzureConfig<'a> {
symbol: "",
style: "blue bold",
disabled: true,
subscription_aliases: HashMap::new(),
}
}
}

View File

@ -56,7 +56,11 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
_ => None,
})
.map(|variable| match variable {
"subscription" => Some(Ok(&subscription.name)),
"subscription" => Some(Ok(config
.subscription_aliases
.get(&subscription.name)
.copied()
.unwrap_or(&subscription.name))),
"username" => Some(Ok(&subscription.user.name)),
_ => None,
})
@ -616,6 +620,81 @@ mod tests {
dir.close()
}
#[test]
fn subscription_name_with_alias() -> io::Result<()> {
let dir = tempfile::tempdir()?;
let azure_profile_contents = r#"{
"installationId": "3deacd2a-b9db-77e1-aa42-23e2f8dfffc3",
"subscriptions": [
{
"id": "f568c543-d12e-de0b-3d85-69843598b565",
"name": "VeryLongSubscriptionName",
"state": "Enabled",
"user": {
"name": "user@domain.com",
"type": "user"
},
"isDefault": false,
"tenantId": "0e8a15ec-b0f5-d355-7062-8ece54c59aee",
"environmentName": "AzureCloud",
"homeTenantId": "0e8a15ec-b0f5-d355-7062-8ece54c59aee",
"managedByTenants": []
},
{
"id": "d4442d26-ea6d-46c4-07cb-4f70b8ae5465",
"name": "AnotherLongSubscriptionName",
"state": "Enabled",
"user": {
"name": "user@domain.com",
"type": "user"
},
"isDefault": false,
"tenantId": "a4e1bb4b-5330-2d50-339d-b9674d3a87bc",
"environmentName": "AzureCloud",
"homeTenantId": "a4e1bb4b-5330-2d50-339d-b9674d3a87bc",
"managedByTenants": []
},
{
"id": "f3935dc9-92b5-9a93-da7b-42c325d86939",
"name": "TheLastLongSubscriptionName",
"state": "Enabled",
"user": {
"name": "user@domain.com",
"type": "user"
},
"isDefault": true,
"tenantId": "f0273a19-7779-e40a-00a1-53b8331b3bb6",
"environmentName": "AzureCloud",
"homeTenantId": "f0273a19-7779-e40a-00a1-53b8331b3bb6",
"managedByTenants": []
}
]
}
"#;
generate_test_config(&dir, azure_profile_contents)?;
let dir_path = &dir.path().to_string_lossy();
let actual = ModuleRenderer::new("azure")
.config(toml::toml! {
[azure]
format = "on [$symbol($subscription:$username)]($style)"
disabled = false
[azure.subscription_aliases]
VeryLongSubscriptionName = "vlsn"
AnotherLongSubscriptionName = "alsn"
TheLastLongSubscriptionName = "tllsn"
})
.env("AZURE_CONFIG_DIR", dir_path.as_ref())
.collect();
let expected = Some(format!(
"on {}",
Color::Blue.bold().paint("ﴃ tllsn:user@domain.com")
));
assert_eq!(actual, expected);
dir.close()
}
#[test]
fn subscription_azure_profile_empty() -> io::Result<()> {
let dir = tempfile::tempdir()?;