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.



9
10
11
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
# File 'lib/commands/abstract_command.rb', line 9

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



205
206
207
208
209
210
211
# File 'lib/commands/abstract_command.rb', line 205

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

#authorizationObject



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

def authorization
  @authorization || config.authorization
end

#config(pass = nil) ⇒ Object



117
118
119
120
# File 'lib/commands/abstract_command.rb', line 117

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

#configure_urlObject



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

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}" ).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



191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/commands/abstract_command.rb', line 191

def http_proxy( url )
  uri = URI.parse( url ) rescue nil
  return nil if uri.nil?
  if no_proxy = ENV[ 'no_proxy' ] || ENV[ 'NO_PROXY' ]
    # does not look on ip-adress ranges
    return nil if no_proxy.split( /, */ ).member?( uri.host )
  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)


126
127
128
129
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
# File 'lib/commands/abstract_command.rb', line 126

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 > 0
    warn "#{request.method} #{url.to_s}"
    if config.authorization
      warn 'use authorization' 
    else
      warn 'no authorization'
    end

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

  http.request(request)
end

#prompt_encryptionObject



79
80
81
82
83
84
# File 'lib/commands/abstract_command.rb', line 79

def prompt_encryption
  password = ask_for_password( "Enter your Nexus encryption credentials (no prompt)" )
 
  # recreate config with password
  config.password = password
end

#proxy_classObject



186
187
188
# File 'lib/commands/abstract_command.rb', line 186

def proxy_class
  @proxy_class || Net::HTTP
end

#setupObject



68
69
70
71
72
73
74
75
76
77
# File 'lib/commands/abstract_command.rb', line 68

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



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

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
   
  unless 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
end

#urlObject



43
44
45
46
47
48
# File 'lib/commands/abstract_command.rb', line 43

def url
  url = config.url
  # no leading slash
  url.sub!(/\/$/,'') if url
  url
end

#use_proxy!(url) ⇒ Object



178
179
180
181
182
183
184
# File 'lib/commands/abstract_command.rb', line 178

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