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.
Direct Known Subclasses
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, idle_timeout: nil, env: {}, stdin_data: nil, on_stdout_chunk: nil, on_stderr_chunk: nil, on_heartbeat: nil, heartbeat_interval: 1.0, observer: 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
110 111 112 |
# File 'lib/agent_harness/command_executor.rb', line 110 def available?(binary) !which(binary).nil? end |
#execute(command, timeout: nil, idle_timeout: nil, env: {}, stdin_data: nil, on_stdout_chunk: nil, on_stderr_chunk: nil, on_heartbeat: nil, heartbeat_interval: 1.0, observer: nil) ⇒ Result
Execute a command with optional timeout
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/agent_harness/command_executor.rb', line 54 def execute(command, timeout: nil, idle_timeout: nil, env: {}, stdin_data: nil, on_stdout_chunk: nil, on_stderr_chunk: nil, on_heartbeat: nil, heartbeat_interval: 1.0, observer: nil) validate_duration!(timeout, name: :timeout, allow_nil: true) validate_duration!(idle_timeout, name: :idle_timeout, allow_nil: true) validate_duration!(heartbeat_interval, name: :heartbeat_interval, allow_nil: true) cmd_array = normalize_command(command) cmd_string = cmd_array.shelljoin log_debug("Executing command", command: cmd_string, timeout: timeout, idle_timeout: idle_timeout) start_time = Time.now stdout, stderr, status = execute_streaming( cmd_array, timeout: timeout, idle_timeout: idle_timeout, env: env, stdin_data: stdin_data, on_stdout_chunk: on_stdout_chunk, on_stderr_chunk: on_stderr_chunk, on_heartbeat: on_heartbeat, heartbeat_interval: heartbeat_interval, observer: observer ) 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
98 99 100 101 102 103 104 |
# File 'lib/agent_harness/command_executor.rb', line 98 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 |