Skip to content

Commit

Permalink
feat(azure): subscription name aliases (#4949)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
marcybelardo committed Apr 13, 2023
1 parent edb96ca commit 27ffa37
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 7 deletions.
8 changes: 8 additions & 0 deletions .github/config-schema.json
Expand Up @@ -29,6 +29,7 @@
"disabled": true,
"format": "on [$symbol($subscription)]($style) ",
"style": "blue bold",
"subscription_aliases": {},
"symbol": ""
},
"allOf": [
Expand Down Expand Up @@ -1849,6 +1850,13 @@
"disabled": {
"default": true,
"type": "boolean"
},
"subscription_aliases": {
"default": {},
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"additionalProperties": false
Expand Down
22 changes: 16 additions & 6 deletions docs/config/README.md
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions src/configs/azure.rs
@@ -1,4 +1,5 @@
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

#[derive(Clone, Deserialize, Serialize)]
#[cfg_attr(
Expand All @@ -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> {
Expand All @@ -21,6 +23,7 @@ impl<'a> Default for AzureConfig<'a> {
symbol: "ﴃ ",
style: "blue bold",
disabled: true,
subscription_aliases: HashMap::new(),
}
}
}
81 changes: 80 additions & 1 deletion src/modules/azure.rs
Expand Up @@ -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,
})
Expand Down Expand Up @@ -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()?;
Expand Down

0 comments on commit 27ffa37

Please sign in to comment.