Class: Aidp::Interfaces::TtyCommandExecutor

Inherits:
Object
  • Object
show all
Includes:
CommandExecutorInterface
Defined in:
lib/aidp/interfaces/command_executor_interface.rb

Overview

TtyCommandExecutor wraps TTY::Command for command execution. This adapter provides the standard implementation used by AIDP.

Examples:

Creating an executor with a logger

logger = AidpLoggerAdapter.new
executor = TtyCommandExecutor.new(logger: logger)

Instance Method Summary collapse

Constructor Details

#initialize(logger: nil, component_name: "command_executor") ⇒ TtyCommandExecutor

Returns a new instance of TtyCommandExecutor.

Parameters:

  • logger (LoggerInterface) (defaults to: nil)

    optional logger for debug output

  • component_name (String) (defaults to: "command_executor")

    component name for logging



133
134
135
136
# File 'lib/aidp/interfaces/command_executor_interface.rb', line 133

def initialize(logger: nil, component_name: "command_executor")
  @logger = logger
  @component_name = component_name
end

Instance Method Details

#execute(command, args: [], input: nil, timeout: nil, **options) ⇒ CommandResult

Execute a shell command using TTY::Command.

Parameters:

  • command (String)

    the command to execute

  • args (Array<String>) (defaults to: [])

    command-line arguments

  • input (String, nil) (defaults to: nil)

    input to pass to stdin (or file path to read from)

  • timeout (Integer, nil) (defaults to: nil)

    timeout in seconds

  • options (Hash)

    additional options passed to TTY::Command#run

Returns:



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/aidp/interfaces/command_executor_interface.rb', line 146

def execute(command, args: [], input: nil, timeout: nil, **options)
  require "tty-command"

  log_debug("executing_command", command: command, args: args, timeout: timeout)

  start_time = Time.now

  begin
    cmd_obj = TTY::Command.new(printer: :null)

    # Prepare input data
    input_data = resolve_input(input)

    # Execute command - use run! to get result even on non-zero exit
    result = cmd_obj.run!(command, *args, input: input_data, timeout: timeout, **options)

    duration = Time.now - start_time
    log_debug("command_completed",
      command: command,
      exit_status: result.exit_status,
      duration: duration.round(2))

    CommandResult.new(
      stdout: result.out,
      stderr: result.err,
      exit_status: result.exit_status
    )
  rescue TTY::Command::TimeoutExceeded
    duration = Time.now - start_time
    log_debug("command_timeout",
      command: command,
      timeout: timeout,
      duration: duration.round(2))
    raise CommandTimeoutError.new(command: command, timeout: timeout)
  rescue Errno::ENOENT, Errno::EACCES => e
    # Command not found or not executable
    duration = Time.now - start_time
    log_debug("command_not_found",
      command: command,
      error: e.message,
      duration: duration.round(2))
    raise CommandExecutionError.new(command: command, original_error: e)
  rescue => e
    duration = Time.now - start_time
    log_debug("command_failed",
      command: command,
      error: e.message,
      duration: duration.round(2))
    raise CommandExecutionError.new(command: command, original_error: e)
  end
end