acmecert: Make error reporting more flexible.
authorFredrik Tolf <fredrik@dolda2000.com>
Fri, 12 Nov 2021 17:30:57 +0000 (18:30 +0100)
committerFredrik Tolf <fredrik@dolda2000.com>
Fri, 12 Nov 2021 17:30:57 +0000 (18:30 +0100)
acmecert

index 0ba66e2..f6f60e2 100755 (executable)
--- a/acmecert
+++ b/acmecert
@@ -1,9 +1,13 @@
 #!/usr/bin/python3
 
-import sys, os, getopt, binascii, json, pprint, signal, time
+import sys, os, getopt, binascii, json, pprint, signal, time, threading
 import urllib.request
 import Crypto.PublicKey.RSA, Crypto.Random, Crypto.Hash.SHA256, Crypto.Signature.PKCS1_v1_5
 
+class msgerror(Exception):
+    def report(self, out):
+        out.write("acmecert: undefined error\n")
+
 service = "https://acme-v02.api.letsencrypt.org/directory"
 _directory = None
 def directory():
@@ -308,11 +312,16 @@ class maybeopen(object):
             self.fp.close()
         return False
 
-class usageerr(Exception):
-    pass
-
+invdata = threading.local()
 commands = {}
 
+class usageerr(msgerror):
+    def __init__(self):
+        self.cmd = invdata.cmd
+
+    def report(self, out):
+        out.write("%s\n" % (self.cmd.__doc__,))
+
 def cmd_reg(args):
     "usage: acmecert reg [OUTPUT-FILE]"
     acct = register()
@@ -447,9 +456,13 @@ def main(argv):
         usage(sys.stderr)
         sys.exit(1)
     try:
-        cmd(args)
-    except usageerr:
-        sys.stderr.write("%s\n" % (cmd.__doc__,))
+        try:
+            invdata.cmd = cmd
+            cmd(args)
+        finally:
+            invdata.cmd = None
+    except msgerror as exc:
+        exc.report(sys.stderr)
         sys.exit(1)
 
 if __name__ == "__main__":