From 39da32b3a5d31a022e958c7b2ca752a1b6fbd3a2 Mon Sep 17 00:00:00 2001
From: Ken Thomas <krt@yahoo-inc.com>
Date: Fri, 29 Jun 2012 22:29:46 +0000
Subject: [PATCH] If no password in env or command line, try prompting.

Implements blueprint password-prompt

This logic was recently added to keystone as a short term fix. The long
term fix is to have that same logic here. Basically, if no password is
present in the env or command line and there's a tty available for us
to prompt, then attempt to use getpass.

Change-Id: Ia0eec800b96c8f6ca1c2540e21e0e03c3880c713
---
 openstackclient/shell.py | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/openstackclient/shell.py b/openstackclient/shell.py
index 14b4f80ac6..3d0adf9937 100644
--- a/openstackclient/shell.py
+++ b/openstackclient/shell.py
@@ -19,6 +19,7 @@
 Command-line interface to the OpenStack APIs
 """
 
+import getpass
 import logging
 import os
 import sys
@@ -149,9 +150,20 @@ class OpenStackShell(App):
                     " either --os-username or env[OS_USERNAME]")
 
             if not self.options.os_password:
-                raise exc.CommandError(
-                    "You must provide a password via"
-                    " either --os-password or env[OS_PASSWORD]")
+                # No password, if we've got a tty, try prompting for it
+                if hasattr(sys.stdin, 'isatty') and sys.stdin.isatty():
+                    # Check for Ctl-D
+                    try:
+                        self.options.os_password = getpass.getpass()
+                    except EOFError:
+                        pass
+                # No password because we did't have a tty or the
+                # user Ctl-D when prompted?
+                if not self.options.os_password:
+                    raise exc.CommandError(
+                        "You must provide a password via"
+                        " either --os-password, or env[OS_PASSWORD], "
+                        " or prompted response")
 
             if not (self.options.os_tenant_id or self.options.os_tenant_name):
                 raise exc.CommandError(