oh-my-posh/src/segment_kubectl.go

54 lines
1.1 KiB
Go
Raw Normal View History

package main
2021-02-10 18:23:00 -08:00
import (
"strings"
)
type kubectl struct {
2021-02-10 18:23:00 -08:00
props *properties
env environmentInfo
Context string
Namespace string
}
func (k *kubectl) string() string {
2021-02-10 18:23:00 -08:00
segmentTemplate := k.props.getString(SegmentTemplate, "{{.Context}}{{if .Namespace}} :: {{.Namespace}}{{end}}")
template := &textTemplate{
Template: segmentTemplate,
Context: k,
Env: k.env,
2021-02-10 18:23:00 -08:00
}
2021-04-11 06:24:03 -07:00
text, err := template.render()
if err != nil {
return err.Error()
}
return text
}
func (k *kubectl) init(props *properties, env environmentInfo) {
k.props = props
k.env = env
}
func (k *kubectl) enabled() bool {
cmd := "kubectl"
if !k.env.hasCommand(cmd) {
return false
}
2021-02-10 18:23:00 -08:00
result, err := k.env.runCommand(cmd, "config", "view", "--minify", "--output", "jsonpath={..current-context},{..namespace}")
2021-02-12 12:39:20 -08:00
displayError := k.props.getBool(DisplayError, false)
if err != nil && displayError {
2021-02-10 18:23:00 -08:00
k.Context = "KUBECTL ERR"
k.Namespace = k.Context
return true
}
2021-02-12 12:39:20 -08:00
if err != nil {
return false
}
2021-02-10 18:23:00 -08:00
values := strings.Split(result, ",")
k.Context = values[0]
k.Namespace = values[1]
return k.Context != ""
}