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



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

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

#authorizationObject



98
99
100
# File 'lib/commands/abstract_command.rb', line 98

def authorization
  @authorization || config.authorization
end

#config(pass = nil) ⇒ Object



93
94
95
96
# File 'lib/commands/abstract_command.rb', line 93

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

#configure_urlObject



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/commands/abstract_command.rb', line 35

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



165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/commands/abstract_command.rb', line 165

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)


102
103
104
105
106
107
108
109
110
111
112
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
# File 'lib/commands/abstract_command.rb', line 102

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



60
61
62
63
64
65
# File 'lib/commands/abstract_command.rb', line 60

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

#proxy_classObject



160
161
162
# File 'lib/commands/abstract_command.rb', line 160

def proxy_class
  @proxy_class || Net::HTTP
end

#setupObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/commands/abstract_command.rb', line 49

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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/commands/abstract_command.rb', line 67

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 have been stored in #{config}"
    else
      say "Your Nexus credentials have been deleted from #{config}"
    end
  end
end

#urlObject



28
29
30
31
32
33
# File 'lib/commands/abstract_command.rb', line 28

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

#use_proxy!(url) ⇒ Object



152
153
154
155
156
157
158
# File 'lib/commands/abstract_command.rb', line 152

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