From 37cba098ed801d40021497b46d4a4f2c82df67d5 Mon Sep 17 00:00:00 2001
From: Maxime Bombar <bombar@crans.org>
Date: Mon, 21 Sep 2020 02:03:19 +0200
Subject: [PATCH] Minimal cpasswords, to be used in external scripts.

---
 cpasswords/client.py | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)

diff --git a/cpasswords/client.py b/cpasswords/client.py
index d214f49..eb7eeda 100755
--- a/cpasswords/client.py
+++ b/cpasswords/client.py
@@ -447,6 +447,41 @@ def show_file(options):
     if old_clipboard is not None:
         saveclipboard(restore=True, old_clipboard=old_clipboard)
 
+@need_filename
+def show_minimal(options):
+    """
+    Action that decrypt file content and only prints the password
+    """
+    fname = options.filename
+    gotit, value = get_file(options, fname)
+    if not gotit:
+        log.warn(value)  # value contient le message d'erreur
+        return
+    passfile = value
+    content = passfile['contents']
+
+    # Kludge (broken db ?)
+    if type(content) == list:
+        log.warn("Eau dans le gaz")
+        content = content[-1]
+
+    # Déchiffre le contenu
+    texte = decrypt(content)
+
+    found = None
+
+    # Essaie de planquer le mot de passe
+    for line in texte.split('\n'):
+        catch_pass = None
+
+        if not found:
+            catch_pass = pass_regexp.match(line)
+
+        if catch_pass is not None:
+            found = True
+            passwd = catch_pass.group(1)
+            break
+    print(passwd)
 
 @need_filename
 def edit_file(options):
@@ -816,6 +851,13 @@ def main():
         const=recrypt_files,
         help=_("recrypt all files having a role listed in --roles"),
     )
+    action_grp.add_argument(
+        '--minimal',
+        action='store_const',
+        dest='action',
+        const=show_minimal,
+        help=_("just print the password in a file, for use in an external script."),
+    )
     action_grp.set_defaults(action=show_file)
 
     # On parse les options fournies en commandline
-- 
GitLab