Class: Gem::Commands::KeysCommand
- Inherits:
-
Gem::Command
- Object
- Gem::Command
- Gem::Commands::KeysCommand
- Includes:
- GemcutterUtilities
- Defined in:
- lib/rubygems/commands/keys_command.rb
Instance Method Summary collapse
- #arguments ⇒ Object
- #defaults_str ⇒ Object
- #execute ⇒ Object
-
#initialize ⇒ KeysCommand
constructor
A new instance of KeysCommand.
- #usage ⇒ Object
Constructor Details
#initialize ⇒ KeysCommand
Returns a new instance of KeysCommand.
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 |
# File 'lib/rubygems/commands/keys_command.rb', line 10 def initialize super 'keys', "Adds management for multiple gemcutter accounts" add_option '-l', '--list', 'List keys' do |value,| [:list] = value end add_option '-d', '--default KEYNAME', Symbol, 'Set the default API key' do |value,| [:default] = value end add_option '-r', '--remove KEYNAME', Symbol, 'Remove the given API key' do |value,| [:remove] = value end add_option '-a', '--add KEYNAME', Symbol, 'Add an API key with the given name' do |value,| [:add] = value end if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.4.0') add_option '--host HOST', 'Use another gemcutter-compatible host' do |value,| [:host] = value end end end |
Instance Method Details
#arguments ⇒ Object
36 37 38 |
# File 'lib/rubygems/commands/keys_command.rb', line 36 def arguments "KEYNAME name of a Rubygems API key" end |
#defaults_str ⇒ Object
40 41 42 |
# File 'lib/rubygems/commands/keys_command.rb', line 40 def defaults_str "--list" end |
#execute ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/rubygems/commands/keys_command.rb', line 48 def execute require 'keycutter' [:list] = !([:default] || [:remove] || [:add]) if [:add] then if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.4.0') gem_host = URI.parse([:host] || Gem.host).host else gem_host = 'Rubygems.org' end say "Enter your #{gem_host} credentials." say "Don't have an account yet? Create one at http://rubygems.org/sign_up" email = ask " Email: " password = ask_for_password "Password: " say args = [:get, 'api/v1/api_key'] args << [:host] if [:host] response = rubygems_api_request(*args) do |request| request.basic_auth email, password end with_response response do |resp| accounts = Gem.configuration.api_keys.merge([:add] => resp.body) Gem.configuration.api_keys = accounts say "Added #{[:add]} API key" end end if [:remove] then accounts = Gem.configuration.api_keys if accounts.has_key? [:remove] accounts.delete([:remove]) Gem.configuration.api_keys = accounts say "Removed #{[:remove]} API key" else say "No such API key" end end if [:default] then if Gem.configuration.api_keys.has_key? [:default] Gem.configuration.rubygems_api_key = Gem.configuration.api_keys[[:default]] say "Now using #{[:default]} API key" else say "No such API key. You can add it with: gem keys -a #{[:default]}" terminate_interaction 1 end end if [:list] then say "*** CURRENT KEYS ***" say api_keys = Gem.configuration.api_keys.sort_by {|k,v| k.to_s} api_keys.each do |api_key| name, key = api_key say " #{Gem.configuration.rubygems_api_key == key ? '*' : ' '} #{name}" end end end |
#usage ⇒ Object
44 45 46 |
# File 'lib/rubygems/commands/keys_command.rb', line 44 def usage "#{program_name} [options] [KEYNAME]" end |