-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: Truncate kubectl context using variables #401
base: main
Are you sure you want to change the base?
Conversation
Requires the following variables to be defined. Example variable definition: set -x tide_kubectl_truncation_strategy l set -x tide_kubectl_truncation_length 31 Same approach as in: https://github.com/IlanCosman/tide/blob/447945d2cff8f70d5c791dd4eec8b322d37798dd/functions/_tide_item_git.fish#L2
I can confirm that this is working on Linux. |
Thanks @olujicz. I checked the Linux test accordingly. |
@@ -1,4 +1,4 @@ | |||
function _tide_item_kubectl | |||
kubectl config view --minify --output 'jsonpath={.current-context}/{..namespace}' 2>/dev/null | read -l context && | |||
kubectl config view --minify --output 'jsonpath={.current-context}/{..namespace}' 2>/dev/null | string shorten -"$tide_kubectl_truncation_strategy"m"$tide_kubectl_truncation_length" | read -l context && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't work in MacOS 14.2.1.
Maybe use AWK instead of string?
function _tide_item_kubectl
set context (kubectl config view --minify --output 'jsonpath={.current-context}/{..namespace}' 2>/dev/null | \
awk -v len="$tide_kubectl_truncation_length" '{print substr($0, 1, len)}')
if set -q context[1]
_tide_print_item kubectl $tide_kubectl_icon' ' (string replace -r '/(|default)$' '' $context)
end
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@0xMH I am open to using awk but the snippet above returns the beginning and not the end of the string.
Example:
The command
kubectl config view --minify --output 'jsonpath={.current-context}/{..namespace}'
returns the untruncated string:
arn:aws:eks:<region>:<numbers>:cluster/<clustername>/<namespace>
The snippet above returns arn:aws:eks:<region>:<some_numbers>
instead of <clustername>/<namespace>
for a given string length.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now?
function _tide_item_kubectl
set context (kubectl config view --minify --output 'jsonpath={.current-context}/{..namespace}' 2>/dev/null | \
awk -v len="$tide_kubectl_truncation_length" '{print substr($0, length($0)-len+1)}')
if set -q context[1]
_tide_print_item kubectl $tide_kubectl_icon' ' (string replace -r '/(|default)$' '' $context)
end
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think, @IlanCosman? I'm interested in advancing this PR for an additional use case, which involves toggling the visibility of the kube context in Fish shell. Specifically, I want it to show only when I'm in the kubie shell.
Truncate
kubectl
output using environment variables.Description
Requires the following variables to be defined.
Example variable definition:
Motivation and Context
My
kubectl
context is too large and almost fills an entire line.Same approach as in:
tide/functions/_tide_item_git.fish
Line 2 in 447945d
Closes #
Screenshots (if appropriate)
How Has This Been Tested
I tested the changes by modifying the file
$HOME/.config/fish/functions/_tide_item_kubectl.fish
.Checklist