Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

rodrigobdz
Copy link

@rodrigobdz rodrigobdz commented Apr 17, 2023

Truncate kubectl output using environment variables.

Description

Requires the following variables to be defined.

Example variable definition:

set -x tide_kubectl_truncation_strategy l
set -x tide_kubectl_truncation_length 31

Motivation and Context

My kubectl context is too large and almost fills an entire line.

Same approach as in:

if git branch --show-current 2>/dev/null | string shorten -"$tide_git_truncation_strategy"m$tide_git_truncation_length | read -l location

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.

  • I have tested using Linux.
  • I have tested using MacOS.

Checklist

  • I am ready to update the wiki accordingly.
  • I have updated the tests accordingly.

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
@olujicz
Copy link

olujicz commented Sep 27, 2023

I can confirm that this is working on Linux.

@rodrigobdz
Copy link
Author

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 &&
Copy link

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

Copy link
Author

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.

Copy link

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works

Copy link

@0xMH 0xMH Jan 23, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants