-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
segment_kubectl.go
44 lines (38 loc) · 926 Bytes
/
segment_kubectl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main
import (
"strings"
)
type kubectl struct {
props *properties
env environmentInfo
Context string
Namespace string
}
func (k *kubectl) string() string {
segmentTemplate := k.props.getString(SegmentTemplate, "{{.Context}}{{if .Namespace}} :: {{.Namespace}}{{end}}")
template := &textTemplate{
Template: segmentTemplate,
Context: k,
}
return template.render()
}
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
}
result, err := k.env.runCommand(cmd, "config", "view", "--minify", "--output", "jsonpath={..current-context},{..namespace}")
if err != nil {
k.Context = "KUBECTL ERR"
k.Namespace = k.Context
return true
}
values := strings.Split(result, ",")
k.Context = values[0]
k.Namespace = values[1]
return k.Context != ""
}