60 lines
1.2 KiB
Ruby
60 lines
1.2 KiB
Ruby
require "devops-client/handler/handler"
|
|
require "devops-client/options/key_options"
|
|
require "json"
|
|
require "devops-client/output/key"
|
|
|
|
class Key < Handler
|
|
|
|
output_with Output::Key
|
|
|
|
def initialize(host, def_options={})
|
|
@host, @options = host, def_options
|
|
@options_parser = KeyOptions.new(ARGV, def_options)
|
|
end
|
|
|
|
def handle
|
|
@options, @args = @options_parser.parse_options_for!(current_command)
|
|
case current_command
|
|
when :list
|
|
list_handler
|
|
output
|
|
when :delete
|
|
delete_handler
|
|
when :add
|
|
add_handler
|
|
end
|
|
end
|
|
|
|
def add_handler
|
|
r = inspect_parameters @options_parser.add_params, @args[2], @args[3]
|
|
unless r.nil?
|
|
@options_parser.invalid_add_command
|
|
abort(r)
|
|
end
|
|
|
|
content = File.read(@args[3])
|
|
q = {
|
|
"key_name" => @args[2],
|
|
"file_name" => File.basename(@args[3]),
|
|
"content" => content
|
|
}
|
|
post "/key", q
|
|
end
|
|
|
|
def delete_handler
|
|
r = inspect_parameters @options_parser.delete_params, @args[2]
|
|
unless r.nil?
|
|
@options_parser.invalid_delete_command
|
|
abort(r)
|
|
end
|
|
if question(I18n.t("handler.key.question.delete", :name => @args[2]))
|
|
delete "/key/#{@args[2]}"
|
|
end
|
|
end
|
|
|
|
def list_handler
|
|
@list = get("/keys")
|
|
end
|
|
|
|
end
|