Skip to content

Commit 92287eb

Browse files
committed
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.
1 parent 909f9ae commit 92287eb

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

lib/aptible/cli/agent.rb

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,17 @@ def login
9292
if options[:sso]
9393
begin
9494
token = options[:sso]
95-
token = ask('Paste token copied from Dashboard:') if token == 'sso'
95+
if token == 'sso'
96+
browser_open_url('https://dashboard.aptible.com/settings/cli-sso-token')
97+
sleep 0.5
98+
token = ask('Paste token copied from Dashboard:')
99+
end
96100
Base64.urlsafe_decode64(token.split('.').first)
97101
save_token(token)
98102
CLI.logger.info "Token written to #{token_file}"
99103
return
100104
rescue StandardError
105+
raise # TODO DEBUG
101106
raise Thor::Error, 'Invalid token provided for SSO'
102107
end
103108
end
@@ -258,6 +263,27 @@ def version_string
258263
def toolbelt?
259264
ENV['APTIBLE_TOOLBELT']
260265
end
266+
267+
# Open the specified URL in the default system web browser
268+
def browser_open_url(url)
269+
case RbConfig::CONFIG.fetch('host_os')
270+
when /darwin/
271+
cmd = 'open'
272+
when /linux|bsd/
273+
cmd = 'xdg-open'
274+
when /mswin|mingw|cygwin/
275+
cmd = 'start'
276+
else
277+
CLI.logger.warn("Unknown OS: #{RbConfig::CONFIG.fetch('host_os').inspect}")
278+
cmd = 'open'
279+
end
280+
281+
if system([cmd, cmd], url)
282+
CLI.logger.info('Opening browser window...')
283+
else
284+
CLI.logger.warn("Failed to open in browser: #{url.inspect}")
285+
end
286+
end
261287
end
262288
end
263289
end

0 commit comments

Comments
 (0)