From 27ffa37cfdf2eff9874e543f88fa389bf5c2dae3 Mon Sep 17 00:00:00 2001 From: marcybell Date: Fri, 14 Apr 2023 03:03:14 +0800 Subject: [PATCH] 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 --- .github/config-schema.json | 8 ++++ docs/config/README.md | 22 ++++++++--- src/configs/azure.rs | 3 ++ src/modules/azure.rs | 81 +++++++++++++++++++++++++++++++++++++- 4 files changed, 107 insertions(+), 7 deletions(-) diff --git a/.github/config-schema.json b/.github/config-schema.json index 9c572b1d..a53e35a7 100644 --- a/.github/config-schema.json +++ b/.github/config-schema.json @@ -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 diff --git a/docs/config/README.md b/docs/config/README.md index 68829fc1..3fdb9c16 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -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. diff --git a/src/configs/azure.rs b/src/configs/azure.rs index aaac529d..63780976 100644 --- a/src/configs/azure.rs +++ b/src/configs/azure.rs @@ -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, } 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(), } } } diff --git a/src/modules/azure.rs b/src/modules/azure.rs index de99a48e..da81beb7 100644 --- a/src/modules/azure.rs +++ b/src/modules/azure.rs @@ -56,7 +56,11 @@ pub fn module<'a>(context: &'a Context) -> Option> { _ => 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()?;