#!/bin/bash
INPUT="$1"
RECURSIVE="$2"

if [[ -f "$INPUT" ]]; then
    INPUT=$(cat "$INPUT")
fi

OUTPUT=$(echo "$INPUT" | yq e 'del(.. | select(length == 0))')
if [[ "$RECURSIVE" == "true" ]]; then
    while [ "$(echo "$OUTPUT" | yq e 'map (.. | select (length == 0)) | any')" = "true" ]; do
        OUTPUT=$(echo "$OUTPUT" | yq e 'del (.. | select (length == 0))')
    done
fi

echo "$OUTPUT"
