From 47afa44ed30640310c9211aed02a7b9c95fe8de5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=9F=E9=85=8C=20=E9=B5=AC=E5=85=84?= Date: Fri, 14 Feb 2025 22:58:38 +0800 Subject: [PATCH] klabels can now filter key & value --- bash/sources/19_k8s_funcs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/bash/sources/19_k8s_funcs b/bash/sources/19_k8s_funcs index e821c0d..9ac90f3 100644 --- a/bash/sources/19_k8s_funcs +++ b/bash/sources/19_k8s_funcs @@ -11,6 +11,8 @@ function klabels { local max_label_length=0 local key= local value= + local filter_value="$1" + local filter_key="$2" while IFS= read -r line; do if [ -z "$header" ]; then @@ -32,20 +34,30 @@ function klabels { max_key_length=$(( ${#key} > max_key_length ? ${#key} : max_key_length )) done - # Print a line separator based on the longest key length - separator_length=$((max_key_length + trim_length)) - printf "%${separator_length}s\n" | tr ' ' "=" - # Loop through labels and align them for label in "${labels[@]}"; do key="${label%%=*}" value="${label#*=}" + if [ -n "$filter_key" ] && ! [[ "$key" == *$filter_key* ]] ; then + continue + fi + + if [ -n "$filter_value" ] && ! [[ "$value" == *$filter_value* ]] ; then + continue + fi + + # Format the label to align the '=' sign printf -v formatted_label "%-${max_key_length}s = %s" "$key" "$value" if [ -z "$first_line" ]; then first_line=1 + + # Print a line separator based on the longest key length + separator_length=$((max_key_length + trim_length)) + printf "%${separator_length}s\n" | tr ' ' "-" + echo "$line_text$formatted_label" else echo "$padding$formatted_label"