Class: Gem::AbstractCommand
- Inherits:
-
Command
- Object
- Command
- Gem::AbstractCommand
- Includes:
- LocalRemoteOptions
- Defined in:
- lib/commands/abstract_command.rb
Direct Known Subclasses
Instance Method Summary collapse
- #ask_for_password(message) ⇒ Object
- #authorization ⇒ Object
- #config(pass = nil) ⇒ Object
- #configure_url ⇒ Object
-
#http_proxy(url) ⇒ URI?
The HTTP-proxy as a URI if set;
nil
otherwise. -
#initialize(name, summary) ⇒ AbstractCommand
constructor
A new instance of AbstractCommand.
- #make_request(method, path) {|request| ... } ⇒ Object
- #prompt_encryption ⇒ Object
- #proxy_class ⇒ Object
- #setup ⇒ Object
- #sign_in ⇒ Object
- #url ⇒ Object
- #use_proxy!(url) ⇒ Object
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, | [ :nexus_repo ] = value end add_option( '-c', '--clear-repo', 'Clears the nexus config for the given repo or the default repo' ) do |value, | [ :nexus_clear ] = value end # backward compatibility add_option( '--nexus-clear', 'DEPRECATED' ) do |value, | [ :nexus_clear ] = value end add_option( '--nexus-config FILE', "File location of nexus config to use.\n default #{Nexus::Config.default_file}" ) do |value, | [ :nexus_config ] = File.( value ) end end |
Instance Method Details
#ask_for_password(message) ⇒ Object
184 185 186 187 188 189 190 |
# File 'lib/commands/abstract_command.rb', line 184 def ask_for_password() system "stty -echo" password = ask() system "stty echo" ui.say("\n") password end |
#authorization ⇒ Object
103 104 105 |
# File 'lib/commands/abstract_command.rb', line 103 def @authorization || config. end |
#config(pass = nil) ⇒ Object
98 99 100 101 |
# File 'lib/commands/abstract_command.rb', line 98 def config( pass = nil ) @config = this_config( pass ) if pass @config ||= this_config end |
#configure_url ⇒ Object
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.
170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/commands/abstract_command.rb', line 170 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
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 151 152 153 154 155 |
# File 'lib/commands/abstract_command.rb', line 107 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. 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_encryption ⇒ Object
65 66 67 68 69 70 |
# 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 end |
#proxy_class ⇒ Object
165 166 167 |
# File 'lib/commands/abstract_command.rb', line 165 def proxy_class @proxy_class || Net::HTTP end |
#setup ⇒ Object
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? || [ :nexus_clear ] use_proxy!( url ) if http_proxy( url ) if( .nil? || config.always_prompt? || [:nexus_clear] ) sign_in end end |
#sign_in ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/commands/abstract_command.rb', line 72 def sign_in 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 if @authorization say "Your Nexus credentials has been stored in #{config}" else say "Your Nexus credentials has been deleted from #{config}" end end end |
#url ⇒ Object
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
157 158 159 160 161 162 163 |
# File 'lib/commands/abstract_command.rb', line 157 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 |