get-ix-pods scripting support added

This commit is contained in:
Lockszmith 2023-11-17 12:55:18 -05:00
parent 64972fc3b2
commit e179df0b48
1 changed files with 61 additions and 35 deletions

View File

@ -10,41 +10,63 @@ get-apps() {
k3s kubectl get --all-namespaces deployments.apps --sort-by='.metadata.namespace' "${@}" k3s kubectl get --all-namespaces deployments.apps --sort-by='.metadata.namespace' "${@}"
} }
FILTER=('') FILTER1=('')
FILTER2=()
OUTPUT=()
ARG="${1:---all}" while [[ -n "$1" ]]; do
ARG="${1}"
shift
if [[ -n "$ARG" ]]; then if [[ "${#OUTPUT[@]}" -eq 0 && "$ARG" =~ ^-(N|name|names-only)$ ]]; then
if [[ "$ARG" =~ ^-(a|-active)$ ]]; then OUTPUT=(awk "'{print \$1;}'" \| sort --unique)
FILTER=(-v '\b0/0\b') elif [[ "${#OUTPUT[@]}" -eq 0 && "$ARG" =~ ^-Nx$ ]]; then
OUTPUT=(awk "'{print \$1;}'" \| sed -e "'s|^ix-||'" \| sort --unique)
elif [[ "${#OUTPUT[@]}" -eq 0 && "$ARG" =~ ^-(H|(no-?)(head(e(r(s)?)?)?))$ ]]; then
OUTPUT=('grep' '.')
elif [[ -z "${FILTER1[*]}" && "$ARG" =~ ^- ]]; then
if [[ "$ARG" =~ ^(-A|--all)$ ]]; then
FILTER1=('.')
elif [[ "$ARG" =~ ^-(a|-active)$ ]]; then
FILTER1=(-v '\b0/0\b')
elif [[ "$ARG" =~ ^-(r|-ready)$ ]]; then elif [[ "$ARG" =~ ^-(r|-ready)$ ]]; then
FILTER=('\b([1-9]\d*)/\1\b') FILTER1=('\b([1-9]\d*)/\1\b')
elif [[ "$ARG" =~ ^-(l|-limbo)$ ]]; then elif [[ "$ARG" =~ ^-(l|-limbo)$ ]]; then
FILTER=('\b([0-9]+)/(?!\1)([1-9][0-9]*)\b') FILTER1=('\b([0-9]+)/(?!\1)([1-9][0-9]*)\b')
elif [[ "$ARG" =~ ^-(s|-stop|-stopped)$ ]]; then elif [[ "$ARG" =~ ^-(s|-stop|-stopped)$ ]]; then
FILTER=('\b0/0\b') FILTER1=('\b0/0\b')
elif [[ "$ARG" =~ ^(-h|-?|/h|/?|--help)$ ]]; then elif [[ "$ARG" =~ ^(-h|-?|/h|/?|--help)$ ]]; then
printf '%s\n' \ printf '%s\n' \
'Usage:' \ 'Usage:' \
" $(basename "$0") <args>" \ " $(basename "$0") <args> [grep -P filter]" \
'' \ '' \
' Default behavior will list all apps' \ ' Default behavior will list all apps' \
'' \ '' \
'Arguments:' \ 'Arguments:' \
' -h | --help Display this help/usage message' \ ' -h | --help Display this help/usage message' \
'' \
'Filters:' \
' -A | --all (default) List all apps' \ ' -A | --all (default) List all apps' \
' -a | --active List all active (non-stopped) apps' \ ' -a | --active List all active (non-stopped) apps' \
' -r | --ready List all active apps in ready state' \ ' -r | --ready List all active apps in ready state' \
' -s | --stopped List all apps in stopped state' \ ' -s | --stopped List all apps in stopped state' \
' -l | --limbo List all active apps not in ready state' ' -l | --limbo List all active apps not in ready state' \
'' \
'Output:' \
' -N | --names-only App names only' \
' -Nx App names only with "ix-" prefix removed' \
' -H | --no-headers No headers'
exit 0 exit 0
elif [[ ! "$ARG" =~ ^(-A|--all)$ ]]; then else
printf '%s\n' \ printf '%s\n' \
"Invalid argument: $ARG" \ "Invalid argument: $ARG" \
>&2 >&2
exit 1 exit 1
fi fi
fi else
FILTER2=("${FILTER2[@]}" "$ARG")
fi
done
APPS="$(get-apps)" APPS="$(get-apps)"
HEADER="$(head -n1 <<<"$APPS")" HEADER="$(head -n1 <<<"$APPS")"
@ -58,8 +80,12 @@ APPS="$(
| join( "|" ) | join( "|" )
' '
)" <<<"$APPS" \ )" <<<"$APPS" \
| grep -P "${FILTER[@]}" | grep -P "${FILTER1[@]}" \
| grep -P "${FILTER2[@]:-.}"
)" )"
echo "$HEADER" if [[ "${#OUTPUT[@]}" -le 1 ]]; then
echo "$APPS" echo "$HEADER"
fi
echo "$APPS" \
| eval "${OUTPUT[*]:-grep .}"