mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-03-05 20:49:04 -08:00
feat: kubectl context display segment
Segment displays the current Kubernetes context name when available.
This commit is contained in:
parent
57e3b4ab80
commit
7537f6dc70
24
docs/docs/segment-kubectl.md
Normal file
24
docs/docs/segment-kubectl.md
Normal 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": " ⎈ "
|
||||
}
|
||||
}
|
||||
```
|
|
@ -20,6 +20,7 @@ module.exports = {
|
|||
"environment",
|
||||
"exit",
|
||||
"git",
|
||||
"kubectl",
|
||||
"node",
|
||||
"os",
|
||||
"path",
|
||||
|
|
|
@ -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
24
segment_kubectl.go
Normal 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
42
segment_kubectl_test.go
Executable 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())
|
||||
}
|
Loading…
Reference in a new issue