Class: Gem::Commands::InaboxsecureCommand

Inherits:
Gem::Command
  • Object
show all
Defined in:
lib/rubygems/commands/inaboxsecure_command.rb

Defined Under Namespace

Modules: Multipart

Instance Method Summary collapse

Constructor Details

#initializeInaboxsecureCommand

Returns a new instance of InaboxsecureCommand.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rubygems/commands/inaboxsecure_command.rb', line 17

def initialize
  super 'inaboxsecure', description

  add_option('-c', '--configure', "Configure GemInABox-Secure") do |value, options|
    options[:configure] = true
  end

  add_option('-g', '--host HOST', "Host to upload to.") do |value, options|
    options[:host] = value
  end
end

Instance Method Details

#argumentsObject



9
10
11
# File 'lib/rubygems/commands/inaboxsecure_command.rb', line 9

def arguments
  "GEM       built gem to push up"
end

#config_pathObject



91
92
93
# File 'lib/rubygems/commands/inaboxsecure_command.rb', line 91

def config_path
  File.join(Gem.user_home, '.gem', 'geminabox')
end

#configureObject



95
96
97
98
99
# File 'lib/rubygems/commands/inaboxsecure_command.rb', line 95

def configure
  say "Enter the root url for your personal geminabox instance. (E.g. http://gems/)"
  host = ask("Host:")
  self.geminabox_host = host
end

#descriptionObject



5
6
7
# File 'lib/rubygems/commands/inaboxsecure_command.rb', line 5

def description
  'Push a gem up to your GemInABox-Secure'
end

#executeObject



29
30
31
32
33
# File 'lib/rubygems/commands/inaboxsecure_command.rb', line 29

def execute
  return configure if options[:configure]
  setup
  send_gem
end

#find_gemObject

Raises:

  • (Gem::CommandLineError)


44
45
46
47
48
49
50
51
# File 'lib/rubygems/commands/inaboxsecure_command.rb', line 44

def find_gem
  say "You didn't specify a gem, looking for one in pkg..."
  path, directory = File.split(Dir.pwd)
  possible_gems = Dir.glob("pkg/#{directory}-*.gem")
  raise Gem::CommandLineError, "Couldn't find a gem in pkg, please specify a gem name on the command line (e.g. gem inabox GEMNAME)" unless possible_gems.any?
  name_regexp = Regexp.new("^pkg/#{directory}-")
  possible_gems.sort_by{ |a| Gem::Version.new(a.sub(name_regexp,'')) }.last
end

#geminabox_hostObject



101
102
103
# File 'lib/rubygems/commands/inaboxsecure_command.rb', line 101

def geminabox_host
  @geminabox_host ||= options[:host] || Gem.configuration.load_file(config_path)[:host]
end

#geminabox_host=(host) ⇒ Object



105
106
107
108
109
110
111
112
113
114
# File 'lib/rubygems/commands/inaboxsecure_command.rb', line 105

def geminabox_host=(host)
  config = Gem.configuration.load_file(config_path).merge(:host => host)

  dirname = File.dirname(config_path)
  Dir.mkdir(dirname) unless File.exists?(dirname)

  File.open(config_path, 'w') do |f|
    f.write config.to_yaml
  end
end

#handle_response(response) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/rubygems/commands/inaboxsecure_command.rb', line 82

def handle_response(response)
  case response
  when Net::HTTPSuccess, Net::HTTPRedirection
    puts response.body
  else
    response.error!
  end
end

#proxyObject



74
75
76
77
78
79
80
# File 'lib/rubygems/commands/inaboxsecure_command.rb', line 74

def proxy
  if proxy_info = ENV['http_proxy'] || ENV['HTTP_PROXY'] and uri = URI.parse(proxy_info)
    Net::HTTP::Proxy(uri.host, uri.port, uri.user, uri.password)
  else
    Net::HTTP
  end
end

#send_gemObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rubygems/commands/inaboxsecure_command.rb', line 53

def send_gem
  # sanitize printed URL if a password is present
  url = URI.parse(geminabox_host)
  url_for_presentation = url.clone
  url_for_presentation.password = '***' if url_for_presentation.password

  @gemfiles.each do |gemfile|
    say "Pushing #{File.basename(gemfile)} to #{url_for_presentation}..."

    File.open(gemfile, "rb") do |file|
      request_body, request_headers = Multipart::MultipartPost.new.prepare_query("file" => file)

      proxy.start(url.host, url.port) {|con|
        req = Net::HTTP::Post.new('/upload', request_headers)
        req.basic_auth(url.user, url.password) if url.user
        handle_response(con.request(req, request_body))
      }
    end
  end
end

#setupObject



35
36
37
38
39
40
41
42
# File 'lib/rubygems/commands/inaboxsecure_command.rb', line 35

def setup
  if options[:args].size == 0
    @gemfiles = [find_gem]
  else
    @gemfiles = get_all_gem_names
  end
  configure unless geminabox_host
end

#usageObject



13
14
15
# File 'lib/rubygems/commands/inaboxsecure_command.rb', line 13

def usage
  "#{program_name} GEM"
end