Class: Meroku::CLI::Keys

Inherits:
Thor
  • Object
show all
Includes:
ActionView::Helpers::TextHelper, Util
Defined in:
lib/meroku/cli/keys.rb

Instance Method Summary collapse

Instance Method Details

#addObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/meroku/cli/keys.rb', line 11

def add
  require 'rest-client'
  require 'rack'
  require 'action_view'

  if !public_key_exists?
    puts "Public key not found at ~/.ssh/id_rsa.pub"
    puts "see https://git-scm.com/book/en/v2/Git-on-the-Server-Generating-Your-SSH-Public-Key"
    return
  end
  key = IO.read("#{Dir.home}/.ssh/id_rsa.pub")
  resp = RestClient.post 'https://www.meroku.com/meroku/keys.json', { key: key, authentication: { token: cli_token } }
  puts "Added #{truncate(JSON.parse(resp)["data"]["key"])}. (#{resp.code} #{Rack::Utils::HTTP_STATUS_CODES[resp.code]})"
rescue RestClient::Exception => e
  out_str = "ERROR"

  if valid_json?(e.response) && JSON.parse(e.response)["errors"].size >= 1
    out_str += " "
    out_str += JSON.parse(e.response)["errors"].map { |e| e.join(' ') }.to_sentence
  end
  out_str += " (#{e.to_s}) "
  puts out_str
end