Save vars in yaml format

This commit is contained in:
Daniel Berteaud 2018-12-20 15:37:36 +01:00
parent 58ed888d63
commit f8a748415c
1 changed files with 10 additions and 5 deletions

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python3
import os, sys, tempfile, getopt, json
import os, sys, tempfile, getopt, json, yaml
from cryptography.fernet import Fernet
def main():
@ -28,12 +28,17 @@ def main():
# Create a fernet object using our key
f = Fernet(key)
# 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
if os.path.exists(args[0]):
crypt = open(args[0], 'rb').read()
# And we decrypt it, and write it in the temp file
clear = f.decrypt(crypt)
try:
convert = yaml.dump(json.loads(clear)).encode('utf-8')
clear = convert
except:
pass
tmp.write(clear)
tmp.flush()
loop = 1
@ -44,9 +49,9 @@ def main():
# and save it
clear = open(tmp.name, 'rb').read()
loop = 0
# Validate JSON data. Edit agin if not valid
# Validate YAML data. Edit agin if not valid
try:
json.loads(clear)
yaml.safe_load(clear)
except:
loop = 1
wcrypt = open(args[0], 'wb')
@ -55,7 +60,7 @@ def main():
wcrypt.close()
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__':
main()