]> git.ipfire.org Git - dbl.git/commitdiff
dnsbl: Create a convenience function to gracefully terminate the program
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 5 Jan 2026 10:16:49 +0000 (10:16 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 5 Jan 2026 10:16:49 +0000 (10:16 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/scripts/dnsbl.in

index 54e7e7e819e181a564a7c4af0a16cecfdd3c8f4e..c8a88bc286a5359441b708120186a509d69ecb51 100644 (file)
@@ -236,6 +236,18 @@ class CLI(object):
                # Otherwise just exit
                sys.exit(0)
 
+       @staticmethod
+       def terminate(message, code=2):
+               """
+                       Convenience function to terminate the program gracefully with a message
+               """
+               # Print the message
+               if message:
+                       sys.stderr.write("%s\n" % message)
+
+               # Terminate with the given code
+               raise SystemExit(code)
+
        def __get_list(self, backend, slug):
                """
                        Fetches a list or terminates the program if we could not find the list.
@@ -245,8 +257,7 @@ class CLI(object):
 
                # Terminate because we could not find the list
                if not list:
-                       sys.stderr.write("Could not find list '%s'\n" % slug)
-                       raise SystemExit(2)
+                       self.terminate("Could not find list '%s'" % slug)
 
                return list
 
@@ -606,8 +617,7 @@ class CLI(object):
 
                # If we could not find a key, we cannot delete it
                if not key:
-                       print("Could not find key %s" % args.key)
-                       return 2
+                       self.terminate("Could not find key %s" % args.key)
 
                # Delete the key
                key.delete(deleted_by=args.deleted_by)
@@ -622,8 +632,7 @@ class CLI(object):
 
                # Fail if we cannot find the report
                if not report:
-                       print("Could not find report %s" % args.id)
-                       return 2
+                       self.terminate("Could not find report %s" % args.id)
 
                # Close the report
                report.close(