feat: add display_error to kubectl

This commit is contained in:
TravisTX 2021-02-12 13:39:20 -07:00 committed by Jan De Dobbeleer
parent 616cc3a685
commit acfda5c9ca
6 changed files with 21 additions and 4 deletions

View file

@ -28,6 +28,7 @@ Display the currently active Kubernetes context name and namespace name.
- template: `string` - A go [text/template][go-text-template] template extended with [sprig][sprig] utilizing the - template: `string` - A go [text/template][go-text-template] template extended with [sprig][sprig] utilizing the
properties below. Defaults to `{{.Context}}{{if .Namespace}} :: {{.Namespace}}{{end}}` properties below. Defaults to `{{.Context}}{{if .Namespace}} :: {{.Namespace}}{{end}}`
- display_error: `boolean` - show the error context when failing to retrieve the kubectl information - defaults to `false`
## Template Properties ## Template Properties

View file

@ -25,6 +25,8 @@ const (
AlwaysEnabled Property = "always_enabled" AlwaysEnabled Property = "always_enabled"
// SegmentTemplate is the template to use to render the information // SegmentTemplate is the template to use to render the information
SegmentTemplate Property = "template" SegmentTemplate Property = "template"
// DisplayError to display when an error occurs or not
DisplayError Property = "display_error"
) )
type properties struct { type properties struct {

View file

@ -16,8 +16,6 @@ type batt struct {
const ( const (
// BatteryIcon to display in front of the battery // BatteryIcon to display in front of the battery
BatteryIcon Property = "battery_icon" BatteryIcon Property = "battery_icon"
// DisplayError to display when an error occurs or not
DisplayError Property = "display_error"
// ChargingIcon to display when charging // ChargingIcon to display when charging
ChargingIcon Property = "charging_icon" ChargingIcon Property = "charging_icon"
// DischargingIcon o display when discharging // DischargingIcon o display when discharging

View file

@ -31,11 +31,15 @@ func (k *kubectl) enabled() bool {
return false return false
} }
result, err := k.env.runCommand(cmd, "config", "view", "--minify", "--output", "jsonpath={..current-context},{..namespace}") result, err := k.env.runCommand(cmd, "config", "view", "--minify", "--output", "jsonpath={..current-context},{..namespace}")
if err != nil { displayError := k.props.getBool(DisplayError, false)
if err != nil && displayError {
k.Context = "KUBECTL ERR" k.Context = "KUBECTL ERR"
k.Namespace = k.Context k.Namespace = k.Context
return true return true
} }
if err != nil {
return false
}
values := strings.Split(result, ",") values := strings.Split(result, ",")
k.Context = values[0] k.Context = values[0]

View file

@ -10,6 +10,7 @@ type kubectlArgs struct {
kubectlExists bool kubectlExists bool
kubectlErr bool kubectlErr bool
template string template string
displayError bool
context string context string
namespace string namespace string
} }
@ -31,6 +32,7 @@ func bootStrapKubectlTest(args *kubectlArgs) *kubectl {
props: &properties{ props: &properties{
values: map[Property]interface{}{ values: map[Property]interface{}{
SegmentTemplate: args.template, SegmentTemplate: args.template,
DisplayError: args.displayError,
}, },
}, },
} }
@ -42,6 +44,7 @@ func TestKubectlSegment(t *testing.T) {
cases := []struct { cases := []struct {
Case string Case string
Template string Template string
DisplayError bool
KubectlExists bool KubectlExists bool
Context string Context string
Namespace string Namespace string
@ -52,14 +55,17 @@ func TestKubectlSegment(t *testing.T) {
{Case: "disabled", Template: standardTemplate, KubectlExists: false, Context: "aaa", Namespace: "bbb", ExpectedString: "", ExpectedEnabled: false}, {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: "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: "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, {Case: "kubectl error", Template: standardTemplate, DisplayError: true, KubectlExists: true, Context: "aaa", Namespace: "bbb", KubectlErr: true,
ExpectedString: "KUBECTL ERR :: KUBECTL ERR", ExpectedEnabled: true}, ExpectedString: "KUBECTL ERR :: KUBECTL ERR", ExpectedEnabled: true},
{Case: "kubectl error hidden", Template: standardTemplate, DisplayError: false, KubectlExists: true, Context: "aaa", Namespace: "bbb", KubectlErr: true,
ExpectedString: "", ExpectedEnabled: false},
} }
for _, tc := range cases { for _, tc := range cases {
args := &kubectlArgs{ args := &kubectlArgs{
kubectlExists: tc.KubectlExists, kubectlExists: tc.KubectlExists,
template: tc.Template, template: tc.Template,
displayError: tc.DisplayError,
context: tc.Context, context: tc.Context,
namespace: tc.Namespace, namespace: tc.Namespace,
kubectlErr: tc.KubectlErr, kubectlErr: tc.KubectlErr,

View file

@ -718,6 +718,12 @@
"properties": { "properties": {
"template": { "template": {
"$ref": "#/definitions/template" "$ref": "#/definitions/template"
},
"display_error": {
"type": "boolean",
"title": "Display Error",
"description": "Show the error context when failing to retrieve the kubectl information",
"default": false
} }
} }
} }