Fix output of dns-list-zones and dns-zone-list

This commit is contained in:
2025-09-01 16:58:14 +02:00
parent d163a82e0e
commit fd33964168
2 changed files with 17 additions and 10 deletions

View File

@@ -14,7 +14,7 @@ def main():
prog, max_help_position=45, width=140))
parser.add_argument('-a', '--all-zones', help='do not ignore zones that are not managed', action='store_true')
parser.add_argument('-c', '--config', help='path to config file', default=dnsmgr.DEFAULT_CFGFILE)
parser.add_argument('-d', '--decode', help='decode internationalized domain names (IDN)', action='store_true')
output = parser.add_mutually_exclusive_group()
output.add_argument('-j', '--json', help='print json format', action='store_true')
output.add_argument('-J', '--json-pretty', help='print pretty json format', action='store_true')
@@ -33,16 +33,16 @@ def main():
dnsmgr.printe(e)
sys.exit(150)
zones.sort(key=lambda z: z.origin.to_text())
zones.sort(key=lambda zone: zone.origin.to_unicode() if args.decode else zone.origin.to_text())
if args.raw:
for zone in zones:
name = zone.origin.to_text()
name = zone.origin.to_unicode(True) if args.decode else zone.origin.to_text(True)
managed = zone.cfgfile is not None
print(f'{name}\t{zone.view}\t{zone.status}\t{managed}')
elif args.json or args.json_pretty:
json_output = [{
'zone': zone.origin.to_text(),
'zone': zone.origin.to_unicode(True) if args.decode else zone.origin.to_text(True),
'view': zone.view,
'status': zone.status,
'managed': zone.cfgfile is not None} for zone in zones]
@@ -58,7 +58,7 @@ def main():
rows = []
for zone in zones:
name = zone.origin.to_unicode(omit_final_dot=True)
name = zone.origin.to_unicode(True) if args.decode else zone.origin.to_text(True)
row = [name, zone.view, zone.status]
if args.all_zones:
row.append(zone.cfgfile is not None)

View File

@@ -2,6 +2,7 @@
import argparse
import dnsmgr
import re
import sys
from json import dumps
@@ -46,6 +47,9 @@ def main():
zones.sort(key=lambda zone: zone.view)
rev_domain = dnsmgr.name_from_text('in-addr.arpa')
rev_domain6 = dnsmgr.name_from_text('ip6.arpa')
zone_records = {}
for zone in zones:
try:
@@ -61,13 +65,17 @@ def main():
continue
for value in rdataset:
records.append({
'name': name.to_text(),
'name_unicode': name.to_unicode(),
'name': name.to_unicode() if args.decode else name.to_text(),
'ttl': str(rdataset.ttl),
'type': str(rdataset.rdtype.to_text(rdataset.rdtype)),
'value': value.to_text(origin=zone.origin, relativize=False)})
records.sort(key=lambda r: f'{r["name_unicode"]}{r["type"]}')
if zone.origin.is_subdomain(rev_domain):
records.sort(key=lambda r: int(re.findall(r'\d+|$', r['name'])[0] or 0))
elif zone.origin.is_subdomain(rev_domain6):
records.sort(key=lambda r: int(''.join(re.findall(r'\d+', r['name'])) or 0))
else:
records.sort(key=lambda r: f'{r["name"]}{r["type"]}')
zone_records[zone.view] = records
views = sorted(zone_records.keys())
@@ -92,8 +100,7 @@ def main():
for view in views:
rows = []
for record in zone_records[view]:
name = record['name_unicode'] if args.decode else record['name']
row = [name, record['ttl'], record['type'], record['value']]
row = [record['name'], record['ttl'], record['type'], record['value']]
rows.append(row)
if len(views) > 1 or view != dnsmgr.NAMED_DEFAULT_VIEW: