Module: DocOpsLab::Dev::ToolExecution
- Defined in:
- lib/docopslab/dev/tool_execution.rb
Class Method Summary collapse
- .docker_available? ⇒ Boolean
- .image_available? ⇒ Boolean
- .run_in_docker(command) ⇒ Object
- .run_with_fallback(tool_name, command, use_docker: false) ⇒ Object
- .tool_available?(tool_name) ⇒ Boolean
Class Method Details
.docker_available? ⇒ Boolean
13 14 15 |
# File 'lib/docopslab/dev/tool_execution.rb', line 13 def docker_available? @docker_available ||= system('which docker > /dev/null 2>&1') end |
.image_available? ⇒ Boolean
17 18 19 20 21 22 23 |
# File 'lib/docopslab/dev/tool_execution.rb', line 17 def image_available? unless docker_available? @image_available = false return @image_available end @image_available ||= system('docker image inspect docopslab/dev > /dev/null 2>&1') end |
.run_in_docker(command) ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/docopslab/dev/tool_execution.rb', line 57 def run_in_docker command # Run command in docopslab/dev container # Handle both String and Array command formats cmd_str = command.is_a?(Array) ? command.shelljoin : command docker_cmd = "docker run -it --rm -v \"$(pwd):/workspace\" -w /workspace docopslab/dev #{cmd_str}" puts "🐳 Running in Docker: #{cmd_str}" system(docker_cmd) end |
.run_with_fallback(tool_name, command, use_docker: false) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/docopslab/dev/tool_execution.rb', line 25 def run_with_fallback tool_name, command, use_docker: false # Accept String or Array command; prefer Array for safer execution cmd_runner = lambda do |cmd| cmd.is_a?(Array) ? system(*cmd) : system(cmd) end # if env var LABDEV_DEBUG=true, print the full command if ENV['LABDEV_DEBUG'] == 'true' if command.is_a?(Array) puts "🐛 [DEBUG] Command to run: #{command.map do |c| Shellwords.escape(c) end.join(' ')}" else puts "🐛 [DEBUG] Command to run: #{command}" end end # Run command natively or fall back to Docker if use_docker || !tool_available?(tool_name) if image_available? run_in_docker(command) else puts "❌ #{tool_name} not available natively and Docker not found" puts " Install #{tool_name} or pull Docker image to continue" if docker_available? puts " Install #{tool_name} or Docker to continue" unless docker_available? false end else cmd_runner.call(command) end end |
.tool_available?(tool_name) ⇒ Boolean
9 10 11 |
# File 'lib/docopslab/dev/tool_execution.rb', line 9 def tool_available? tool_name system("which #{tool_name} > /dev/null 2>&1") end |