feat(az): display account info

This commit is contained in:
Jan De Dobbeleer 2021-05-21 19:56:31 +02:00 committed by Jan De Dobbeleer
parent c3b6f31d09
commit ed610c13ee
4 changed files with 95 additions and 71 deletions

View file

@ -33,6 +33,7 @@ To enable this, set `$env:AZ_ENABLED = $true` in your `$PROFILE`.
## Properties ## Properties
- info_separator: `string` - text/icon to put in between the subscription name and ID - defaults to ` | ` - display_account: `boolean` - display the subscription account name or not - defaults to `false`
- display_id: `boolean` - display the subscription ID or not - defaults to `false`
- display_name: `boolean` - display the subscription name or not - defaults to `true` - display_name: `boolean` - display the subscription name or not - defaults to `true`
- display_id: `boolean` - display the subscription ID or not - defaults to `false`
- info_separator: `string` - text/icon to put in between the values - defaults to ` | `

View file

@ -47,10 +47,11 @@ function global:Initialize-ModuleSupport {
if ($env:AZ_ENABLED -eq $true) { if ($env:AZ_ENABLED -eq $true) {
try { try {
$subscription = Get-AzContext | Select-Object -ExpandProperty "Subscription" | Select-Object "Name", "Id" $subscription = Get-AzContext | Select-Object -ExpandProperty "Subscription" | Select-Object "Name", "Id", "Account"
if ($null -ne $subscription) { if ($null -ne $subscription) {
$env:AZ_SUBSCRIPTION_NAME = $subscription.Name $env:AZ_SUBSCRIPTION_NAME = $subscription.Name
$env:AZ_SUBSCRIPTION_ID = $subscription.Id $env:AZ_SUBSCRIPTION_ID = $subscription.Id
$env:AZ_SUBSCRIPTION_ACCOUNT = $subscription.Account
} }
} }
catch {} catch {}

View file

@ -1,7 +1,6 @@
package main package main
import ( import (
"fmt"
"strings" "strings"
) )
@ -10,6 +9,9 @@ type az struct {
env environmentInfo env environmentInfo
name string name string
id string id string
account string
builder strings.Builder
separator string
} }
const ( const (
@ -19,6 +21,8 @@ const (
DisplaySubscriptionID Property = "display_id" DisplaySubscriptionID Property = "display_id"
// DisplaySubscriptionName hides or shows the subscription display name // DisplaySubscriptionName hides or shows the subscription display name
DisplaySubscriptionName Property = "display_name" DisplaySubscriptionName Property = "display_name"
// DisplaySubscriptionAccount hides or shows the subscription account name
DisplaySubscriptionAccount Property = "display_account"
updateConsentNeeded = "Do you want to continue?" updateConsentNeeded = "Do you want to continue?"
updateMessage = "AZ CLI: Update needed!" updateMessage = "AZ CLI: Update needed!"
@ -27,12 +31,27 @@ const (
) )
func (a *az) string() string { func (a *az) string() string {
separator := "" a.separator = a.props.getString(SubscriptionInfoSeparator, " | ")
if a.idEnabled() && a.nameEnabled() { writeValue := func(value string) {
separator = a.props.getString(SubscriptionInfoSeparator, " | ") if len(value) == 0 {
return
}
if a.builder.Len() > 0 {
a.builder.WriteString(a.separator)
}
a.builder.WriteString(value)
}
if a.props.getBool(DisplaySubscriptionAccount, false) {
writeValue(a.account)
}
if a.props.getBool(DisplaySubscriptionName, true) {
writeValue(a.name)
}
if a.props.getBool(DisplaySubscriptionID, false) {
writeValue(a.id)
} }
return fmt.Sprintf("%s%s%s", a.getName(), separator, a.getID()) return a.builder.String()
} }
func (a *az) init(props *properties, env environmentInfo) { func (a *az) init(props *properties, env environmentInfo) {
@ -41,75 +60,50 @@ func (a *az) init(props *properties, env environmentInfo) {
} }
func (a *az) enabled() bool { func (a *az) enabled() bool {
var enabled bool if a.getFromEnvVars() {
a.name, a.id, enabled = a.getFromEnvVars() return true
if enabled {
return enabled
} }
a.name, a.id, enabled = a.getFromAzCli() return a.getFromAzCli()
return enabled
} }
func (a *az) getFromEnvVars() (string, string, bool) { func (a *az) getFromEnvVars() bool {
name := a.env.getenv("AZ_SUBSCRIPTION_NAME") a.name = a.env.getenv("AZ_SUBSCRIPTION_NAME")
id := a.env.getenv("AZ_SUBSCRIPTION_ID") a.id = a.env.getenv("AZ_SUBSCRIPTION_ID")
a.account = a.env.getenv("AZ_SUBSCRIPTION_ID")
if name == "" && id == "" { if a.name == "" && a.id == "" {
return "", "", false return false
} }
return name, id, true return true
} }
func (a *az) getFromAzCli() (string, string, bool) { func (a *az) getFromAzCli() bool {
cmd := "az" cmd := "az"
if (!a.idEnabled() && !a.nameEnabled()) || !a.env.hasCommand(cmd) { if !a.env.hasCommand(cmd) {
return "", "", false return false
} }
output, _ := a.env.runCommand(cmd, "account", "show", "--query=[name,id]", "-o=tsv") output, _ := a.env.runCommand(cmd, "account", "show", "--query=[name,id,user.name]", "-o=tsv")
if output == "" { if len(output) == 0 {
return "", "", false return false
} }
if strings.Contains(output, updateConsentNeeded) { if strings.Contains(output, updateConsentNeeded) {
a.props.foreground = updateForeground a.props.foreground = updateForeground
a.props.background = updateBackground a.props.background = updateBackground
return updateMessage, "", true a.name = updateMessage
return true
} }
splittedOutput := strings.Split(output, "\n") splittedOutput := strings.Split(output, "\n")
if len(splittedOutput) < 2 { if len(splittedOutput) < 3 {
return "", "", false return false
} }
name := strings.TrimSpace(splittedOutput[0]) a.name = strings.TrimSpace(splittedOutput[0])
id := strings.TrimSpace(splittedOutput[1]) a.id = strings.TrimSpace(splittedOutput[1])
a.account = strings.TrimSpace(splittedOutput[2])
return name, id, true return true
}
func (a *az) getID() string {
if !a.idEnabled() {
return ""
}
return a.id
}
func (a *az) getName() string {
if !a.nameEnabled() {
return ""
}
return a.name
}
func (a *az) idEnabled() bool {
return a.props.getBool(DisplaySubscriptionID, false)
}
func (a *az) nameEnabled() bool {
return a.props.getBool(DisplaySubscriptionName, true)
} }

View file

@ -14,13 +14,29 @@ func TestAzSegment(t *testing.T) {
ExpectedString string ExpectedString string
EnvSubName string EnvSubName string
EnvSubID string EnvSubID string
EnvSubAccount string
CliExists bool CliExists bool
CliSubName string CliSubName string
CliSubID string CliSubID string
CliSubAccount string
InfoSeparator string InfoSeparator string
DisplayID bool DisplayID bool
DisplayName bool DisplayName bool
DisplayAccount bool
}{ }{
{
Case: "print only account",
ExpectedEnabled: true,
ExpectedString: "foobar",
CliExists: true,
CliSubName: "foo",
CliSubID: "bar",
CliSubAccount: "foobar",
InfoSeparator: "$",
DisplayID: false,
DisplayName: false,
DisplayAccount: true,
},
{ {
Case: "envvars present", Case: "envvars present",
ExpectedEnabled: true, ExpectedEnabled: true,
@ -35,7 +51,7 @@ func TestAzSegment(t *testing.T) {
{ {
Case: "envvar name present", Case: "envvar name present",
ExpectedEnabled: true, ExpectedEnabled: true,
ExpectedString: "foo$", ExpectedString: "foo",
EnvSubName: "foo", EnvSubName: "foo",
CliExists: false, CliExists: false,
InfoSeparator: "$", InfoSeparator: "$",
@ -45,7 +61,7 @@ func TestAzSegment(t *testing.T) {
{ {
Case: "envvar id present", Case: "envvar id present",
ExpectedEnabled: true, ExpectedEnabled: true,
ExpectedString: "$bar", ExpectedString: "bar",
EnvSubID: "bar", EnvSubID: "bar",
CliExists: false, CliExists: false,
InfoSeparator: "$", InfoSeparator: "$",
@ -55,7 +71,7 @@ func TestAzSegment(t *testing.T) {
{ {
Case: "cli not found", Case: "cli not found",
ExpectedEnabled: false, ExpectedEnabled: false,
ExpectedString: "$", ExpectedString: "",
CliExists: false, CliExists: false,
InfoSeparator: "$", InfoSeparator: "$",
DisplayID: true, DisplayID: true,
@ -96,13 +112,11 @@ func TestAzSegment(t *testing.T) {
}, },
{ {
Case: "print none", Case: "print none",
ExpectedEnabled: false, ExpectedEnabled: true,
CliExists: true, CliExists: true,
CliSubName: "foo", CliSubName: "foo",
CliSubID: "bar", CliSubID: "bar",
InfoSeparator: "$", InfoSeparator: "$",
DisplayID: false,
DisplayName: false,
}, },
{ {
Case: "update needed", Case: "update needed",
@ -113,6 +127,16 @@ func TestAzSegment(t *testing.T) {
DisplayID: false, DisplayID: false,
DisplayName: true, DisplayName: true,
}, },
{
Case: "account info",
ExpectedEnabled: true,
ExpectedString: updateMessage,
CliExists: true,
CliSubName: "Do you want to continue? (Y/n): Visual Studio Enterprise",
DisplayID: false,
DisplayName: true,
DisplayAccount: true,
},
} }
for _, tc := range cases { for _, tc := range cases {
@ -120,12 +144,16 @@ func TestAzSegment(t *testing.T) {
env.On("getenv", "AZ_SUBSCRIPTION_NAME").Return(tc.EnvSubName) env.On("getenv", "AZ_SUBSCRIPTION_NAME").Return(tc.EnvSubName)
env.On("getenv", "AZ_SUBSCRIPTION_ID").Return(tc.EnvSubID) env.On("getenv", "AZ_SUBSCRIPTION_ID").Return(tc.EnvSubID)
env.On("hasCommand", "az").Return(tc.CliExists) env.On("hasCommand", "az").Return(tc.CliExists)
env.On("runCommand", "az", []string{"account", "show", "--query=[name,id]", "-o=tsv"}).Return(fmt.Sprintf("%s\n%s\n", tc.CliSubName, tc.CliSubID), nil) env.On("runCommand", "az", []string{"account", "show", "--query=[name,id,user.name]", "-o=tsv"}).Return(
fmt.Sprintf("%s\n%s\n%s\n", tc.CliSubName, tc.CliSubID, tc.CliSubAccount),
nil,
)
props := &properties{ props := &properties{
values: map[Property]interface{}{ values: map[Property]interface{}{
SubscriptionInfoSeparator: tc.InfoSeparator, SubscriptionInfoSeparator: tc.InfoSeparator,
DisplaySubscriptionID: tc.DisplayID, DisplaySubscriptionID: tc.DisplayID,
DisplaySubscriptionName: tc.DisplayName, DisplaySubscriptionName: tc.DisplayName,
DisplaySubscriptionAccount: tc.DisplayAccount,
}, },
} }