summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbhijit Menon-Sen <ams@2ndQuadrant.com>2015-08-27 12:37:42 +0530
committerAbhijit Menon-Sen <ams@2ndQuadrant.com>2015-08-27 22:04:18 +0530
commit090cfc9e03f7e66c50581a67d09efa0d5ed2499b (patch)
tree6add6d9187e845da3bc2e6cc045dc40e272c6b00
parentb6de6e69a6dbae8e401786978f9c530270191537 (diff)
downloadansible-090cfc9e03f7e66c50581a67d09efa0d5ed2499b.tar.gz
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.)
-rw-r--r--lib/ansible/cli/vault.py12
-rw-r--r--lib/ansible/parsing/vault/__init__.py15
2 files changed, 17 insertions, 10 deletions
diff --git a/lib/ansible/cli/vault.py b/lib/ansible/cli/vault.py
index 086579c333..e9eee78121 100644
--- a/lib/ansible/cli/vault.py
+++ b/lib/ansible/cli/vault.py
@@ -102,17 +102,25 @@ class VaultCLI(CLI):
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 ['-']:
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):
+ 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 ['-']:
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):
diff --git a/lib/ansible/parsing/vault/__init__.py b/lib/ansible/parsing/vault/__init__.py
index e7c60611e8..7746a2de29 100644
--- a/lib/ansible/parsing/vault/__init__.py
+++ b/lib/ansible/parsing/vault/__init__.py
@@ -329,25 +329,24 @@ class VaultEditor:
def read_data(self, filename):
try:
if filename == '-':
- f = sys.stdin
+ data = sys.stdin.read()
else:
- f = open(filename, "rb")
- data = f.read()
- f.close()
+ with open(filename, "rb") as fh:
+ data = fh.read()
except Exception as e:
raise AnsibleError(str(e))
return data
def write_data(self, data, filename):
+ bytes = to_bytes(data, errors='strict')
if filename == '-':
- f = sys.stdout
+ sys.stdout.write(bytes)
else:
if os.path.isfile(filename):
os.remove(filename)
- f = open(filename, "wb")
- f.write(to_bytes(data, errors='strict'))
- f.close()
+ with open(filename, "wb") as fh:
+ fh.write(bytes)
def shuffle_files(self, src, dest):
# overwrite dest with src