Browse Source

refactor(tools): Use argparse for the certificate creation script

Julius Pfrommer 5 years ago
parent
commit
89145733bf
1 changed files with 26 additions and 8 deletions
  1. 26 8
      tools/certs/create_self-signed.py

+ 26 - 8
tools/certs/create_self-signed.py

@@ -10,17 +10,35 @@ import netifaces
 import sys
 import sys
 import os
 import os
 import socket
 import socket
+import argparse
 
 
-if len(sys.argv) < 2:
-    sys.exit('Usage: %s directory to output certificates' % sys.argv[0])
+parser = argparse.ArgumentParser()
 
 
-if not os.path.exists(sys.argv[1]):
-    sys.exit('ERROR: Directory %s was not found!' % sys.argv[1])
+parser.add_argument('outdir',
+                    type=str,
+                    nargs='?',
+                    default=os.getcwd(),
+                    metavar='<OutputDirectory>')
+
+parser.add_argument('-u', '--uri',
+                    metavar="<ApplicationUri>",
+                    type=str,
+                    dest="uri")
+
+parser.add_argument('-k', '--keysize',
+                    metavar="<KeySize>",
+                    type=int,
+                    dest="keysize")
+
+args = parser.parse_args()
+
+if not os.path.exists(args.outdir):
+    sys.exit('ERROR: Directory %s was not found!' % args.outdir)
 
 
 keysize = 2048
 keysize = 2048
 
 
-if len(sys.argv) == 3:
-    keysize = int(sys.argv[2])
+if args.keysize:
+    keysize = args.keysize
 
 
 certsdir = os.path.dirname(os.path.abspath(__file__))
 certsdir = os.path.dirname(os.path.abspath(__file__))
 print(certsdir)
 print(certsdir)
@@ -64,7 +82,7 @@ if iteratorValue < 2:
 os.environ['HOSTNAME'] = socket.gethostname()
 os.environ['HOSTNAME'] = socket.gethostname()
 openssl_conf = os.path.join(certsdir, "localhost.cnf")
 openssl_conf = os.path.join(certsdir, "localhost.cnf")
 
 
-os.chdir(os.path.abspath(sys.argv[1]))
+os.chdir(os.path.abspath(args.outdir))
 
 
 os.system("""openssl req \
 os.system("""openssl req \
      -config {} \
      -config {} \
@@ -82,4 +100,4 @@ os.system("openssl rsa -inform PEM -in localhost.key -outform DER -out server_ke
 os.remove("localhost.key")
 os.remove("localhost.key")
 os.remove("localhost.crt")
 os.remove("localhost.crt")
 
 
-print("Certificates generated in " + sys.argv[1])
+print("Certificates generated in " + args.outdir)