Rename dns-list to dns-list-zones
This commit is contained in:
71
dns-list-zones
Executable file
71
dns-list-zones
Executable file
@@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import argparse
|
||||
import dnsmgr
|
||||
import sys
|
||||
|
||||
from json import dumps
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(
|
||||
description='List DNS zones.',
|
||||
formatter_class=lambda prog: argparse.HelpFormatter(
|
||||
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)
|
||||
|
||||
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')
|
||||
output.add_argument('-r', '--raw', help='print raw format', action='store_true')
|
||||
args = parser.parse_args()
|
||||
|
||||
try:
|
||||
manager = dnsmgr.DNSManager(cfgfile=args.config)
|
||||
except RuntimeError as e:
|
||||
dnsmgr.printe(f'config: {e}')
|
||||
sys.exit(100)
|
||||
|
||||
try:
|
||||
zones = manager.all_zones if args.all_zones else manager.zones
|
||||
except RuntimeError as e:
|
||||
dnsmgr.printe(e)
|
||||
sys.exit(150)
|
||||
|
||||
zones.sort(key=lambda z: z.origin.to_text())
|
||||
|
||||
if args.raw:
|
||||
for zone in zones:
|
||||
name = zone.origin.to_text()
|
||||
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(),
|
||||
'view': zone.view,
|
||||
'status': zone.status,
|
||||
'managed': zone.cfgfile is not None} for zone in zones]
|
||||
if args.json_pretty:
|
||||
print(dumps(json_output, indent=2))
|
||||
else:
|
||||
print(dumps(json_output))
|
||||
|
||||
else:
|
||||
field_names = ['Zone', 'View', 'Status']
|
||||
if args.all_zones:
|
||||
field_names.append('Managed')
|
||||
|
||||
rows = []
|
||||
for zone in zones:
|
||||
name = zone.origin.to_unicode(omit_final_dot=True)
|
||||
row = [name, zone.view, zone.status]
|
||||
if args.all_zones:
|
||||
row.append(zone.cfgfile is not None)
|
||||
rows.append(row)
|
||||
print(dnsmgr.prettytable(field_names, rows))
|
||||
print()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user