Skip to content

Commit

Permalink
With aptible login --sso, open browser for token.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ab committed Jun 3, 2021
1 parent 909f9ae commit a3073a7
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion lib/aptible/cli/agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down Expand Up @@ -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

0 comments on commit a3073a7

Please sign in to comment.