Added 19 k8s helper functions
This commit is contained in:
		
							
								
								
									
										55
									
								
								bash/sources/19_k8s_funcs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								bash/sources/19_k8s_funcs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,55 @@
 | 
			
		||||
#!/bin/bash
 | 
			
		||||
 | 
			
		||||
# Written with the help of chatgpt o3-mini
 | 
			
		||||
function klabels {
 | 
			
		||||
	local header=
 | 
			
		||||
	local padding=
 | 
			
		||||
	local trim_length=
 | 
			
		||||
	local line_text=
 | 
			
		||||
	local first_line=
 | 
			
		||||
	local max_key_length=0
 | 
			
		||||
	local max_label_length=0
 | 
			
		||||
	local key=
 | 
			
		||||
	local value=
 | 
			
		||||
 | 
			
		||||
	while IFS= read -r line; do
 | 
			
		||||
		if [ -z "$header" ]; then
 | 
			
		||||
			header="$line"
 | 
			
		||||
			padding="${header//LABELS/}"
 | 
			
		||||
			padding="${padding//?/ }"
 | 
			
		||||
			echo "$header"
 | 
			
		||||
			trim_length=${#padding}
 | 
			
		||||
			continue
 | 
			
		||||
		fi
 | 
			
		||||
 | 
			
		||||
		line_text="${line:0:trim_length}"
 | 
			
		||||
		first_line=
 | 
			
		||||
		IFS=',' read -ra labels <<< "${line:trim_length}"
 | 
			
		||||
 | 
			
		||||
		# Calculate the maximum key length to align key=value pairs
 | 
			
		||||
		for label in "${labels[@]}"; do
 | 
			
		||||
			key="${label%%=*}"
 | 
			
		||||
			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#*=}"
 | 
			
		||||
 | 
			
		||||
			# 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
 | 
			
		||||
				echo "$line_text$formatted_label"
 | 
			
		||||
			else
 | 
			
		||||
				echo "$padding$formatted_label"
 | 
			
		||||
			fi
 | 
			
		||||
		done
 | 
			
		||||
	done
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user