oh-my-posh/segment_az.go
Travis Illig 5844faa54d feat: dotnet segment for .NET SDK display
New segment for .NET SDK version (or unsupported version) display.

Includes update for handling command execution errors so segments
can act differently based on exit codes. Using a custom error
type to make it testable rather than passing the OS error directly
to the segment.
2020-10-16 11:39:01 -07:00

81 lines
1.6 KiB
Go

package main
import (
"fmt"
"strings"
)
type az struct {
props *properties
env environmentInfo
name string
id 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"
)
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
}
output, _ := a.env.runCommand("az", "account", "show", "--query=[name,id]", "-o=tsv")
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)
}