Class: Gem::AbstractCommand

Inherits:
Command
  • Object
show all
Includes:
LocalRemoteOptions
Defined in:
lib/commands/abstract_command.rb

Direct Known Subclasses

Commands::NexusCommand

Instance Method Summary collapse

Constructor Details

#initialize(name, summary) ⇒ AbstractCommand

Returns a new instance of AbstractCommand.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/commands/abstract_command.rb', line 12

def initialize(name, summary)
  super

  add_option('-r', '--repo KEY',
             "pick the configuration under that key.\n                                     can be used in conjuction with --clear-repo and the upload itself.") do |value, options|
    options[:nexus_repo] = value
  end

  add_option('-c', '--clear-repo',
             'Clears the nexus config for the given repo or the default repo') do |value, options|
    options[:nexus_clear] = value
  end

  add_option('--url URL',
             'URL of the rubygems repository on a Nexus server') do |value, options|
    options[:nexus_url] = value
  end

  add_option('--credential USER:PASS',
             'Enter your Nexus credentials in "Username:Password" format') do |value, options|
    options[:nexus_credential] = value
  end

  add_option('--nexus-config FILE',
             "File location of nexus config to use.\n                                     default #{Nexus::Config.default_file}") do |value, options|
    options[:nexus_config] = File.expand_path(value)
  end

  add_option('--ignore-ssl-errors',
             'No check certificate.') do |_value, options|
    options[:ssl_verify_mode] = OpenSSL::SSL::VERIFY_NONE
  end
end

Instance Method Details

#ask_for_password(message) ⇒ Object



217
218
219
220
221
222
223
# File 'lib/commands/abstract_command.rb', line 217

def ask_for_password(message)
  system 'stty -echo'
  password = ask(message)
  system 'stty echo'
  ui.say("\n")
  password
end

#authorizationObject



126
127
128
# File 'lib/commands/abstract_command.rb', line 126

def authorization
  @authorization || config.authorization
end

#config(pass = nil) ⇒ Object



121
122
123
124
# File 'lib/commands/abstract_command.rb', line 121

def config(pass = nil)
  @config = this_config(pass) if pass
  @config ||= this_config
end

#configure_urlObject



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

def configure_url
  url =
    if options[:nexus_url]
      options[:nexus_url]
    else
      say 'Enter the URL of the rubygems repository on a Nexus server'
      ask('URL: ')
    end

  if !URI.parse(url.to_s).host.nil?
    config.url = url

    say "The Nexus URL has been stored in #{config}"
  else
    raise 'no URL given'
  end
end

#http_proxy(url) ⇒ URI?

Returns the HTTP-proxy as a URI if set; nil otherwise.

Returns:

  • (URI, nil)

    the HTTP-proxy as a URI if set; nil otherwise



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/commands/abstract_command.rb', line 196

def http_proxy(url)
  uri = begin
    URI.parse(url)
  rescue StandardError
    nil
  end
  return nil if uri.nil?

  no_proxy = ENV['no_proxy']
  if (no_proxy || ENV['NO_PROXY']) && no_proxy.split(/, */).member?(uri.host)
    # does not look on ip-adress ranges
    return nil
  end

  key = uri.scheme == 'http' ? 'http_proxy' : 'https_proxy'
  proxy = Gem.configuration[:http_proxy] || ENV[key] || ENV[key.upcase]
  return nil if proxy.nil? || proxy == :no_proxy

  URI.parse(proxy)
end

#make_request(method, path) {|request| ... } ⇒ Object

Yields:

  • (request)


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/commands/abstract_command.rb', line 130

def make_request(method, path)
  require 'net/http'
  require 'net/https'

  url = URI.parse("#{self.url}/#{path}")

  http = proxy_class.new(url.host, url.port)

  if url.scheme == 'https'
    http.use_ssl = true
    http.verify_mode =
      options[:ssl_verify_mode] || config.ssl_verify_mode || OpenSSL::SSL::VERIFY_PEER
  end

  # Because sometimes our gems are huge and our people are on vpns
  http.read_timeout = 300

  request_method =
    case method
    when :get
      proxy_class::Get
    when :post
      proxy_class::Post
    when :put
      proxy_class::Put
    when :delete
      proxy_class::Delete
    else
      raise ArgumentError
    end

  request = request_method.new(url.path)
  request.add_field 'User-Agent', 'Ruby' unless RUBY_VERSION =~ /^1.9/

  yield request if block_given?

  if Gem.configuration.verbose.to_s.to_i.positive?
    warn "#{request.method} #{url}"
    if config.authorization
      warn 'use authorization'
    else
      warn 'no authorization'
    end

    warn "use proxy at #{http.proxy_address}:#{http.proxy_port}" if http.proxy_address
  end

  if Gem.configuration.verbose.to_s.to_i.positive?
    say "http request to #{self.url}/#{path} for #{method}"
  end
  http.request(request)
end

#prompt_encryptionObject



82
83
84
85
86
87
# File 'lib/commands/abstract_command.rb', line 82

def prompt_encryption
  password = ask_for_password('Enter your Nexus encryption credentials (no prompt)')

  # recreate config with password
  config.password = password
end

#proxy_classObject



191
192
193
# File 'lib/commands/abstract_command.rb', line 191

def proxy_class
  @proxy_class || Net::HTTP
end

#setupObject



71
72
73
74
75
76
77
78
79
80
# File 'lib/commands/abstract_command.rb', line 71

def setup
  prompt_encryption if config.encrypted?
  configure_url if config.url.nil? || options[:nexus_clear]
  use_proxy!(url) if http_proxy(url)
  if authorization.nil? ||
     config.always_prompt? ||
     options[:nexus_clear]
    
  end
end

#sign_inObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/commands/abstract_command.rb', line 89

def 
  token =
    if options[:nexus_credential]
      options[:nexus_credential]
    else
      say 'Enter your Nexus credentials'
      username = ask('Username: ')
      password = ask_for_password('Password: ')
      "#{username}:#{password}"
    end

  # mimic strict_encode64 which is not there on ruby1.8
  auth = "Basic #{Base64.encode64(token).gsub(/\s+/, '')}"
  @authorization = token == ':' ? nil : auth

  return if config.always_prompt?

  config.authorization = @authorization
  if @authorization
    say "Your Nexus credentials have been stored in #{config}"
  else
    say "Your Nexus credentials have been deleted from #{config}"
  end
end

#urlObject



46
47
48
49
50
51
# File 'lib/commands/abstract_command.rb', line 46

def url
  url = config.url
  # no leading slash
  url&.sub!(%r{/$}, '')
  url
end

#use_proxy!(url) ⇒ Object



183
184
185
186
187
188
189
# File 'lib/commands/abstract_command.rb', line 183

def use_proxy!(url)
  proxy_uri = http_proxy(url)
  @proxy_class = Net::HTTP::Proxy(proxy_uri.host,
                                  proxy_uri.port,
                                  proxy_uri.user,
                                  proxy_uri.password)
end