Class: Mozzn::Commands::Key

Inherits:
Thor
  • Object
show all
Defined in:
lib/mozzn/commands/key.rb

Instance Method Summary collapse

Instance Method Details

#addObject



7
8
9
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
35
36
37
38
39
40
41
42
43
# File 'lib/mozzn/commands/key.rb', line 7

def add
  token = Mozzn::Config.new.read['token']
  if token.nil?
    raise Thor::Error,"You need to login in order to continue."
  end
  mozzn = Mozzn::Api.new(token)
  if options[:key_path].present?
    key_path = File.expand_path(options[:key_path])
  elsif options[:public_key].present?
    public_key = options[:public_key]
  else
    raise Thor::Error, "Neither a key path or an SSH key were provided. You must use -p or -k options."
  end

  if public_key.nil?
    if File.exist?(key_path)
      File.open(key_path, "rb") do |f|
        public_key = f.read
      end
    else
      raise Thor::Error, "Unable to read #{key_path}. File does not exist or not accessible."
    end
  end

  path = 'keys'
  params = {
    key: {
      public: public_key
    }
  }
  response = mozzn.post(path, params)
  say response['info'], :green
rescue Mozzn::Disconnected
  say 'Unable to connect to Mozzn check the internet connection!', :red
rescue Mozzn::UnexpectedOutput
  say 'UnexpectedOutput', :red
end

#destroy(name = nil) ⇒ Object



46
47
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
# File 'lib/mozzn/commands/key.rb', line 46

def destroy name=nil
  token = Mozzn::Config.new.read['token']
  if token.nil?
    raise Thor::Error,"You need to login in order to continue."
  end
  mozzn = Mozzn::Api.new(token)
  params = {
    name: name
  }
  search_path = "keys/search"
  begin
  response = mozzn.get(search_path, params)
  if response.has_key?('info')
    raise Thor::Error, "#{response['info']}."
  else
    id = response['key_id']
    resources_path = "keys/#{id}/remove"
    response = mozzn.get(resources_path,nil)
    if response.has_key?('message')
      raise Thor::Error,response['message']
    else
      say response['info'], :green
    end
  end  
  rescue JSON::ParserError => e
    raise Thor::Error,"You do not have an application with the name #{params[:name]}. Please check the application name."
  end
  rescue Mozzn::Disconnected
  say 'Unable to connect to Mozzn. Check your internet connection!', :red
  rescue Mozzn::UnexpectedOutput
  say 'UnexpectedOutput', :red
end

#listObject



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
# File 'lib/mozzn/commands/key.rb', line 80

def list
  token = Mozzn::Config.new.read['token']
  if token.nil?
    raise Thor::Error,"You need to login in order to continue."
  end
  mozzn = Mozzn::Api.new(token)
  path = "keys"
  response = mozzn.get(path, nil)
  if response.has_key? ('info')
    say response['info'], :yellow
    return
  end
  table = Terminal::Table.new(headings: ['ID', 'Name']) do |t|
    response['keys'].each do |k|
      key = k['id']
      value = k['name']
      t.add_row [key, value]  
    end
  end
  say "Your SSH keys are:"
  say table
  rescue Mozzn::Disconnected
  say 'Unable to connect to Mozzn. Check your internet connection!', :red
  rescue Mozzn::UnexpectedOutput
  say 'UnexpectedOutput', :red
end