Class: Gem::Commands::PushCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/rubygems_plugin.rb

Overview

Patch the PushCommand to first check the whitelist.

You can technically only push one gem at once, but if you pass several gems, we check that they are all on the whitelist.

Instance Method Summary collapse

Constructor Details

#initializePushCommand

Returns a new instance of PushCommand.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rubygems_plugin.rb', line 21

def initialize
  unsafe_initialize

  default_file = File.join(Gem.user_home, '.gem_push_safety')
  defaults.merge!(:push_safety_file => default_file)

  add_option :PushSafety, '--push-safety-file STRING',
    "whitelist file (default #{default_file})" do |value, options|
    options[:push_safety_file] = value
  end
end

Instance Method Details

#descriptionObject



33
34
35
# File 'lib/rubygems_plugin.rb', line 33

def description
  "#{unsafe_description} (with PushSafety plugin)"
end

#executeObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rubygems_plugin.rb', line 37

def execute
  white_list_file = options[:push_safety_file]
  unless File.exists?(white_list_file)
    raise "The whitelist file '#{white_list_file}' does not exist;"\
      " PushSafety will not allow you to push any gems."
  end

  white_list = File.read(white_list_file).split(/\s+/)
  if white_list.empty? || white_list.all?{|f| f.empty?}
    raise "The whitelist file '#{white_list_file}' is empty;"\
      " PushSafety will not allow you to push any gems."
  end

  grey_list = get_all_gem_names.map {|gem_file|
    Gem::Format.from_file_by_path(gem_file).spec.name}
  black_list = grey_list - white_list

  unless black_list.empty?
    raise "The following gems are not on your PushSafety whitelist:"\
      "\n#{black_list.join("\n")}\nYour whitelist file is #{white_list_file}."
  end

  unsafe_execute
end

#unsafe_descriptionObject



17
# File 'lib/rubygems_plugin.rb', line 17

alias unsafe_description description

#unsafe_executeObject



19
# File 'lib/rubygems_plugin.rb', line 19

alias unsafe_execute execute

#unsafe_initializeObject



18
# File 'lib/rubygems_plugin.rb', line 18

alias unsafe_initialize initialize