2020-10-14 16:56:25 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type az struct {
|
|
|
|
props *properties
|
|
|
|
env environmentInfo
|
|
|
|
name string
|
|
|
|
id string
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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 {
|
|
|
|
separator := ""
|
|
|
|
if a.idEnabled() && a.nameEnabled() {
|
|
|
|
separator = a.props.getString(SubscriptionInfoSeparator, " | ")
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s%s%s", a.getName(), separator, a.getID())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *az) init(props *properties, env environmentInfo) {
|
|
|
|
a.props = props
|
|
|
|
a.env = env
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *az) enabled() bool {
|
2021-03-27 06:52:27 -07:00
|
|
|
var enabled bool
|
|
|
|
a.name, a.id, enabled = a.getFromEnvVars()
|
|
|
|
if enabled {
|
|
|
|
return enabled
|
|
|
|
}
|
|
|
|
|
|
|
|
a.name, a.id, enabled = a.getFromAzCli()
|
|
|
|
return enabled
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *az) getFromEnvVars() (string, string, bool) {
|
|
|
|
name := a.env.getenv("AZ_SUBSCRIPTION_NAME")
|
|
|
|
id := a.env.getenv("AZ_SUBSCRIPTION_ID")
|
|
|
|
|
|
|
|
if name == "" && id == "" {
|
|
|
|
return "", "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
return name, id, true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *az) getFromAzCli() (string, string, bool) {
|
2021-01-05 04:05:37 -08:00
|
|
|
cmd := "az"
|
|
|
|
if (!a.idEnabled() && !a.nameEnabled()) || !a.env.hasCommand(cmd) {
|
2021-03-27 06:52:27 -07:00
|
|
|
return "", "", false
|
2020-10-14 16:56:25 -07:00
|
|
|
}
|
|
|
|
|
2021-01-05 04:05:37 -08:00
|
|
|
output, _ := a.env.runCommand(cmd, "account", "show", "--query=[name,id]", "-o=tsv")
|
2020-10-14 16:56:25 -07:00
|
|
|
if output == "" {
|
2021-03-27 06:52:27 -07:00
|
|
|
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
|
|
|
|
return updateMessage, "", true
|
|
|
|
}
|
|
|
|
|
2020-10-14 16:56:25 -07:00
|
|
|
splittedOutput := strings.Split(output, "\n")
|
|
|
|
if len(splittedOutput) < 2 {
|
2021-03-27 06:52:27 -07:00
|
|
|
return "", "", false
|
2020-10-14 16:56:25 -07:00
|
|
|
}
|
|
|
|
|
2021-03-27 06:52:27 -07:00
|
|
|
name := strings.TrimSpace(splittedOutput[0])
|
|
|
|
id := strings.TrimSpace(splittedOutput[1])
|
|
|
|
|
|
|
|
return name, id, true
|
2020-10-14 16:56:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|