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.



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

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



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

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

#executeObject



36
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 36

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|
    get_gem_name_from_gem_file(gem_file)
  }
  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



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

alias unsafe_description description

#unsafe_executeObject



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

alias unsafe_execute execute

#unsafe_initializeObject



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

alias unsafe_initialize initialize