cvs export option

This commit is contained in:
Matthew Saunders Brown 2024-02-06 10:33:18 -08:00
parent 9f46179174
commit e459e1a512

View File

@ -18,6 +18,7 @@ help()
echo "" echo ""
echo " -h Print this help." echo " -h Print this help."
echo " -z <zone> Zone to export." echo " -z <zone> Zone to export."
echo " -c Output in csv format instead of columns."
} }
pdns:getoptions "$@" pdns:getoptions "$@"
@ -29,19 +30,30 @@ if [[ -z $zone ]]; then
fi fi
tmpfile=$(mktemp) tmpfile=$(mktemp)
echo "name,ttl,class,type,value" > $tmpfile.csv
# export zone and check http status # export zone and check http status
zone_status=$(/usr/bin/curl --silent --output "$tmpfile" --write-out "%{http_code}" -H "X-API-Key: $api_key" $api_base_url/zones/$zone/export) zone_status=$(/usr/bin/curl --silent --output "$tmpfile" --write-out "%{http_code}" -H "X-API-Key: $api_key" $api_base_url/zones/$zone/export)
if [[ $zone_status = 200 ]]; then if [[ $zone_status = 200 ]]; then
# return zone level records
sed -e 's/\t/|/g' $tmpfile|column -t -s \| |grep ^$zone. # convert tabs to commas
# return subdomain records sed -i 's/\t/,/g' $tmpfile
sed -e 's/\t/|/g' $tmpfile|column -t -s \| |grep -v ^$zone. # zone level records
grep ^$zone. $tmpfile >> $tmpfile.csv
# subdomain records
grep -v ^$zone. $tmpfile >> $tmpfile.csv
if [[ $csv ]]; then
cat $tmpfile.csv
else
column -t -s , $tmpfile.csv
fi
elif [[ $zone_status = 404 ]]; then elif [[ $zone_status = 404 ]]; then
echo 404 Not Found, $zone does not exist echo 404 Not Found, $zone does not exist
else else
echo Unexecpted http response checking for existence of zone $zone: $zone_status echo Unexecpted http response checking for existence of zone $zone: $zone_status
fi fi
rm $tmpfile.csv
rm $tmpfile rm $tmpfile