Skip to content

Commit

Permalink
Integrate CommandExecutor for command execution feature
Browse files Browse the repository at this point in the history
- Introduced a new `CommandExecutor` class to facilitate the execution of system commands within the CLI.
- Added an `execute` method to the `UploadsCLI` class that allows users to run commands with specified parameters.
- Enhanced the initialization process to include an instance of `CommandExecutor`, ensuring proper logging and client context.
  • Loading branch information
elg0nz committed Aug 30, 2024
1 parent f440c28 commit b96744f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
63 changes: 63 additions & 0 deletions lib/command_executor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require 'httpx'
require 'json'
require 'logger'

class CommandExecutor
attr_reader :logger, :client_uuid, :endpoint_url
REACTOR_URL = ENV['MOCKSI_REACTOR_URL'] || "https://crowllectordb.onrender.com/api/v1/reactor"

def initialize(logger, client_uuid)
@logger = logger
@client_uuid = client_uuid
@endpoint_url = REACTOR_URL
end

def execute_command(command, params)
request_body = build_request_body(command, params)
response = send_request(request_body)

if response.nil?
logger.error "Failed to execute command due to a request error."
elsif response.is_a?(HTTPX::ErrorResponse)
logger.error "HTTPX Error: #{response.error.message}"
elsif response.status == 200
process_response(response)
else
logger.error "Command execution failed. Status: #{response.status}, Body: #{response.body}"
end
end

private

def build_request_body(command, params)
{
client_id: client_uuid,
command: command,
instructions: params.join(' ')
}.to_json
end

def send_request(request_body)
logger.info "sending request to #{endpoint_url}"
logger.info "request body: #{request_body}"
HTTPX.post(endpoint_url, headers: { "Content-Type" => "application/json", "x-client-id" => client_uuid }, body: request_body)
rescue => e
logger.error "Failed to send request: #{e.message}"
nil
end

def process_response(response)
result = JSON.parse(response.body)
log_command_result(result)
rescue JSON::ParserError => e
logger.error "Failed to parse response JSON: #{e.message}"
end

def log_command_result(result)
if result['result'] == 'error'
logger.error "Error during command execution: #{result['message']}"
else
logger.info "Command executed successfully. #{result}"
end
end
end
9 changes: 9 additions & 0 deletions lib/uploads_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require_relative 'file_storage'
require_relative 'file_uploader'
require_relative 'file_handler'
require_relative 'command_executor'
require 'logger'

class UploadsCLI < Thor
Expand All @@ -17,6 +18,7 @@ def initialize(*args)
@file_handler = FileHandler.new(@base_dir, @logger)
@client_uuid = get_client_uuid
@file_uploader = FileUploader.new(@logger, @client_uuid)
@command_executor = CommandExecutor.new(@logger, @client_uuid)
end

desc "update", "Update uploaded requests and responses"
Expand All @@ -43,6 +45,13 @@ def process(*args)
process_files
end

desc "execute COMMAND PARAMS", "Execute a command with the given parameters"
option :base_dir, type: :string, desc: 'Base directory for storing intercepted data. Defaults to ./tmp/intercepted_data'
def execute(command, *params)
set_base_dir
@command_executor.execute_command(command, params)
end

private

def set_base_dir
Expand Down

0 comments on commit b96744f

Please sign in to comment.