Browse Source

cmd_tls: implemented writing tls config to specific file path

Daniel-Constantin Mierla 7 years ago
parent
commit
ccce8f8b12
1 changed files with 25 additions and 2 deletions
  1. 25 2
      kamcli/commands/cmd_tls.py

+ 25 - 2
kamcli/commands/cmd_tls.py

@@ -1,3 +1,5 @@
+import sys
+import os
 import click
 from sqlalchemy import create_engine
 from kamcli.ioutils import ioutils_dbres_print
@@ -40,9 +42,11 @@ def tls_showdb(ctx, oformat, ostyle):
 #
 #
 @cli.command('cfgprint', short_help='Print TLS config generated from database records')
[email protected]('cfgpath', nargs=-1, metavar='[<cfgpath>]')
[email protected]('odir', '--odir', '-d',
+                default=None, help='Output directory path for certificates content')
[email protected]('cfgpath', nargs=-1, metavar='[<cfgpath>]', type=click.Path())
 @pass_context
-def tls_cfgprint(ctx, cfgpath):
+def tls_cfgprint(ctx, odir, cfgpath):
     """Print TLS config generated from database records
 
     \b
@@ -51,6 +55,19 @@ def tls_cfgprint(ctx, cfgpath):
     e = create_engine(ctx.gconfig.get('db', 'rwurl'))
     ctx.vlog('Generating TLS config from database records')
     res = e.execute('select * from tlscfg')
+
+    if cfgpath:
+        cfgpath = cfgpath[0]
+
+    if not odir:
+        if cfgpath:
+            odir = os.path.dirname(str(cfgpath))
+
+    bstdout = sys.stdout
+    if cfgpath:
+        cfgsock = open(str(cfgpath), 'w')
+        sys.stdout = cfgsock
+
     pcount = 0
     for row in res:
         if pcount > 0:
@@ -91,6 +108,12 @@ def tls_cfgprint(ctx, cfgpath):
 
         pcount += 1
 
+    if cfgpath:
+        sys.stdout = bstdout
+        cfgsock.close()
+
+
+
 ##
 #
 #