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

View File

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