-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters