2010-02-21 21:52:35 -05:00
|
|
|
require 'rubygems/remote_fetcher'
|
|
|
|
|
|
|
|
module Gem::GemcutterUtilities
|
2012-11-29 01:52:18 -05:00
|
|
|
# TODO: move to Gem::Command
|
2011-03-01 04:41:32 -05:00
|
|
|
OptionParser.accept Symbol do |value|
|
|
|
|
value.to_sym
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Add the --key option
|
|
|
|
|
|
|
|
def add_key_option
|
2011-03-09 17:32:29 -05:00
|
|
|
add_option('-k', '--key KEYNAME', Symbol,
|
|
|
|
'Use the given API key',
|
|
|
|
'from ~/.gem/credentials') do |value,options|
|
2011-03-01 04:41:32 -05:00
|
|
|
options[:key] = value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def api_key
|
|
|
|
if options[:key] then
|
|
|
|
verify_api_key options[:key]
|
2012-11-29 01:52:18 -05:00
|
|
|
elsif Gem.configuration.api_keys.key?(host)
|
|
|
|
Gem.configuration.api_keys[host]
|
2011-03-01 04:41:32 -05:00
|
|
|
else
|
|
|
|
Gem.configuration.rubygems_api_key
|
|
|
|
end
|
|
|
|
end
|
2010-02-21 21:52:35 -05:00
|
|
|
|
2013-02-04 21:37:35 -05:00
|
|
|
def sign_in sign_in_host = self.host
|
2010-02-21 21:52:35 -05:00
|
|
|
return if Gem.configuration.rubygems_api_key
|
|
|
|
|
2013-02-04 21:37:35 -05:00
|
|
|
pretty_host = if Gem::DEFAULT_HOST == sign_in_host then
|
|
|
|
'RubyGems.org'
|
|
|
|
else
|
|
|
|
sign_in_host
|
|
|
|
end
|
|
|
|
|
|
|
|
say "Enter your #{pretty_host} credentials."
|
|
|
|
say "Don't have an account yet? " +
|
|
|
|
"Create one at https://#{sign_in_host}/sign_up"
|
2010-02-21 21:52:35 -05:00
|
|
|
|
|
|
|
email = ask " Email: "
|
|
|
|
password = ask_for_password "Password: "
|
|
|
|
say "\n"
|
|
|
|
|
2013-02-04 21:37:35 -05:00
|
|
|
response = rubygems_api_request(:get, "api/v1/api_key",
|
|
|
|
sign_in_host) do |request|
|
2010-02-21 21:52:35 -05:00
|
|
|
request.basic_auth email, password
|
|
|
|
end
|
|
|
|
|
|
|
|
with_response response do |resp|
|
|
|
|
say "Signed in."
|
|
|
|
Gem.configuration.rubygems_api_key = resp.body
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
attr_writer :host
|
|
|
|
def host
|
|
|
|
configured_host = Gem.host unless
|
|
|
|
Gem.configuration.disable_default_gem_server
|
|
|
|
|
|
|
|
@host ||= ENV['RUBYGEMS_HOST'] || configured_host
|
|
|
|
end
|
|
|
|
|
|
|
|
def rubygems_api_request(method, path, host = nil, &block)
|
2011-01-18 19:08:49 -05:00
|
|
|
require 'net/http'
|
2010-02-21 21:52:35 -05:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
self.host = host if host
|
|
|
|
unless self.host
|
|
|
|
alert_error "You must specify a gem server"
|
|
|
|
terminate_interaction 1 # TODO: question this
|
|
|
|
end
|
|
|
|
|
|
|
|
uri = URI.parse "#{self.host}/#{path}"
|
2011-03-01 04:41:32 -05:00
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
request_method = Net::HTTP.const_get method.to_s.capitalize
|
|
|
|
|
|
|
|
Gem::RemoteFetcher.fetcher.request(uri, request_method, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_response(resp)
|
|
|
|
case resp
|
|
|
|
when Net::HTTPSuccess then
|
|
|
|
if block_given? then
|
|
|
|
yield resp
|
|
|
|
else
|
|
|
|
say resp.body
|
|
|
|
end
|
|
|
|
else
|
|
|
|
say resp.body
|
2012-11-29 01:52:18 -05:00
|
|
|
terminate_interaction 1 # TODO: question this
|
2010-02-21 21:52:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-03-01 04:41:32 -05:00
|
|
|
def verify_api_key(key)
|
|
|
|
if Gem.configuration.api_keys.key? key then
|
|
|
|
Gem.configuration.api_keys[key]
|
|
|
|
else
|
2012-11-29 01:52:18 -05:00
|
|
|
alert_error "No such API key. Please add it to your configuration (done automatically on initial `gem push`)."
|
|
|
|
terminate_interaction 1 # TODO: question this
|
2011-03-01 04:41:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
end
|