Class: AgentHarness::CommandExecutor
- Inherits:
-
Object
- Object
- AgentHarness::CommandExecutor
- Defined in:
- lib/agent_harness/command_executor.rb
Overview
Executes shell commands with timeout support
Provides a clean interface for running CLI commands with proper error handling, timeout support, and result capture.
Defined Under Namespace
Classes: Result
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
-
#available?(binary) ⇒ Boolean
Check if a binary is available.
-
#execute(command, timeout: nil, env: {}, stdin_data: nil) ⇒ Result
Execute a command with optional timeout.
-
#initialize(logger: nil) ⇒ CommandExecutor
constructor
A new instance of CommandExecutor.
-
#which(binary) ⇒ String?
Check if a binary exists in PATH.
Constructor Details
#initialize(logger: nil) ⇒ CommandExecutor
Returns a new instance of CommandExecutor.
34 35 36 |
# File 'lib/agent_harness/command_executor.rb', line 34 def initialize(logger: nil) @logger = logger end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
32 33 34 |
# File 'lib/agent_harness/command_executor.rb', line 32 def logger @logger end |
Instance Method Details
#available?(binary) ⇒ Boolean
Check if a binary is available
86 87 88 |
# File 'lib/agent_harness/command_executor.rb', line 86 def available?(binary) !which(binary).nil? end |
#execute(command, timeout: nil, env: {}, stdin_data: nil) ⇒ Result
Execute a command with optional timeout
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/agent_harness/command_executor.rb', line 46 def execute(command, timeout: nil, env: {}, stdin_data: nil) cmd_array = normalize_command(command) cmd_string = cmd_array.shelljoin log_debug("Executing command", command: cmd_string, timeout: timeout) start_time = Time.now stdout, stderr, status = if timeout execute_with_timeout(cmd_array, timeout: timeout, env: env, stdin_data: stdin_data) else execute_without_timeout(cmd_array, env: env, stdin_data: stdin_data) end duration = Time.now - start_time Result.new( stdout: stdout, stderr: stderr, exit_code: status.exitstatus, duration: duration ) end |
#which(binary) ⇒ String?
Check if a binary exists in PATH
74 75 76 77 78 79 80 |
# File 'lib/agent_harness/command_executor.rb', line 74 def which(binary) ENV["PATH"].split(File::PATH_SEPARATOR).each do |path| full_path = File.join(path, binary) return full_path if File.executable?(full_path) end nil end |