File: execute_command.rb

package info (click to toggle)
ruby-riddle 2.3.1-2~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 10,752 kB
  • sloc: sql: 25,022; php: 5,992; ruby: 4,757; sh: 59; makefile: 5
file content (51 lines) | stat: -rw-r--r-- 1,016 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
46
47
48
49
50
51
# frozen_string_literal: true

class Riddle::ExecuteCommand
  WINDOWS = (RUBY_PLATFORM =~ /mswin|mingw/)

  def self.call(command, verbose = true)
    new(command, verbose).call
  end

  def initialize(command, verbose)
    @command, @verbose = command, verbose

    return unless WINDOWS

    @command = "start /B #{@command} 1> NUL 2>&1"
    @verbose = true
  end

  def call
    result = verbose? ? result_from_system : result_from_backticks
    return result if result.status == 0

    error = Riddle::CommandFailedError.new "Sphinx command failed to execute"
    error.command_result = result
    raise error
  end

  private

  attr_reader :command, :verbose

  def result_from_backticks
    begin
      output = `#{command}`
    rescue SystemCallError => error
      output = error.message
    end

    Riddle::CommandResult.new command, $?.exitstatus, output
  end

  def result_from_system
    system command

    Riddle::CommandResult.new command, $?.exitstatus
  end

  def verbose?
    verbose
  end
end