Class: Gem::Commands::KeysCommand

Inherits:
Gem::Command
  • Object
show all
Includes:
GemcutterUtilities
Defined in:
lib/rubygems/commands/keys_command.rb

Instance Method Summary collapse

Constructor Details

#initializeKeysCommand

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,options|
    options[:list] = value
  end

  add_option '-d', '--default KEYNAME', Symbol, 'Set the default API key' do |value,options|
    options[:default] = value
  end

  add_option '-r', '--remove KEYNAME', Symbol, 'Remove the given API key' do |value,options|
    options[:remove] = value
  end

  add_option '-a', '--add KEYNAME', Symbol, 'Add an API key with the given name' do |value,options|
    options[: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,options|
      options[:host] = value
    end
  end
end

Instance Method Details

#argumentsObject



36
37
38
# File 'lib/rubygems/commands/keys_command.rb', line 36

def arguments
  "KEYNAME          name of a Rubygems API key"
end

#defaults_strObject



40
41
42
# File 'lib/rubygems/commands/keys_command.rb', line 40

def defaults_str
  "--list"
end

#executeObject



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'

  options[:list] = !(options[:default] || options[:remove] || options[:add])

  if options[:add] then
    if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.4.0')
      gem_host = URI.parse(options[: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 << options[:host] if options[: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(options[:add] => resp.body)
      Gem.configuration.api_keys = accounts
      say "Added #{options[:add]} API key"
    end
  end

  if options[:remove] then
    accounts = Gem.configuration.api_keys
    if accounts.has_key? options[:remove]
      accounts.delete(options[:remove])
      Gem.configuration.api_keys = accounts
      say "Removed #{options[:remove]} API key"
    else
      say "No such API key"
    end
  end

  if options[:default] then
    if Gem.configuration.api_keys.has_key? options[:default]
      Gem.configuration.rubygems_api_key = Gem.configuration.api_keys[options[:default]]
      say "Now using #{options[:default]} API key"
    else
      say "No such API key. You can add it with: gem keys -a #{options[:default]}"
      terminate_interaction 1
    end
  end

  if options[: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

#usageObject



44
45
46
# File 'lib/rubygems/commands/keys_command.rb', line 44

def usage
  "#{program_name} [options] [KEYNAME]"
end