]> git.ipfire.org Git - people/stevee/pypdns.git/blobdiff - cli.py
Code rework of the CLI.
[people/stevee/pypdns.git] / cli.py
diff --git a/cli.py b/cli.py
index 699d75553bd735625290146f62366df153bc2d96..ad7a7e621c4ba3b0be0bc45f938d7d223012201c 100644 (file)
--- a/cli.py
+++ b/cli.py
@@ -24,19 +24,18 @@ import backend
 import argparse
 
 from i18n import _
-
-# Initialize DNS module.
-dns = backend.DNS(backend.DB)
+from backend import *
 
 # Create main class for the CLI.
 class Cli(object):
        def __init__(self):
                self.parser = argparse.ArgumentParser(
-                       description = _("CLI tool to adminstrate authoritative PowerDNS servers."),
-               )
+                       description = _("CLI tool to adminstrate authoritative \
+                               PowerDNS servers."))
 
                # Add entry for version displaying.
-               self.parser.add_argument('--version', action='version', version='pyPDNS 0.1.1')
+               self.parser.add_argument('--version', action='version', 
+                       version='pyPDNS 0.1.1')
 
                # Add sub-commands.
                self.sub_commands = self.parser.add_subparsers()
@@ -45,7 +44,8 @@ class Cli(object):
                #self.parse_command_add()
                #self.parse_command_remove()
 
-               # Finally parse all arguments from the command line and save them.
+               # Finally parse all arguments from the command line and save
+               # them.
                self.args = self.parser.parse_args()
 
                # Map return values from action to functions to proceed.
@@ -66,121 +66,151 @@ class Cli(object):
                # Impement "show all".
                sub_show_records = sub_show_subcommands.add_parser("all",
                        help="Show all domains and records.")
-               sub_show_records.add_argument("action", action="store_const", const="show_all")
+               sub_show_records.add_argument("action", action="store_const", 
+                       const="show_all")
 
                # Implement "show records" and pick up given domain.
                sub_show_records = sub_show_subcommands.add_parser("records",
                        help="Show records from a given domain.")
                sub_show_records.add_argument("domain", nargs="?",
                        help="Show records from a given domain.")
-               sub_show_records.add_argument("action", action="store_const", const="show_records")
+               sub_show_records.add_argument("action", action="store_const",
+                       const="show_records")
 
                # Implement "show domains".
                sub_show_domains = sub_show_subcommands.add_parser("domains",
                        help="Show all configured domains.")
-               sub_show_domains.add_argument("action", action="store_const", const="show_domains")
+               sub_show_domains.add_argument("action", action="store_const",
+                       const="show_domains")
 
        def run(self):
                action = self.args.action
 
                try:
                        func = self.action2func[action]
+
                except KeyError:
                        raise Exception, "Unhandled action: %s" % action
 
+               else:
+                       # Initialize DNS module.
+                       self.dns = DNS(DB)
+
                return func()
 
        def handle_show_domains(self):
-               # Get all domains and print them
-               domains = dns.get_domains()
-               if domains:
+               """
+               Get all domains and print them.
+               """
+               domains = self.dns.get_domains()
+
+               # Print the domains, if there are any configured.
+               if not domains:
+                       print "No domain configured, yet."
+
+               else:
+                       print "Currently configured domains:"
                        for domain in domains:
-                               print domain.id, domain.name
+                               print " - %s" % domain.name
                
-               # If there hasn't been any domain configured, print an error message.
-               else:
-                       print "No domain configured, yet."
 
        def handle_show_records(self):
-               if self.args.domain:
-                       # Check if the given domain name really exists.
-                       domain = dns.get_domain(self.args.domain)
-                       if domain:
-                               # Get all records and print them.
-                               if domain.records:
-                                       print " Showing records for: %s \n" % self.args.domain
-
-                                       soa = domain.SOA
-
-                                       if soa:
-                                               FORMAT = " %-8s: %s"
-                                               print FORMAT % ("MName", soa.mname)
-                                               print FORMAT % ("E-Mail",soa.email)
-                                               print FORMAT % ("Serial", soa.serial)
-                                               print FORMAT % ("Refresh", soa.refresh)
-                                               print FORMAT % ("Retry", soa.retry)
-                                               print FORMAT % ("Expire", soa.expire)
-                                               print FORMAT % ("Minimum", soa.minimum)
-
-                                               print "" #Just an emtpy line.
-                                       else:
-                                               print " No Start of Authority (SOA record) created yet. Your domain will not work correctly. \n"
+                       # Print message if domain doens't exist.
+                       if not self.args.domain:
+                               print " No domain name specified to show \
+                                       records."
+                               print " Use 'pdns show domains' to get a list \
+                                       from all available domains."
+       
+                               return
+
+                       # Print message if domain doens't exist.
+                       domain = self.dns.get_domain(self.args.domain)
+
+                       if not domain:
+                               print " No such domain, use 'pdns show \
+                                       domains' to get a list of all \
+                                       available domains."
+
+                               return
+
+                       # Print message if no records have been configured yet.
+                       if not domain.has_records():
+                               print " Domain has no records yet."
+
+                       else:
+                               print " Showing records \
+                                       for: %s \n" % self.args.domain
 
