1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

More helpful prompts from ansible-vault encrypt/decrypt

Now we issue a "Reading … from stdin" prompt if our input isatty(), as
gpg does. We also suppress the "x successful" confirmation message at
the end if we're part of a pipeline.

(The latter requires that we not close sys.stdout in VaultEditor, and
for symmetry we do the same for sys.stdin, though it doesn't matter in
that case.)
This commit is contained in:
Abhijit Menon-Sen 2015-08-27 12:37:42 +05:30
parent b6de6e69a6
commit 090cfc9e03
2 changed files with 17 additions and 10 deletions

View file

@ -102,17 +102,25 @@ class VaultCLI(CLI):
def execute_encrypt(self): def execute_encrypt(self):
if len(self.args) == 0 and sys.stdin.isatty():
self.display.display("Reading plaintext input from stdin", stderr=True)
for f in self.args or ['-']: for f in self.args or ['-']:
self.editor.encrypt_file(f, output_file=self.options.output_file) self.editor.encrypt_file(f, output_file=self.options.output_file)
self.display.display("Encryption successful", stderr=True) if sys.stdout.isatty():
self.display.display("Encryption successful", stderr=True)
def execute_decrypt(self): def execute_decrypt(self):
if len(self.args) == 0 and sys.stdin.isatty():
self.display.display("Reading ciphertext input from stdin", stderr=True)
for f in self.args or ['-']: for f in self.args or ['-']:
self.editor.decrypt_file(f, output_file=self.options.output_file) self.editor.decrypt_file(f, output_file=self.options.output_file)
self.display.display("Decryption successful", stderr=True) if sys.stdout.isatty():
self.display.display("Decryption successful", stderr=True)
def execute_create(self): def execute_create(self):

View file

@ -329,25 +329,24 @@ class VaultEditor:
def read_data(self, filename): def read_data(self, filename):
try: try:
if filename == '-': if filename == '-':
f = sys.stdin data = sys.stdin.read()
else: else:
f = open(filename, "rb") with open(filename, "rb") as fh:
data = f.read() data = fh.read()
f.close()
except Exception as e: except Exception as e:
raise AnsibleError(str(e)) raise AnsibleError(str(e))
return data return data
def write_data(self, data, filename): def write_data(self, data, filename):
bytes = to_bytes(data, errors='strict')
if filename == '-': if filename == '-':
f = sys.stdout sys.stdout.write(bytes)
else: else:
if os.path.isfile(filename): if os.path.isfile(filename):
os.remove(filename) os.remove(filename)
f = open(filename, "wb") with open(filename, "wb") as fh:
f.write(to_bytes(data, errors='strict')) fh.write(bytes)
f.close()
def shuffle_files(self, src, dest): def shuffle_files(self, src, dest):
# overwrite dest with src # overwrite dest with src