feat: kubectl context display segment

Segment displays the current Kubernetes context name when available.
This commit is contained in:
Travis Illig 2020-10-15 10:48:41 -07:00 committed by Jan De Dobbeleer
parent 57e3b4ab80
commit 7537f6dc70
5 changed files with 94 additions and 0 deletions

View file

@ -0,0 +1,24 @@
---
id: kubectl
title: Kubectl Context
sidebar_label: Kubectl
---
## What
Display the currently active Kubernetes context name.
## Sample Configuration
```json
{
"type": "kubectl",
"style": "powerline",
"powerline_symbol": "",
"foreground": "#000000",
"background": "#ebcc34",
"properties": {
"prefix": " ⎈ "
}
}
```

View file

@ -20,6 +20,7 @@ module.exports = {
"environment",
"exit",
"git",
"kubectl",
"node",
"os",
"path",

View file

@ -65,6 +65,8 @@ const (
EnvVar SegmentType = "envvar"
//Az writes the Azure subscription info we're currently in
Az SegmentType = "az"
//Kubectl writes the Kubernetes context we're currently in
Kubectl SegmentType = "kubectl"
//Powerline writes it Powerline style
Powerline SegmentStyle = "powerline"
//Plain writes it without ornaments
@ -120,6 +122,7 @@ func (segment *Segment) mapSegmentWithWriter(env environmentInfo) error {
Os: &osInfo{},
EnvVar: &envvar{},
Az: &az{},
Kubectl: &kubectl{},
}
if writer, ok := functions[segment.Type]; ok {
props := &properties{

24
segment_kubectl.go Normal file
View file

@ -0,0 +1,24 @@
package main
type kubectl struct {
props *properties
env environmentInfo
contextName string
}
func (k *kubectl) string() string {
return k.contextName
}
func (k *kubectl) init(props *properties, env environmentInfo) {
k.props = props
k.env = env
}
func (k *kubectl) enabled() bool {
if !k.env.hasCommand("kubectl") {
return false
}
k.contextName = k.env.runCommand("kubectl", "config", "current-context")
return true
}

42
segment_kubectl_test.go Executable file
View file

@ -0,0 +1,42 @@
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
type kubectlArgs struct {
enabled bool
contextName string
}
func bootStrapKubectlTest(args *kubectlArgs) *kubectl {
env := new(MockedEnvironment)
env.On("hasCommand", "kubectl").Return(args.enabled)
env.On("runCommand", "kubectl", []string{"config", "current-context"}).Return(args.contextName)
k := &kubectl{
env: env,
props: &properties{},
}
return k
}
func TestKubectlWriterDisabled(t *testing.T) {
args := &kubectlArgs{
enabled: false,
}
kubectl := bootStrapKubectlTest(args)
assert.False(t, kubectl.enabled())
}
func TestKubectlEnabled(t *testing.T) {
expected := "context-name"
args := &kubectlArgs{
enabled: true,
contextName: expected,
}
kubectl := bootStrapKubectlTest(args)
assert.True(t, kubectl.enabled())
assert.Equal(t, expected, kubectl.string())
}