Skip to content

Commit b96744f

Browse files
committed
Integrate CommandExecutor for command execution feature
- 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.
1 parent f440c28 commit b96744f

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

lib/command_executor.rb

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
require 'httpx'
2+
require 'json'
3+
require 'logger'
4+
5+
class CommandExecutor
6+
attr_reader :logger, :client_uuid, :endpoint_url
7+
REACTOR_URL = ENV['MOCKSI_REACTOR_URL'] || "https://crowllectordb.onrender.com/api/v1/reactor"
8+
9+
def initialize(logger, client_uuid)
10+
@logger = logger
11+
@client_uuid = client_uuid
12+
@endpoint_url = REACTOR_URL
13+
end
14+
15+
def execute_command(command, params)
16+
request_body = build_request_body(command, params)
17+
response = send_request(request_body)
18+
19+
if response.nil?
20+
logger.error "Failed to execute command due to a request error."
21+
elsif response.is_a?(HTTPX::ErrorResponse)
22+
logger.error "HTTPX Error: #{response.error.message}"
23+
elsif response.status == 200
24+
process_response(response)
25+
else
26+
logger.error "Command execution failed. Status: #{response.status}, Body: #{response.body}"
27+
end
28+
end
29+
30+
private
31+
32+
def build_request_body(command, params)
33+
{
34+
client_id: client_uuid,
35+
command: command,
36+
instructions: params.join(' ')
37+
}.to_json
38+
end
39+
40+
def send_request(request_body)
41+
logger.info "sending request to #{endpoint_url}"
42+
logger.info "request body: #{request_body}"
43+
HTTPX.post(endpoint_url, headers: { "Content-Type" => "application/json", "x-client-id" => client_uuid }, body: request_body)
44+
rescue => e
45+
logger.error "Failed to send request: #{e.message}"
46+
nil
47+
end
48+
49+
def process_response(response)
50+
result = JSON.parse(response.body)
51+
log_command_result(result)
52+
rescue JSON::ParserError => e
53+
logger.error "Failed to parse response JSON: #{e.message}"
54+
end
55+
56+
def log_command_result(result)
57+
if result['result'] == 'error'
58+
logger.error "Error during command execution: #{result['message']}"
59+
else
60+
logger.info "Command executed successfully. #{result}"
61+
end
62+
end
63+
end

lib/uploads_cli.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require_relative 'file_storage'
77
require_relative 'file_uploader'
88
require_relative 'file_handler'
9+
require_relative 'command_executor'
910
require 'logger'
1011

1112
class UploadsCLI < Thor
@@ -17,6 +18,7 @@ def initialize(*args)
1718
@file_handler = FileHandler.new(@base_dir, @logger)
1819
@client_uuid = get_client_uuid
1920
@file_uploader = FileUploader.new(@logger, @client_uuid)
21+
@command_executor = CommandExecutor.new(@logger, @client_uuid)
2022
end
2123

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

48+
desc "execute COMMAND PARAMS", "Execute a command with the given parameters"
49+
option :base_dir, type: :string, desc: 'Base directory for storing intercepted data. Defaults to ./tmp/intercepted_data'
50+
def execute(command, *params)
51+
set_base_dir
52+
@command_executor.execute_command(command, params)
53+
end
54+
4655
private
4756

4857
def set_base_dir

0 commit comments

Comments
 (0)