2020-10-14 16:56:25 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type az struct {
|
2021-05-21 10:56:31 -07:00
|
|
|
props *properties
|
|
|
|
env environmentInfo
|
|
|
|
name string
|
|
|
|
id string
|
|
|
|
account string
|
|
|
|
builder strings.Builder
|
|
|
|
separator string
|
2020-10-14 16:56:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2020-11-12 00:43:32 -08:00
|
|
|
// SubscriptionInfoSeparator is put between the name and ID
|
2020-10-14 16:56:25 -07:00
|
|
|
SubscriptionInfoSeparator Property = "info_separator"
|
2020-11-12 00:43:32 -08:00
|
|
|
// DisplaySubscriptionID hides or show the subscription GUID
|
2020-10-14 16:56:25 -07:00
|
|
|
DisplaySubscriptionID Property = "display_id"
|
2020-11-12 00:43:32 -08:00
|
|
|
// DisplaySubscriptionName hides or shows the subscription display name
|
2020-10-14 16:56:25 -07:00
|
|
|
DisplaySubscriptionName Property = "display_name"
|
2021-05-21 10:56:31 -07:00
|
|
|
// DisplaySubscriptionAccount hides or shows the subscription account name
|
|
|
|
DisplaySubscriptionAccount Property = "display_account"
|
2021-04-22 11:18:58 -07:00
|
|
|
|
|
|
|
updateConsentNeeded = "Do you want to continue?"
|
|
|
|
updateMessage = "AZ CLI: Update needed!"
|
|
|
|
updateForeground = "#ffffff"
|
|
|
|
updateBackground = "#ff5349"
|
2020-10-14 16:56:25 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func (a *az) string() string {
|
2021-05-21 10:56:31 -07:00
|
|
|
a.separator = a.props.getString(SubscriptionInfoSeparator, " | ")
|
|
|
|
writeValue := func(value string) {
|
|
|
|
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)
|
2020-10-14 16:56:25 -07:00
|
|
|
}
|
|
|
|
|
2021-05-21 10:56:31 -07:00
|
|
|
return a.builder.String()
|
2020-10-14 16:56:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *az) init(props *properties, env environmentInfo) {
|
|
|
|
a.props = props
|
|
|
|
a.env = env
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *az) enabled() bool {
|
2021-05-21 10:56:31 -07:00
|
|
|
if a.getFromEnvVars() {
|
|
|
|
return true
|
2021-03-27 06:52:27 -07:00
|
|
|
}
|
|
|
|
|
2021-05-21 10:56:31 -07:00
|
|
|
return a.getFromAzCli()
|
2021-03-27 06:52:27 -07:00
|
|
|
}
|
|
|
|
|
2021-05-21 10:56:31 -07:00
|
|
|
func (a *az) getFromEnvVars() bool {
|
|
|
|
a.name = a.env.getenv("AZ_SUBSCRIPTION_NAME")
|
|
|
|
a.id = a.env.getenv("AZ_SUBSCRIPTION_ID")
|
2021-05-28 10:32:59 -07:00
|
|
|
a.account = a.env.getenv("AZ_SUBSCRIPTION_ACCOUNT")
|
2021-03-27 06:52:27 -07:00
|
|
|
|
2021-05-21 10:56:31 -07:00
|
|
|
if a.name == "" && a.id == "" {
|
|
|
|
return false
|
2021-03-27 06:52:27 -07:00
|
|
|
}
|
|
|
|
|
2021-05-21 10:56:31 -07:00
|
|
|
return true
|
2021-03-27 06:52:27 -07:00
|
|
|
}
|
|
|
|
|
2021-05-21 10:56:31 -07:00
|
|
|
func (a *az) getFromAzCli() bool {
|
2021-01-05 04:05:37 -08:00
|
|
|
cmd := "az"
|
2021-05-21 10:56:31 -07:00
|
|
|
if !a.env.hasCommand(cmd) {
|
|
|
|
return false
|
2020-10-14 16:56:25 -07:00
|
|
|
}
|
|
|
|
|
2021-05-21 10:56:31 -07:00
|
|
|
output, _ := a.env.runCommand(cmd, "account", "show", "--query=[name,id,user.name]", "-o=tsv")
|
|
|
|
if len(output) == 0 {
|
|
|
|
return false
|
2020-10-14 16:56:25 -07:00
|
|
|
}
|
|
|
|
|
2021-04-22 11:18:58 -07:00
|
|
|
if strings.Contains(output, updateConsentNeeded) {
|
|
|
|
a.props.foreground = updateForeground
|
|
|
|
a.props.background = updateBackground
|
2021-05-21 10:56:31 -07:00
|
|
|
a.name = updateMessage
|
|
|
|
return true
|
2021-04-22 11:18:58 -07:00
|
|
|
}
|
|
|
|
|
2020-10-14 16:56:25 -07:00
|
|
|
splittedOutput := strings.Split(output, "\n")
|
2021-05-21 10:56:31 -07:00
|
|
|
if len(splittedOutput) < 3 {
|
|
|
|
return false
|
2020-10-14 16:56:25 -07:00
|
|
|
}
|
|
|
|
|
2021-05-21 10:56:31 -07:00
|
|
|
a.name = strings.TrimSpace(splittedOutput[0])
|
|
|
|
a.id = strings.TrimSpace(splittedOutput[1])
|
|
|
|
a.account = strings.TrimSpace(splittedOutput[2])
|
|
|
|
return true
|
2020-10-14 16:56:25 -07:00
|
|
|
}
|