diff --git a/Gemfile.lock b/Gemfile.lock index dc6962d..4ab84ff 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - xcskarel (0.16.1) + xcskarel (0.16.2) colored (= 1.2) commander (= 4.3.5) excon (= 0.45.4) @@ -220,17 +220,18 @@ GEM retriable (2.1.0) rouge (1.10.1) rspec (3.1.0) - rspec-core (~> 3.1.0) - rspec-expectations (~> 3.1.0) - rspec-mocks (~> 3.1.0) - rspec-core (3.1.7) - rspec-support (~> 3.1.0) - rspec-expectations (3.1.2) + rspec-core (~> 3.4.0) + rspec-expectations (~> 3.4.0) + rspec-mocks (~> 3.4.0) + rspec-core (3.4.4) + rspec-support (~> 3.4.0) + rspec-expectations (3.4.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.1.0) - rspec-mocks (3.1.3) - rspec-support (~> 3.1.0) - rspec-support (3.1.2) + rspec-support (~> 3.4.0) + rspec-mocks (3.4.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.4.0) + rspec-support (3.4.1) rubyzip (1.1.7) scan (0.5.2) fastlane_core (>= 0.36.1, < 1.0.0) diff --git a/bin/xcskarel b/bin/xcskarel index faeab94..c486b45 100755 --- a/bin/xcskarel +++ b/bin/xcskarel @@ -107,8 +107,8 @@ class XCSKarelApplication command :bots do |c| c.syntax = 'xcskarel bots [options]' c.description = 'Fetches all Bots found on the specified server' - c.example 'get all bot names & identifiers', 'xcskarel bots --hostname 127.0.0.1' - c.example 'get all bot metadata', 'xcskarel bots --no_filter --hostname 127.0.0.1' + c.example 'get all bot names & identifiers', 'xcskarel bots --host 127.0.0.1' + c.example 'get all bot metadata', 'xcskarel bots --no_filter --host 127.0.0.1' add_xcs_options(c) c.action do |args, options| server = create_server_from_options(options) @@ -117,10 +117,56 @@ class XCSKarelApplication end end + command :'delete bot' do |c| + c.syntax = 'xcskarel delete bot [options]' + c.description = 'Deletes the bot specified by bot name or bot id' + c.example 'delete the bot on host with name or id', 'xcskarel delete bot --host 127.0.0.1 --bot "My Bot"' + add_xcs_options(c) + add_bot_options(c) + c.action do |args, options| + raise "No Bot id was specified, please see `xcskarel delete bot --help`" unless options.bot + server = create_server_from_options(options) + delete_result = XCSKarel::Application.delete_bot(server, options.bot) + puts delete_result + end + end + + command :'duplicate bot' do |c| + c.syntax = 'xcskarel duplicate bot [options]' + c.description = 'Duplicates the bot by bot name or bot id with possible modifications' + c.example 'duplicate the bot on host with name', 'xcskarel duplicate bot --host 127.0.0.1 --bot "Useful bot" --config path/to/config.json' + c.option '--newname "New bot name"', 'The name for the newly duplicated bot' + c.option '--config FILE', 'A JSON file with configuration information for the duplicated bot' + add_xcs_options(c) + add_bot_options(c) + c.action do |args, options| + raise "No Bot id was specified, please see `xcskarel duplicate bot --help`" unless options.bot + server = create_server_from_options(options) + body = nil + body = { name: options.newname } unless options.newname.nil? + filePath = nil + filePath = File.expand_path(options.config) unless options.config.nil? + fileContents = nil + unless filePath.nil? + fileContents = File.read(filePath) if File.exists?(filePath) + end + unless fileContents.nil? || fileContents.empty? + if body.nil? + body = { configuration: JSON.parse(fileContents) } + else + body['configuration'] = JSON.parse(fileContents) + end + end + puts body + duplicate_result = XCSKarel::Application.duplicate_bot(server, options.bot, body.to_json) + puts XCSKarel::Application.format(duplicate_result, options, ['name', '_id']) + end + end + command :integrations do |c| c.syntax = 'xcskarel integrations [options]' c.description = 'Fetches all Integrations for a specified Bot identifier' - c.example 'get all integration identifiers, numbers & results', 'xcskarel integrations --bot BOT_ID --hostname 127.0.0.1' + c.example 'get all integration identifiers, numbers & results', 'xcskarel integrations --bot BOT_ID --host 127.0.0.1' add_xcs_options(c) add_bot_options(c) c.action do |args, options| @@ -178,7 +224,7 @@ class XCSKarelApplication command :health do |c| c.syntax = 'xcskarel health [options]' c.description = 'Fetches health information of the specified server' - c.example 'get health', 'xcskarel health --hostname 127.0.0.1' + c.example 'get health', 'xcskarel health --host 127.0.0.1' add_xcs_options(c) c.action do |args, options| server = create_server_from_options(options) @@ -190,7 +236,7 @@ class XCSKarelApplication command :status do |c| c.syntax = 'xcskarel status [options]' c.description = 'Fetches status information of the specified server' - c.example 'get status', 'xcskarel status --hostname 127.0.0.1' + c.example 'get status', 'xcskarel status --host 127.0.0.1' add_xcs_options(c) c.action do |args, options| server = create_server_from_options(options) diff --git a/lib/xcskarel/application.rb b/lib/xcskarel/application.rb index f55aad1..b146bdd 100644 --- a/lib/xcskarel/application.rb +++ b/lib/xcskarel/application.rb @@ -100,6 +100,18 @@ def self.integrations(server, bot_id_or_name) server.get_integrations(bot['_id']) end + def self.delete_bot(server, bot_id_or_name) + bot = server.find_bot_by_id_or_name(bot_id_or_name) + XCSKarel.log.debug "Found Bot #{bot['name']} with id #{bot['_id']}".yellow + server.delete_bot(bot) + end + + def self.duplicate_bot(server, bot_id_or_name, body) + bot = server.find_bot_by_id_or_name(bot_id_or_name) + XCSKarel.log.debug "Found Bot #{bot['name']} with id #{bot['_id']}".yellow + server.duplicate_bot(bot, body) + end + def self.integrate(server, bot_id_or_name) # find bot by id or name diff --git a/lib/xcskarel/server.rb b/lib/xcskarel/server.rb index 0afb40e..05ae807 100644 --- a/lib/xcskarel/server.rb +++ b/lib/xcskarel/server.rb @@ -90,11 +90,34 @@ def integrate(bot) integration = JSON.parse(integration) XCSKarel.log.info "Successfully started integration #{integration['number']} on Bot \"#{bot['name']}\"".green else - raise "Failed to integrate Bot #{bot_id}".red + raise "Failed to integrate Bot #{bot['name']}".red end return integration end + def delete_bot(bot) + response = delete_endpoint("/bots/#{bot['_id']}") + delete = response.body + if response.status == 204 + XCSKarel.log.info "Successfully deleted Bot \"#{bot['name']}\"".green + else + raise "Failed to delete Bot #{bot['name']}".red + end + return delete + end + + def duplicate_bot(bot, body) + # puts body + response = post_endpoint("/bots/#{bot['_id']}/duplicate", body) + duplicate = response.body + if response.status == 201 + XCSKarel.log.info "Successfully duplicated Bot \"#{bot['name']}\"".green + else + raise "Failed to duplicate Bot #{bot['name']}".red + end + return JSON.parse(duplicate) + end + def find_bot_by_id_or_name(id_or_name, raise_if_none_found=true) bots = self.get_bots found_bots = bots.select { |bot| [bot['name'], bot['_id']].index(id_or_name) != nil } @@ -125,6 +148,14 @@ def post_endpoint(endpoint, body) call_endpoint("post", endpoint, body) end + def delete_endpoint(endpoint) + call_endpoint("delete", endpoint, nil) + end + + def patch_endpoint(endpoint, body) + call_endpoint("patch", endpoint, body) + end + private def call_endpoint(method, endpoint, body) @@ -134,7 +165,13 @@ def call_endpoint(method, endpoint, body) when "get" response = Excon.get(url, headers: headers) when "post" - response = Excon.post(url, headers: headers, body: body) + local_headers = headers + local_headers['Content-Type'] = 'application/json' unless body.nil? + response = Excon.post(url, headers: local_headers, body: body) + when "delete" + response = Excon.delete(url, headers: headers, body: body) + when "patch" + response = Excon.patch(url, headers: headers, body: body) else raise "Unrecognized method #{method}" end diff --git a/lib/xcskarel/version.rb b/lib/xcskarel/version.rb index f274eb6..e27c0e0 100644 --- a/lib/xcskarel/version.rb +++ b/lib/xcskarel/version.rb @@ -1,3 +1,3 @@ module XCSKarel - VERSION = '0.16.1' + VERSION = '0.16.2' end