From 58ed888d6355ff8d1a9c9ceb950ad95676f9b1d1 Mon Sep 17 00:00:00 2001 From: Daniel Berteaud Date: Sun, 16 Dec 2018 10:12:04 +0100 Subject: [PATCH] Validate JSON when quiting the editor --- bin/var_editor.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/bin/var_editor.py b/bin/var_editor.py index 72a5935..e9110ec 100755 --- a/bin/var_editor.py +++ b/bin/var_editor.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -import os, sys, tempfile,getopt +import os, sys, tempfile, getopt, json from cryptography.fernet import Fernet def main(): @@ -36,11 +36,19 @@ def main(): clear = f.decrypt(crypt) tmp.write(clear) tmp.flush() - # Now, lets open our favorite editor to edit the file - os.system(os.getenv('EDITOR', 'vim') + ' ' + tmp.name) - # We closed the editor, we just have to open the cleartext file, encrypt its content - # and save it - clear = open(tmp.name, 'rb').read() + loop = 1 + while loop == 1: + # Now, lets open our favorite editor to edit the file + os.system(os.getenv('EDITOR', 'vim') + ' ' + tmp.name) + # We closed the editor, we just have to open the cleartext file, encrypt its content + # and save it + clear = open(tmp.name, 'rb').read() + loop = 0 + # Validate JSON data. Edit agin if not valid + try: + json.loads(clear) + except: + loop = 1 wcrypt = open(args[0], 'wb') wcrypt.write(f.encrypt(clear)) wcrypt.flush()