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"
|
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
if (!a.idEnabled() && !a.nameEnabled()) || !a.env.hasCommand("az") {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-10-16 08:43:02 -07:00
|
|
|
output, _ := a.env.runCommand("az", "account", "show", "--query=[name,id]", "-o=tsv")
|
2020-10-14 16:56:25 -07:00
|
|
|
if output == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
splittedOutput := strings.Split(output, "\n")
|
|
|
|
if len(splittedOutput) < 2 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
a.name = strings.TrimSpace(splittedOutput[0])
|
|
|
|
a.id = strings.TrimSpace(splittedOutput[1])
|
|
|
|
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)
|
|
|
|
}
|