From a3073a7b504d94348e891a6c5a4ca8747f3cbf39 Mon Sep 17 00:00:00 2001 From: Andy Brody Date: Thu, 3 Jun 2021 18:47:52 -0400 Subject: [PATCH] With aptible login --sso, open browser for token. When a user runs `aptible login --sso` with no token provided, we need to prompt them for the token. And users will need to go to the cli-sso-token page, so we may as well send them directly to the right page. This matches the behavior of similar commands like `aws sso login` from the AWS CLI, which opens a browser window. --- lib/aptible/cli/agent.rb | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/aptible/cli/agent.rb b/lib/aptible/cli/agent.rb index d16f7b8e..af936ae4 100644 --- a/lib/aptible/cli/agent.rb +++ b/lib/aptible/cli/agent.rb @@ -92,7 +92,11 @@ def login if options[:sso] begin token = options[:sso] - token = ask('Paste token copied from Dashboard:') if token == 'sso' + if token == 'sso' + browser_open_url('https://dashboard.aptible.com/settings/cli-sso-token') + sleep 0.5 + token = ask('Paste token copied from Dashboard:') + end Base64.urlsafe_decode64(token.split('.').first) save_token(token) CLI.logger.info "Token written to #{token_file}" @@ -258,6 +262,27 @@ def version_string def toolbelt? ENV['APTIBLE_TOOLBELT'] end + + # Open the specified URL in the default system web browser + def browser_open_url(url) + case RbConfig::CONFIG.fetch('host_os') + when /darwin/ + cmd = 'open' + when /linux|bsd/ + cmd = 'xdg-open' + when /mswin|mingw|cygwin/ + cmd = 'start' + else + CLI.logger.warn("Unknown OS: #{RbConfig::CONFIG.fetch('host_os').inspect}") + cmd = 'open' + end + + if system([cmd, cmd], url) + CLI.logger.info('Opening browser window...') + else + CLI.logger.warn("Failed to open in browser: #{url.inspect}") + end + end end end end