Save vars in yaml format

master
Daniel Berteaud 6 years ago
parent 58ed888d63
commit f8a748415c
  1. 15
      bin/var_editor.py

@ -1,6 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os, sys, tempfile, getopt, json import os, sys, tempfile, getopt, json, yaml
from cryptography.fernet import Fernet from cryptography.fernet import Fernet
def main(): def main():
@ -28,12 +28,17 @@ def main():
# Create a fernet object using our key # Create a fernet object using our key
f = Fernet(key) f = Fernet(key)
# This temp file will hold the decrypted content while we edit it # This temp file will hold the decrypted content while we edit it
tmp = tempfile.NamedTemporaryFile() tmp = tempfile.NamedTemporaryFile(suffix='.yml')
# We open the file which contains the encrypted content, if it exists # We open the file which contains the encrypted content, if it exists
if os.path.exists(args[0]): if os.path.exists(args[0]):
crypt = open(args[0], 'rb').read() crypt = open(args[0], 'rb').read()
# And we decrypt it, and write it in the temp file # And we decrypt it, and write it in the temp file
clear = f.decrypt(crypt) clear = f.decrypt(crypt)
try:
convert = yaml.dump(json.loads(clear)).encode('utf-8')
clear = convert
except:
pass
tmp.write(clear) tmp.write(clear)
tmp.flush() tmp.flush()
loop = 1 loop = 1
@ -44,9 +49,9 @@ def main():
# and save it # and save it
clear = open(tmp.name, 'rb').read() clear = open(tmp.name, 'rb').read()
loop = 0 loop = 0
# Validate JSON data. Edit agin if not valid # Validate YAML data. Edit agin if not valid
try: try:
json.loads(clear) yaml.safe_load(clear)
except: except:
loop = 1 loop = 1
wcrypt = open(args[0], 'wb') wcrypt = open(args[0], 'wb')
@ -55,7 +60,7 @@ def main():
wcrypt.close() wcrypt.close()
def usage(): def usage():
print(argv[0] + ' [-k secret] [-K ./secret.txt] variables.json') print(argv[0] + ' [-k secret] [-K ./secret.txt] variables.txt')
if __name__ == '__main__': if __name__ == '__main__':
main() main()

Loading…
Cancel
Save