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

  # backward compatibility
  add_option( '--nexus-clear', 'DEPRECATED' ) do |value, options|
    options[ :nexus_clear ] = 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
end

Instance Method Details

#ask_for_password(message) ⇒ Object



190
191
192
193
194
195
196
# File 'lib/commands/abstract_command.rb', line 190

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

#authorizationObject



109
110
111
# File 'lib/commands/abstract_command.rb', line 109

def authorization
  @authorization || config.authorization
end

#config(pass = nil) ⇒ Object



104
105
106
107
# File 'lib/commands/abstract_command.rb', line 104

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

#configure_urlObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/commands/abstract_command.rb', line 40

def configure_url
  say "Enter the URL of the rubygems repository on a Nexus server"

  url = ask("URL: ")

  if URI.parse( "#{url}" ).host != nil
    config.url = url

    say 'The Nexus URL has been stored in ~/.gem/nexus'
  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



176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/commands/abstract_command.rb', line 176

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)


113
114
115
116
117
118
119
120
121
122
123
124
125
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
# File 'lib/commands/abstract_command.rb', line 113

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



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

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

#    if options[ :nexus_encrypt ] && !config.encrypted?
#      config.encrypt_credentials
#    elsif options[ :nexus_encrypt ] == false && config.encrypted?
#      config.decrypt_credentials      
#    end
end

#proxy_classObject



171
172
173
# File 'lib/commands/abstract_command.rb', line 171

def proxy_class
  @proxy_class || Net::HTTP
end

#setupObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/commands/abstract_command.rb', line 54

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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/commands/abstract_command.rb', line 78

def 
  say "Enter your Nexus credentials"
  username = ask("Username: ")
  password = ask_for_password("Password: ")

  # mimic strict_encode64 which is not there on ruby1.8
  token = "#{username}:#{password}"
  auth = "Basic #{Base64.encode64(token).gsub(/\s+/, '')}"
  @authorization = token == ':' ? nil : auth
   
  unless config.always_prompt?
    config.authorization = @authorization
    if @authorization
      say "Your Nexus credentials has been stored in #{config}"
    else
      say "Your Nexus credentials has been deleted from #{config}"
    end
  end
end

#urlObject



33
34
35
36
37
38
# File 'lib/commands/abstract_command.rb', line 33

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

#use_proxy!(url) ⇒ Object



163
164
165
166
167
168
169
# File 'lib/commands/abstract_command.rb', line 163

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