mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-02-21 02:55:37 -08:00
feat: kubectl namespace
This commit is contained in:
parent
bf44a446be
commit
731ebf6f9a
|
@ -6,7 +6,7 @@ sidebar_label: Kubectl
|
||||||
|
|
||||||
## What
|
## What
|
||||||
|
|
||||||
Display the currently active Kubernetes context name.
|
Display the currently active Kubernetes context name and namespace name.
|
||||||
|
|
||||||
## Sample Configuration
|
## Sample Configuration
|
||||||
|
|
||||||
|
@ -18,7 +18,27 @@ Display the currently active Kubernetes context name.
|
||||||
"foreground": "#000000",
|
"foreground": "#000000",
|
||||||
"background": "#ebcc34",
|
"background": "#ebcc34",
|
||||||
"properties": {
|
"properties": {
|
||||||
"prefix": " \uFD31 "
|
"prefix": " \uFD31 ",
|
||||||
|
"template": "{{.Context}}{{if .Namespace}} :: {{.Namespace}}{{end}}"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Properties
|
||||||
|
|
||||||
|
- template: `string` - A go [text/template][go-text-template] template utilizing the properties below.
|
||||||
|
Defaults to `{{.Context}}{{if .Namespace}} :: {{.Namespace}}{{end}}`
|
||||||
|
|
||||||
|
## Template Properties
|
||||||
|
|
||||||
|
- `.Context`: `string` - the current kubectl context
|
||||||
|
- `.Namespace`: `string` - the current kubectl namespace
|
||||||
|
|
||||||
|
## Tips
|
||||||
|
|
||||||
|
It is common for the Kubernetes "default" namespace to be used when no namespace is provided. If you want your prompt to
|
||||||
|
render an empty current namespace using the word "default", you can use something like this for the template:
|
||||||
|
|
||||||
|
`{{.Context}} :: {{if .Namespace}}{{.Namespace}}{{else}}default{{end}}`
|
||||||
|
|
||||||
|
[go-text-template]: https://golang.org/pkg/text/template/
|
||||||
|
|
|
@ -1,13 +1,23 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
type kubectl struct {
|
type kubectl struct {
|
||||||
props *properties
|
props *properties
|
||||||
env environmentInfo
|
env environmentInfo
|
||||||
contextName string
|
Context string
|
||||||
|
Namespace string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *kubectl) string() string {
|
func (k *kubectl) string() string {
|
||||||
return k.contextName
|
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) {
|
func (k *kubectl) init(props *properties, env environmentInfo) {
|
||||||
|
@ -20,6 +30,15 @@ func (k *kubectl) enabled() bool {
|
||||||
if !k.env.hasCommand(cmd) {
|
if !k.env.hasCommand(cmd) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
k.contextName, _ = k.env.runCommand(cmd, "config", "current-context")
|
result, err := k.env.runCommand(cmd, "config", "view", "--minify", "--output", "jsonpath={..current-context},{..namespace}")
|
||||||
return k.contextName != ""
|
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 != ""
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,45 +7,65 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type kubectlArgs struct {
|
type kubectlArgs struct {
|
||||||
enabled bool
|
kubectlExists bool
|
||||||
contextName string
|
kubectlErr bool
|
||||||
|
template string
|
||||||
|
context string
|
||||||
|
namespace string
|
||||||
}
|
}
|
||||||
|
|
||||||
func bootStrapKubectlTest(args *kubectlArgs) *kubectl {
|
func bootStrapKubectlTest(args *kubectlArgs) *kubectl {
|
||||||
env := new(MockedEnvironment)
|
env := new(MockedEnvironment)
|
||||||
env.On("hasCommand", "kubectl").Return(args.enabled)
|
env.On("hasCommand", "kubectl").Return(args.kubectlExists)
|
||||||
env.On("runCommand", "kubectl", []string{"config", "current-context"}).Return(args.contextName, nil)
|
kubectlOut := args.context + "," + args.namespace
|
||||||
|
var kubectlErr error = nil
|
||||||
|
if args.kubectlErr {
|
||||||
|
kubectlErr = &commandError{
|
||||||
|
err: "oops",
|
||||||
|
exitCode: 1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
env.On("runCommand", "kubectl", []string{"config", "view", "--minify", "--output", "jsonpath={..current-context},{..namespace}"}).Return(kubectlOut, kubectlErr)
|
||||||
k := &kubectl{
|
k := &kubectl{
|
||||||
env: env,
|
env: env,
|
||||||
props: &properties{},
|
props: &properties{
|
||||||
|
values: map[Property]interface{}{
|
||||||
|
SegmentTemplate: args.template,
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
return k
|
return k
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestKubectlWriterDisabled(t *testing.T) {
|
func TestKubectlSegment(t *testing.T) {
|
||||||
args := &kubectlArgs{
|
standardTemplate := "{{.Context}}{{if .Namespace}} :: {{.Namespace}}{{end}}"
|
||||||
enabled: false,
|
cases := []struct {
|
||||||
|
Case string
|
||||||
|
Template string
|
||||||
|
KubectlExists bool
|
||||||
|
Context string
|
||||||
|
Namespace string
|
||||||
|
KubectlErr bool
|
||||||
|
ExpectedEnabled bool
|
||||||
|
ExpectedString string
|
||||||
|
}{
|
||||||
|
{Case: "disabled", Template: standardTemplate, KubectlExists: false, Context: "aaa", Namespace: "bbb", ExpectedString: "", ExpectedEnabled: false},
|
||||||
|
{Case: "normal", Template: standardTemplate, KubectlExists: true, Context: "aaa", Namespace: "bbb", ExpectedString: "aaa :: bbb", ExpectedEnabled: true},
|
||||||
|
{Case: "no namespace", Template: standardTemplate, KubectlExists: true, Context: "aaa", Namespace: "", ExpectedString: "aaa", ExpectedEnabled: true},
|
||||||
|
{Case: "kubectl error", Template: standardTemplate, KubectlExists: true, Context: "aaa", Namespace: "bbb", KubectlErr: true,
|
||||||
|
ExpectedString: "KUBECTL ERR :: KUBECTL ERR", ExpectedEnabled: true},
|
||||||
}
|
}
|
||||||
kubectl := bootStrapKubectlTest(args)
|
|
||||||
assert.False(t, kubectl.enabled())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestKubectlEnabled(t *testing.T) {
|
for _, tc := range cases {
|
||||||
expected := "context-name"
|
args := &kubectlArgs{
|
||||||
args := &kubectlArgs{
|
kubectlExists: tc.KubectlExists,
|
||||||
enabled: true,
|
template: tc.Template,
|
||||||
contextName: expected,
|
context: tc.Context,
|
||||||
|
namespace: tc.Namespace,
|
||||||
|
kubectlErr: tc.KubectlErr,
|
||||||
|
}
|
||||||
|
kubectl := bootStrapKubectlTest(args)
|
||||||
|
assert.Equal(t, tc.ExpectedEnabled, kubectl.enabled(), tc.Case)
|
||||||
|
assert.Equal(t, tc.ExpectedString, kubectl.string(), tc.Case)
|
||||||
}
|
}
|
||||||
kubectl := bootStrapKubectlTest(args)
|
|
||||||
assert.True(t, kubectl.enabled())
|
|
||||||
assert.Equal(t, expected, kubectl.string())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestKubectlNoContext(t *testing.T) {
|
|
||||||
args := &kubectlArgs{
|
|
||||||
enabled: true,
|
|
||||||
contextName: "",
|
|
||||||
}
|
|
||||||
kubectl := bootStrapKubectlTest(args)
|
|
||||||
assert.False(t, kubectl.enabled())
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue