diff --git a/docs/docs/segment-az.md b/docs/docs/segment-az.md index 0adec984..32038411 100644 --- a/docs/docs/segment-az.md +++ b/docs/docs/segment-az.md @@ -23,6 +23,13 @@ Display the currently active Azure subscription information. } ``` +## Properties + +- source: `string` - where to fetch the information from - defaults to `first_match` + - `first_match`: try the CLI config first, then the PowerShell module. The first to resolve is displayed + - `cli`: fetch the information from the CLI config + - `pwsh`: fetch the information from the PowerShell Module config + ## Template ([info][templates]) :::note default template diff --git a/src/segments/az.go b/src/segments/az.go index 94ca6e6c..e784fc10 100644 --- a/src/segments/az.go +++ b/src/segments/az.go @@ -16,6 +16,14 @@ type Az struct { Origin string } +const ( + Source properties.Property = "source" + + pwsh = "pwsh" + cli = "cli" + firstMatch = "first_match" +) + type AzureConfig struct { Subscriptions []*AzureSubscription `json:"subscriptions"` InstallationID string `json:"installationId"` @@ -90,7 +98,16 @@ func (a *Az) Init(props properties.Properties, env environment.Environment) { } func (a *Az) Enabled() bool { - return a.getAzureProfile() || a.getAzureRmContext() + source := a.props.GetString(Source, firstMatch) + switch source { + case firstMatch: + return a.getCLISubscription() || a.getModuleSubscription() + case pwsh: + return a.getModuleSubscription() + case cli: + return a.getCLISubscription() + } + return false } func (a *Az) FileContentWithoutBom(file string) string { @@ -99,7 +116,7 @@ func (a *Az) FileContentWithoutBom(file string) string { return strings.TrimLeft(config, ByteOrderMark) } -func (a *Az) getAzureProfile() bool { +func (a *Az) getCLISubscription() bool { var content string profile := filepath.Join(a.ConfigHome(), "azureProfile.json") if content = a.FileContentWithoutBom(profile); len(content) == 0 { @@ -119,7 +136,7 @@ func (a *Az) getAzureProfile() bool { return false } -func (a *Az) getAzureRmContext() bool { +func (a *Az) getModuleSubscription() bool { var content string cfgHome := a.ConfigHome() profiles := []string{ diff --git a/src/segments/az_test.go b/src/segments/az_test.go index 20a09c82..f5e8553c 100644 --- a/src/segments/az_test.go +++ b/src/segments/az_test.go @@ -20,6 +20,7 @@ func TestAzSegment(t *testing.T) { HasCLI bool HasPowerShell bool Template string + Source string }{ { Case: "no config files found", @@ -67,6 +68,35 @@ func TestAzSegment(t *testing.T) { Template: "{{ .Origin }}", HasCLI: true, }, + { + Case: "Az CLI Profile only", + ExpectedEnabled: true, + ExpectedString: "AzureCliCloud", + Template: "{{ .EnvironmentName }}", + HasCLI: true, + Source: cli, + }, + { + Case: "Az CLI Profile only - disabled", + ExpectedEnabled: false, + Template: "{{ .EnvironmentName }}", + HasCLI: false, + Source: cli, + }, + { + Case: "PowerShell Profile only", + ExpectedEnabled: true, + ExpectedString: "AzurePoshCloud", + Template: "{{ .EnvironmentName }}", + HasPowerShell: true, + Source: pwsh, + }, + { + Case: "Az CLI Profile only - disabled", + ExpectedEnabled: false, + Template: "{{ .EnvironmentName }}", + Source: pwsh, + }, } for _, tc := range cases { @@ -86,9 +116,14 @@ func TestAzSegment(t *testing.T) { env.On("FileContent", filepath.Join(home, ".azure", "azureProfile.json")).Return(azureProfile) env.On("FileContent", filepath.Join(home, ".azure", "AzureRmContext.json")).Return(azureRmContext) env.On("Getenv", "AZURE_CONFIG_DIR").Return("") + if tc.Source == "" { + tc.Source = firstMatch + } az := &Az{ - env: env, - props: properties.Map{}, + env: env, + props: properties.Map{ + Source: tc.Source, + }, } assert.Equal(t, tc.ExpectedEnabled, az.Enabled(), tc.Case) assert.Equal(t, tc.ExpectedString, renderTemplate(env, tc.Template, az), tc.Case) diff --git a/themes/schema.json b/themes/schema.json index 634c9ab9..548b91e1 100644 --- a/themes/schema.json +++ b/themes/schema.json @@ -318,7 +318,20 @@ }, "then": { "title": "Azure Segment", - "description": "https://ohmyposh.dev/docs/az" + "description": "https://ohmyposh.dev/docs/az", + "properties": { + "source": { + "type": "string", + "title": "Source", + "description": "https://ohmyposh.dev/docs/az#properties", + "default": "first_match", + "enum": [ + "first_match", + "cli", + "pwsh" + ] + } + } } }, {