Repository: JanDeDobbeleer/oh-my-posh
Pull request #1879 (2022-03-09 12:47:34)

feat(az): allow switching between subscription source
### Prerequisites

- [x] I have read and understood the `CONTRIBUTING` guide
- [x] The commit message follows the [conventional commits][cc] guidelines
- [x] Tests for the changes have been added (for bug fixes/features)
- [x] Docs have been added / updated (for bug fixes/features)

### Description

relates to #1562


<!---

Tips:

If you're not comfortable with working with Git, we're working a guide (https://ohmyposh.dev/docs/contributing_git) to help you out.
Oh My Posh advises GitKraken (https://www.gitkraken.com/invite/nQmDPR9D) as your preferred cross platform Git GUI power tool.

-->

[cc]: https://www.conventionalcommits.org/en/v1.0.0/#summary



Patch:
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/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"
+                ]
+              }
+            }
           }
         },
         {


Test patch:
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)