Skip to content

Commit

Permalink
feat: kubectl context display segment
Browse files Browse the repository at this point in the history
Segment displays the current Kubernetes context name when available.
  • Loading branch information
tillig authored and JanDeDobbeleer committed Oct 15, 2020
1 parent 57e3b4a commit 7537f6d
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
24 changes: 24 additions & 0 deletions docs/docs/segment-kubectl.md
@@ -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": ""
}
}
```
1 change: 1 addition & 0 deletions docs/sidebars.js
Expand Up @@ -20,6 +20,7 @@ module.exports = {
"environment",
"exit",
"git",
"kubectl",
"node",
"os",
"path",
Expand Down
3 changes: 3 additions & 0 deletions segment.go
Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down
24 changes: 24 additions & 0 deletions segment_kubectl.go
@@ -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 changes: 42 additions & 0 deletions segment_kubectl_test.go
@@ -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())
}

0 comments on commit 7537f6d

Please sign in to comment.