90 lines
1.8 KiB
Bash
Executable File
90 lines
1.8 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
SCRIPT_PATH=$(realpath -s "${0}")
|
|
SCRIPT_DIR=$(dirname "$SCRIPT_PATH")
|
|
SCRIPT=$(basename "$SCRIPT_PATH")
|
|
|
|
usage() {
|
|
cat <<EOF
|
|
Usage: $SCRIPT [OPTIONS]...
|
|
|
|
List available DNS zones.
|
|
|
|
Options:
|
|
-c, --config path to config file
|
|
-j, --json print json format
|
|
-J, --json-pretty print pretty json format (implies -j)
|
|
-r, --raw print raw format
|
|
-h, --help print this help message
|
|
|
|
EOF
|
|
exit
|
|
}
|
|
|
|
config_file="/etc/dns-manager/config.sh"
|
|
json=false
|
|
json_pretty=false
|
|
raw=false
|
|
|
|
while [ -n "$1" ]; do
|
|
opt=$1
|
|
shift
|
|
case "$opt" in
|
|
-c|--config)
|
|
if [ -z "$1" ]; then
|
|
echo "$SCRIPT: missing argument to option -- '$opt'" >&2
|
|
exit 1
|
|
fi
|
|
config_file=$1
|
|
shift
|
|
;;
|
|
-h|--help)
|
|
usage
|
|
;;
|
|
-j|--json)
|
|
json=true
|
|
;;
|
|
-J|--json-pretty)
|
|
json=true
|
|
json_pretty=true
|
|
;;
|
|
-r|--raw)
|
|
raw=true
|
|
;;
|
|
-*)
|
|
echo "$SCRIPT: invalid option -- '$opt'" >&2
|
|
exit 1
|
|
;;
|
|
*)
|
|
echo "$SCRIPT: invalid argument -- '$opt'" >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
done
|
|
|
|
if $json && $raw; then
|
|
echo "$SCRIPT: invalid options: --raw and --json are mutually exclusive" >&2
|
|
exit 1
|
|
fi
|
|
|
|
source "$config_file" || exit 2
|
|
|
|
LIB_DIR=${LIB_DIR:-$SCRIPT_DIR/lib}
|
|
source "$LIB_DIR"/dns.sh || exit 3
|
|
source "$LIB_DIR"/output.sh || exit 3
|
|
|
|
zones_data=$(dns_zones) || exit 10
|
|
|
|
if $raw; then
|
|
"$JQ" --raw-output '.[] | "\(.zone)\t\(.view)\t\(.status)"' <<<"$zones_data"
|
|
elif $json; then
|
|
jq_opts=""
|
|
$json_pretty || jq_opts="--compact-output"
|
|
"$JQ" $jq_opts <<<"$zones_data"
|
|
else
|
|
while read -r zone view status; do
|
|
zone=$("$IDN2" --decode <<<"$zone")
|
|
echo "$zone$TAB$view$TAB$status"
|
|
done < <("$JQ" --raw-output '.[] | "\(.zone)\t\(.view)\t\(.status)"' <<<"$zones_data") | table_output "ZONE" "VIEW" "STATUS"
|
|
fi
|