Class: VagrantPlugins::GithubKeyManager::Action::DeleteKey

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-github_key_manager/action/delete_key.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ DeleteKey

Returns a new instance of DeleteKey.



5
6
7
8
9
# File 'lib/vagrant-github_key_manager/action/delete_key.rb', line 5

def initialize(app, env)
  @app = app
  @ui = env[:ui]
  @machine = env[:machine]
end

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vagrant-github_key_manager/action/delete_key.rb', line 11

def call(env)
  config = env[:global_config].github_key_manager
  github_api_url = config.github_api_url


  #@machine.communicate.execute("curl -X DELETE -u #{gitUsername}:#{gitPassword} #{endpoint}")
  if @machine.communicate.test('test -f ~/.ssh/github_key_id')
    @ui.info("We will now delete the registered ssh key to your github account.")
    github_username = @ui.ask("What is your github account name? ")
    github_password = @ui.ask("What is your github account password? (not stored) ", echo: false)

    execute_script = "require \"rubygems\"\nrequire \"json\"\nrequire \"net/https\"\nrequire \"uri\"\n\ngithub_ssh_key_id = (File.read File.expand_path(\"~/.ssh/github_key_id\")).chomp\n\nuri = URI.parse \"\#{github_api_url}\"\nhttps = Net::HTTP.new(uri.host, uri.port)\nhttps.use_ssl = uri.scheme == \"https\"\nhttps.start do |https|\n  req = Net::HTTP::Delete.new(\"\\\#{uri.path}/\\\#{github_ssh_key_id}\")\n  req.basic_auth \"\#{github_username}\", \"\#{github_password}\"\n  response = https.request(req)\n  File.delete File.expand_path(\"~/.ssh/github_key_id\")\nend\n"
    @machine.communicate.execute("ruby -e '#{execute_script}'")
    @ui.info("delete ssh key")
  end
  @app.call(env)
end