Class: RailsDoctor::CommandRunner
- Inherits:
-
Object
- Object
- RailsDoctor::CommandRunner
- Defined in:
- lib/rails_doctor/command_runner.rb
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#project_root ⇒ Object
readonly
Returns the value of attribute project_root.
Instance Method Summary collapse
- #executable?(name) ⇒ Boolean
-
#initialize(project_root:, env: ENV) ⇒ CommandRunner
constructor
A new instance of CommandRunner.
- #run(command, timeout_seconds: 120) ⇒ Object
Constructor Details
#initialize(project_root:, env: ENV) ⇒ CommandRunner
Returns a new instance of CommandRunner.
13 14 15 16 |
# File 'lib/rails_doctor/command_runner.rb', line 13 def initialize(project_root:, env: ENV) @project_root = File.(project_root) @env = env end |
Instance Attribute Details
#env ⇒ Object (readonly)
Returns the value of attribute env.
11 12 13 |
# File 'lib/rails_doctor/command_runner.rb', line 11 def env @env end |
#project_root ⇒ Object (readonly)
Returns the value of attribute project_root.
11 12 13 |
# File 'lib/rails_doctor/command_runner.rb', line 11 def project_root @project_root end |
Instance Method Details
#executable?(name) ⇒ Boolean
47 48 49 50 51 52 53 |
# File 'lib/rails_doctor/command_runner.rb', line 47 def executable?(name) path = env.fetch("PATH", "").split(File::PATH_SEPARATOR) path.any? do |dir| candidate = File.join(dir, name) File.file?(candidate) && File.executable?(candidate) end end |
#run(command, timeout_seconds: 120) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rails_doctor/command_runner.rb', line 18 def run(command, timeout_seconds: 120) started = Process.clock_gettime(Process::CLOCK_MONOTONIC) stdout = +"" stderr = +"" status_code = nil begin Timeout.timeout(timeout_seconds) do stdout, stderr, status = Open3.capture3(env.to_h, command, chdir: project_root) status_code = status.exitstatus end rescue Timeout::Error stderr = "Command timed out after #{timeout_seconds}s" status_code = 124 rescue Errno::ENOENT => error stderr = error. status_code = 127 end finished = Process.clock_gettime(Process::CLOCK_MONOTONIC) CommandResult.new( command: command, stdout: stdout, stderr: stderr, exit_status: status_code, duration_ms: ((finished - started) * 1000).round ) end |