Skip to content

Instantly share code, notes, and snippets.

@nielswind
Last active April 19, 2023 09:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nielswind/8a9e2ae451ff6b690f9baff6a28851ba to your computer and use it in GitHub Desktop.
Save nielswind/8a9e2ae451ff6b690f9baff6a28851ba to your computer and use it in GitHub Desktop.
extract (k8s) kind and optionally name from yaml
#!/usr/bin/env bash
# https://gist.githubusercontent.com/nielswind/8a9e2ae451ff6b690f9baff6a28851ba/raw/extract.sh
function depcheck() {
local DEPS=($*)
for cmd in "${DEPS[@]}"
do
[[ $(command -v $cmd 2>/dev/null) ]] || { echo -en "\n$cmd needs to be installed.";fail=1; }
done
[[ $fail -ne 1 ]] || { echo -en "\n${0}: Install the above and rerun this script\n";exit 1; }
}
depcheck yq
[[ "$#" -lt 1 || "$#" -gt 2 ]] && { echo "Usage: ${0##*/} Kind [Name]"; echo "Note: reads from stdin"; exit 1; }
export K=$1
export N=$2
IFS=
out=$(cat <(yq e '. | select(.kind == env(K))'))
[[ "$#" -eq 2 ]] && out=$(echo $out | yq e '. | select(.metadata.name == env(N))')
echo $out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment