diff --git a/docs/docs/segment-kubectl.md b/docs/docs/segment-kubectl.md index 58e881eb..e722db8d 100644 --- a/docs/docs/segment-kubectl.md +++ b/docs/docs/segment-kubectl.md @@ -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 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 diff --git a/src/properties.go b/src/properties.go index c45f4799..b6426630 100644 --- a/src/properties.go +++ b/src/properties.go @@ -25,6 +25,8 @@ const ( AlwaysEnabled Property = "always_enabled" // SegmentTemplate is the template to use to render the information SegmentTemplate Property = "template" + // DisplayError to display when an error occurs or not + DisplayError Property = "display_error" ) type properties struct { diff --git a/src/segment_battery.go b/src/segment_battery.go index ccae9848..97f4a072 100644 --- a/src/segment_battery.go +++ b/src/segment_battery.go @@ -16,8 +16,6 @@ type batt struct { const ( // BatteryIcon to display in front of the battery BatteryIcon Property = "battery_icon" - // DisplayError to display when an error occurs or not - DisplayError Property = "display_error" // ChargingIcon to display when charging ChargingIcon Property = "charging_icon" // DischargingIcon o display when discharging diff --git a/src/segment_kubectl.go b/src/segment_kubectl.go index 7f0fc59d..b965b8a5 100644 --- a/src/segment_kubectl.go +++ b/src/segment_kubectl.go @@ -31,11 +31,15 @@ func (k *kubectl) enabled() bool { return false } 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.Namespace = k.Context return true } + if err != nil { + return false + } values := strings.Split(result, ",") k.Context = values[0] diff --git a/src/segment_kubectl_test.go b/src/segment_kubectl_test.go index 55eb04fc..40b40971 100644 --- a/src/segment_kubectl_test.go +++ b/src/segment_kubectl_test.go @@ -10,6 +10,7 @@ type kubectlArgs struct { kubectlExists bool kubectlErr bool template string + displayError bool context string namespace string } @@ -31,6 +32,7 @@ func bootStrapKubectlTest(args *kubectlArgs) *kubectl { props: &properties{ values: map[Property]interface{}{ SegmentTemplate: args.template, + DisplayError: args.displayError, }, }, } @@ -42,6 +44,7 @@ func TestKubectlSegment(t *testing.T) { cases := []struct { Case string Template string + DisplayError bool KubectlExists bool Context 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: "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: "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}, + {Case: "kubectl error hidden", Template: standardTemplate, DisplayError: false, KubectlExists: true, Context: "aaa", Namespace: "bbb", KubectlErr: true, + ExpectedString: "", ExpectedEnabled: false}, } for _, tc := range cases { args := &kubectlArgs{ kubectlExists: tc.KubectlExists, template: tc.Template, + displayError: tc.DisplayError, context: tc.Context, namespace: tc.Namespace, kubectlErr: tc.KubectlErr, diff --git a/themes/schema.json b/themes/schema.json index dfc355b3..2e8ac63d 100644 --- a/themes/schema.json +++ b/themes/schema.json @@ -718,6 +718,12 @@ "properties": { "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 } } }