From 6fdffadb86fe486505cc2a6776a66ba71749ec37 Mon Sep 17 00:00:00 2001 From: Marc Abramowitz Date: Wed, 1 Mar 2017 14:49:55 -0800 Subject: [PATCH] auth: automatically open browser for login (#918) --- dcos/auth.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/dcos/auth.py b/dcos/auth.py index 8ea92c0..272bba9 100644 --- a/dcos/auth.py +++ b/dcos/auth.py @@ -1,8 +1,8 @@ import getpass - import sys - +import textwrap import time +import webbrowser import jwt @@ -58,10 +58,22 @@ def _prompt_user_for_token(url, token_type): :rtype: str """ - msg = "\n{}\n\n {}\n\nEnter {}:".format( - "Please go to the following link in your browser:", - url, - token_type) + msg = textwrap.dedent("""\ + If your browser didn't open, please go to the following link: + + {url} + + Enter {token_type}: """) + msg = msg.lstrip().format(url=url, token_type=token_type) + + try: + webbrowser.open_new_tab(url) + except webbrowser.Error as exc: + logger.warning( + 'Exception occurred while calling webbrowser.open(%r): %s', + url, exc, + ) + pass sys.stderr.write(msg) sys.stderr.flush() token = sys.stdin.readline().strip()