From: Michael Tremer Date: Mon, 5 Jan 2026 10:16:49 +0000 (+0000) Subject: dnsbl: Create a convenience function to gracefully terminate the program X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4812cac7b22adeb66dbc6ba82c898473cdcaf3bd;p=dbl.git dnsbl: Create a convenience function to gracefully terminate the program Signed-off-by: Michael Tremer --- diff --git a/src/scripts/dnsbl.in b/src/scripts/dnsbl.in index 54e7e7e..c8a88bc 100644 --- a/src/scripts/dnsbl.in +++ b/src/scripts/dnsbl.in @@ -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(