feat(az): allow switching between subscription source

relates to #1562
This commit is contained in:
Jan De Dobbeleer 2022-03-09 13:46:57 +01:00 committed by Jan De Dobbeleer
parent f630fe89d6
commit 1b81e085e9
4 changed files with 78 additions and 6 deletions

View file

@ -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

View file

@ -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{

View file

@ -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)

View file

@ -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"
]
}
}
}
},
{