Class: Pod::Command::Keys::Rm

Inherits:
Pod::Command::Keys show all
Defined in:
lib/pod/command/keys/rm.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Pod::Command::Keys

#create_keyring, #get_current_keyring

Constructor Details

#initialize(argv) ⇒ Rm

Returns a new instance of Rm.



32
33
34
35
36
37
# File 'lib/pod/command/keys/rm.rb', line 32

def initialize(argv)
  @key_name = argv.shift_argument
  @project_name = argv.shift_argument
  @wipe_all = argv.flag?('all')
  super
end

Class Method Details

.optionsObject



26
27
28
29
30
# File 'lib/pod/command/keys/rm.rb', line 26

def self.options
  [[
    '--all', 'Remove all the stored keys without asking'
  ]].concat(super)
end

Instance Method Details

#create_regex(pattern) ⇒ Object



90
91
92
93
# File 'lib/pod/command/keys/rm.rb', line 90

def create_regex(pattern)
  regex_str = "^#{pattern.gsub('*', '.*')}$"
  Regexp.new(regex_str)
end

#delete_key(key, keyring) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/pod/command/keys/rm.rb', line 64

def delete_key(key, keyring)
  keyring.save(key, '')
  keyring.keys.delete key
  CocoaPodsKeys::KeyringLiberator.save_keyring(keyring)

  prefix = CocoaPodsKeys::Keyring.keychain_prefix
   = prefix + keyring.name
  delete_generic = `security delete-generic-password -a #{key.shellescape} -l #{.shellescape} 2>&1`

  if delete_generic.include? 'security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.'
    return "Removed value for #{key}, but could not delete from Keychain."
  elsif delete_generic.include? 'password has been deleted.'
    return "Removed value for #{key}, and deleted associated key in Keychain."
  else
    return "Removed value for #{key}."
  end
end

#matches(keys) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/pod/command/keys/rm.rb', line 82

def matches(keys)
  if @key_name.include? '*'
    return keys.select { |e| e =~ create_regex(@key_name) }
  else
    return keys.select { |e| e == @key_name }
  end
end

#runObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/pod/command/keys/rm.rb', line 45

def run
  keyring = get_current_keyring
  unless keyring
    raise Informative, 'Could not find a project to remove the key from.'
  end

  if @wipe_all
    @key_name = '*'
  end

  matching_keys = matches(keyring.keys)
  if matching_keys.count > 0
    messages = matching_keys.map { |e| delete_key(e, keyring) }
    raise Informative, messages.join("\n")
  else
    raise Informative, "Could not find key that matched \"#{@key_name}\"."
  end
end

#validate!Object



39
40
41
42
43
# File 'lib/pod/command/keys/rm.rb', line 39

def validate!
  super
  verify_podfile_exists!
  help! 'A key name is required for lookup.' unless @key_name || @wipe_all
end