command/login: Store user/pass on client

Will allow us to prompt for other info without forgetting this data.
This commit is contained in:
Justin Campbell 2017-08-30 10:00:52 -04:00
parent 9a992ffe17
commit 0e091fee16
2 changed files with 31 additions and 16 deletions

View File

@ -7,6 +7,9 @@ module VagrantPlugins
class Client
include Vagrant::Util::Presence
attr_accessor :username_or_email
attr_accessor :password
# Initializes a login client with the given Vagrant::Environment.
#
# @param [Vagrant::Environment] env
@ -40,11 +43,9 @@ module VagrantPlugins
# Login logs a user in and returns the token for that user. The token
# is _not_ stored unless {#store_token} is called.
#
# @param [String] username_or_email
# @param [String] password
# @param [String] description
# @return [String] token The access token, or nil if auth failed.
def login(username_or_email, password, description: nil)
def login(description: nil)
@logger.info("Logging in '#{username_or_email}'")
with_error_handling do

View File

@ -17,6 +17,10 @@ module VagrantPlugins
options[:check] = c
end
o.on("-d", "--description DESCRIPTION", String, "Description for the Vagrant Cloud token") do |t|
options[:description] = t
end
o.on("-k", "--logout", "Logs you out if you're logged in") do |k|
options[:logout] = k
end
@ -24,6 +28,10 @@ module VagrantPlugins
o.on("-t", "--token TOKEN", String, "Set the Vagrant Cloud token") do |t|
options[:token] = t
end
o.on("-u", "--username USERNAME_OR_EMAIL", String, "Specify your Vagrant Cloud username or email address") do |t|
options[:login] = t
end
end
# Parse the options
@ -31,6 +39,7 @@ module VagrantPlugins
return if !argv
@client = Client.new(@env)
@client.username_or_email = options[:login]
# Determine what task we're actually taking based on flags
if options[:check]
@ -50,25 +59,30 @@ module VagrantPlugins
end
# Ask for the username
login = nil
password = nil
description = nil
while !login
login = @env.ui.ask("Vagrant Cloud Username: ")
if @client.username_or_email
@env.ui.output("Vagrant Cloud username or email: #{@client.username_or_email}")
end
until @client.username_or_email
@client.username_or_email = @env.ui.ask("Vagrant Cloud username or email: ")
end
while !password
password = @env.ui.ask("Password (will be hidden): ", echo: false)
until @client.password
@client.password = @env.ui.ask("Password (will be hidden): ", echo: false)
end
description = options[:description]
if description
@env.ui.output("Token description: #{description}")
else
description_default = "Vagrant login from #{Socket.gethostname}"
while !description
until description
description =
@env.ui.ask("Token description (Defaults to #{description_default.inspect}): ")
end
description = description_default if description.empty?
end
token = @client.login(login, password, description: description)
token = @client.login(description: description)
if !token
@env.ui.error(I18n.t("login_command.invalid_login"))
return 1