oh-my-posh/src/segment_az.go

110 lines
2.5 KiB
Go
Raw Normal View History

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
}
const (
// SubscriptionInfoSeparator is put between the name and ID
SubscriptionInfoSeparator Property = "info_separator"
// DisplaySubscriptionID hides or show the subscription GUID
DisplaySubscriptionID Property = "display_id"
// DisplaySubscriptionName hides or shows the subscription display name
DisplaySubscriptionName Property = "display_name"
2021-05-21 10:56:31 -07:00
// DisplaySubscriptionAccount hides or shows the subscription account name
DisplaySubscriptionAccount Property = "display_account"
updateConsentNeeded = "Do you want to continue?"
updateMessage = "AZ CLI: Update needed!"
updateForeground = "#ffffff"
updateBackground = "#ff5349"
)
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)
}
2021-05-21 10:56:31 -07:00
return a.builder.String()
}
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")
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 {
cmd := "az"
2021-05-21 10:56:31 -07:00
if !a.env.hasCommand(cmd) {
return false
}
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
}
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
}
splittedOutput := strings.Split(output, "\n")
2021-05-21 10:56:31 -07:00
if len(splittedOutput) < 3 {
return false
}
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
}