mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-03-05 20:49:04 -08:00
parent
f630fe89d6
commit
1b81e085e9
|
@ -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])
|
## Template ([info][templates])
|
||||||
|
|
||||||
:::note default template
|
:::note default template
|
||||||
|
|
|
@ -16,6 +16,14 @@ type Az struct {
|
||||||
Origin string
|
Origin string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
Source properties.Property = "source"
|
||||||
|
|
||||||
|
pwsh = "pwsh"
|
||||||
|
cli = "cli"
|
||||||
|
firstMatch = "first_match"
|
||||||
|
)
|
||||||
|
|
||||||
type AzureConfig struct {
|
type AzureConfig struct {
|
||||||
Subscriptions []*AzureSubscription `json:"subscriptions"`
|
Subscriptions []*AzureSubscription `json:"subscriptions"`
|
||||||
InstallationID string `json:"installationId"`
|
InstallationID string `json:"installationId"`
|
||||||
|
@ -90,7 +98,16 @@ func (a *Az) Init(props properties.Properties, env environment.Environment) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Az) Enabled() bool {
|
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 {
|
func (a *Az) FileContentWithoutBom(file string) string {
|
||||||
|
@ -99,7 +116,7 @@ func (a *Az) FileContentWithoutBom(file string) string {
|
||||||
return strings.TrimLeft(config, ByteOrderMark)
|
return strings.TrimLeft(config, ByteOrderMark)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Az) getAzureProfile() bool {
|
func (a *Az) getCLISubscription() bool {
|
||||||
var content string
|
var content string
|
||||||
profile := filepath.Join(a.ConfigHome(), "azureProfile.json")
|
profile := filepath.Join(a.ConfigHome(), "azureProfile.json")
|
||||||
if content = a.FileContentWithoutBom(profile); len(content) == 0 {
|
if content = a.FileContentWithoutBom(profile); len(content) == 0 {
|
||||||
|
@ -119,7 +136,7 @@ func (a *Az) getAzureProfile() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Az) getAzureRmContext() bool {
|
func (a *Az) getModuleSubscription() bool {
|
||||||
var content string
|
var content string
|
||||||
cfgHome := a.ConfigHome()
|
cfgHome := a.ConfigHome()
|
||||||
profiles := []string{
|
profiles := []string{
|
||||||
|
|
|
@ -20,6 +20,7 @@ func TestAzSegment(t *testing.T) {
|
||||||
HasCLI bool
|
HasCLI bool
|
||||||
HasPowerShell bool
|
HasPowerShell bool
|
||||||
Template string
|
Template string
|
||||||
|
Source string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
Case: "no config files found",
|
Case: "no config files found",
|
||||||
|
@ -67,6 +68,35 @@ func TestAzSegment(t *testing.T) {
|
||||||
Template: "{{ .Origin }}",
|
Template: "{{ .Origin }}",
|
||||||
HasCLI: true,
|
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 {
|
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", "azureProfile.json")).Return(azureProfile)
|
||||||
env.On("FileContent", filepath.Join(home, ".azure", "AzureRmContext.json")).Return(azureRmContext)
|
env.On("FileContent", filepath.Join(home, ".azure", "AzureRmContext.json")).Return(azureRmContext)
|
||||||
env.On("Getenv", "AZURE_CONFIG_DIR").Return("")
|
env.On("Getenv", "AZURE_CONFIG_DIR").Return("")
|
||||||
|
if tc.Source == "" {
|
||||||
|
tc.Source = firstMatch
|
||||||
|
}
|
||||||
az := &Az{
|
az := &Az{
|
||||||
env: env,
|
env: env,
|
||||||
props: properties.Map{},
|
props: properties.Map{
|
||||||
|
Source: tc.Source,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
assert.Equal(t, tc.ExpectedEnabled, az.Enabled(), tc.Case)
|
assert.Equal(t, tc.ExpectedEnabled, az.Enabled(), tc.Case)
|
||||||
assert.Equal(t, tc.ExpectedString, renderTemplate(env, tc.Template, az), tc.Case)
|
assert.Equal(t, tc.ExpectedString, renderTemplate(env, tc.Template, az), tc.Case)
|
||||||
|
|
|
@ -318,7 +318,20 @@
|
||||||
},
|
},
|
||||||
"then": {
|
"then": {
|
||||||
"title": "Azure Segment",
|
"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"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue