2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
2012-11-29 01:52:18 -05:00
|
|
|
require 'rubygems/command'
|
|
|
|
require 'rubygems/local_remote_options'
|
|
|
|
require 'rubygems/version_option'
|
|
|
|
require 'rubygems/gemcutter_utilities'
|
|
|
|
|
|
|
|
class Gem::Commands::YankCommand < Gem::Command
|
|
|
|
include Gem::LocalRemoteOptions
|
|
|
|
include Gem::VersionOption
|
|
|
|
include Gem::GemcutterUtilities
|
|
|
|
|
|
|
|
def description # :nodoc:
|
2013-09-14 04:59:02 -04:00
|
|
|
<<-EOF
|
2017-10-07 21:32:18 -04:00
|
|
|
The yank command permanently removes a gem you pushed to a server.
|
2013-09-14 04:59:02 -04:00
|
|
|
|
|
|
|
Once you have pushed a gem several downloads will happen automatically
|
2017-10-07 21:32:18 -04:00
|
|
|
via the webhooks. If you accidentally pushed passwords or other sensitive
|
2013-09-14 04:59:02 -04:00
|
|
|
data you will need to change them immediately and yank your gem.
|
|
|
|
EOF
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def arguments # :nodoc:
|
|
|
|
"GEM name of gem"
|
|
|
|
end
|
|
|
|
|
|
|
|
def usage # :nodoc:
|
2016-02-01 07:43:26 -05:00
|
|
|
"#{program_name} GEM -v VERSION [-p PLATFORM] [--key KEY_NAME] [--host HOST]"
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def initialize
|
2013-09-14 04:59:02 -04:00
|
|
|
super 'yank', 'Remove a pushed gem from the index'
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
add_version_option("remove")
|
|
|
|
add_platform_option("remove")
|
|
|
|
|
2016-02-01 07:43:26 -05:00
|
|
|
add_option('--host HOST',
|
2017-10-07 21:32:18 -04:00
|
|
|
'Yank from another gemcutter-compatible host',
|
|
|
|
' (e.g. https://rubygems.org)') do |value, options|
|
2016-02-01 07:43:26 -05:00
|
|
|
options[:host] = value
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
2012-11-29 03:10:23 -05:00
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
add_key_option
|
2016-02-01 07:43:26 -05:00
|
|
|
@host = nil
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2016-02-01 07:43:26 -05:00
|
|
|
@host = options[:host]
|
|
|
|
|
|
|
|
sign_in @host
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
version = get_version_from_requirements(options[:version])
|
|
|
|
platform = get_platform_from_requirements(options)
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
if version
|
2016-02-01 07:43:26 -05:00
|
|
|
yank_gem(version, platform)
|
2012-11-29 01:52:18 -05:00
|
|
|
else
|
|
|
|
say "A version argument is required: #{usage}"
|
|
|
|
terminate_interaction
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
def yank_gem(version, platform)
|
2012-11-29 01:52:18 -05:00
|
|
|
say "Yanking gem from #{self.host}..."
|
2014-09-13 23:30:02 -04:00
|
|
|
yank_api_request(:delete, version, platform, "api/v1/gems/yank")
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
def yank_api_request(method, version, platform, api)
|
2012-11-29 01:52:18 -05:00
|
|
|
name = get_one_gem_name
|
2016-02-01 07:43:26 -05:00
|
|
|
response = rubygems_api_request(method, api, host) do |request|
|
2012-11-29 01:52:18 -05:00
|
|
|
request.add_field("Authorization", api_key)
|
|
|
|
|
|
|
|
data = {
|
|
|
|
'gem_name' => name,
|
|
|
|
'version' => version,
|
|
|
|
}
|
|
|
|
data['platform'] = platform if platform
|
|
|
|
|
|
|
|
request.set_form_data data
|
|
|
|
end
|
|
|
|
say response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_version_from_requirements(requirements)
|
|
|
|
requirements.requirements.first[1].version
|
|
|
|
rescue
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_platform_from_requirements(requirements)
|
|
|
|
Gem.platforms[1].to_s if requirements.key? :added_platform
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|