+                               soa = domain.SOA
+
+                               # Check if the domain has a SOA record.
+                               if not soa:
+                                       print " No Start of Authority \
+                                               (SOA record) created yet. \
+                                               Your domain will not work \
+                                               correctly. \n"
+
+                               else:
+                                       # Define table layout for SOA table..
+                                       FORMAT = " %-8s: %s"
+                                       print FORMAT % ("MName", soa.mname)
+                                       print FORMAT % ("E-Mail",soa.email)
+                                       print FORMAT % ("Serial", soa.serial)
+                                       print FORMAT % ("Refresh", soa.refresh)
+                                       print FORMAT % ("Retry", soa.retry)
+                                       print FORMAT % ("Expire", soa.expire)
+                                       print FORMAT % ("Minimum", soa.minimum)
+
+                                       print "" #Just an emtpy line.
                                        # Define layout for records table.
                                        FORMAT = " %-24s %-8s %-34s %-10s"
 
-                                       # Define tables headline for records and print it.
+                                       # Define tables headline for records
+                                       # and print it.
                                        title = FORMAT % (_("Name"), _("Type"), _("Content"), _("TTL"))
                                        print title
                                        print "=" * len(title) # spacing line
 
-                                       # Display all remaining records, except SOA records.
+                                       # Display all remaining records,
+                                       # except SOA records.
                                        for record in domain.records:
                                                if not record.type == "SOA":
                                                        print FORMAT % (record.dnsname, record.type, record.content, record.ttl)
 
-                               # If there aren't any records yet, print a short notice about that.
-                               else:
-                                       print " Domain has no records, yet."
 
-                       # If given domain doesn't exist, print an error message.
-                       else:
-                               print " Invalid domain. Use 'pdns show domains' to get a list from all available domains."
-
-               # If no domain name has been specified, also print an error message.
-               else:
-                       print " No domain name specified to show records."
-                       print " Use 'pdns show domains' to get a list from all available domains."
-       
        def handle_show_all(self):
                # Get all domains and print them
-                domains = dns.get_domains()
-                if domains:
-                        for domain in domains:
-                                print domain.name
-                               print "=" * 80
-
-                               # Print all records.
-                               if domain.records:
-                                       for record in domain.records:
-                                               if record.type == "SOA":
-                                                       FORMAT = ("%-30s %s")
-                                                       print FORMAT % (record.dnsname, record.type)
-                                               else:
-                                                       FORMAT = ("%-30s %-6s %s")
-                                                       print FORMAT % (record.dnsname, record.type, record.content)
-                               # Print a notice if the domain hasn't any records yet.
-                                else:
-                                        print "Domain has no records, yet."
-
-                               # Just an emty line.
-                               print ""
-
-                # If there hasn't been any domain configured, print an error message.
-                else:
-                        print "No domain configured, yet."
+                domains = self.dns.get_domains()
+
+               # Print message if no domain has been configured.
+               if not domains:
+                       print "No domain configured, yet."
+
+                       return
+
+               for domain in domains:
+                       print domain.name
+
+                       # Spacing line.
+                       print "=" * 80
+
+                       # Print a message if domain has no records.
+                       if not domain.has_records():
+                               print "Domain has no records yet."
+
+                       # Print records.
+                       for record in domain.records:
+                               if record.type == "SOA":
+                                       FORMAT = ("%-30s %s")
+                                       print FORMAT % (record.dnsname, record.type)
+                               else:
+                                       FORMAT = ("%-30s %-6s %s")
+                                       print FORMAT % (record.dnsname, record.type, record.content)
 
+                       # Just an emty line.
+                       print ""