Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: kubectl context display segment
Segment displays the current Kubernetes context name when available.
- Loading branch information
1 parent
57e3b4a
commit 7537f6d
Showing
5 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": " ⎈ " | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ module.exports = { | |
"environment", | ||
"exit", | ||
"git", | ||
"kubectl", | ||
"node", | ||
"os", | ||
"path", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) | ||
} |