Module: Fourflusher::Executable

Included in:
SimControl
Defined in:
lib/fourflusher/executable.rb

Overview

Module which provides support for running executables.

In a class it can be used as:

extend Executable
executable :git

This will create two methods ‘git` and `git!` both accept a command but the later will raise on non successful executions. The methods return the output of the command.

Defined Under Namespace

Classes: Indenter

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.capture_command(executable, command, capture: :merge) ⇒ (String, Process::Status)

Runs the given command, capturing the desired output.

Parameters:

  • bin (String)

    The binary to use.

  • command (Array<#to_s>)

    The command to send to the binary.

  • capture (Symbol) (defaults to: :merge)

    Whether it should raise if the command fails.

Returns:

  • ((String, Process::Status))

    The desired captured output from the command, and the status from running the command.

Raises:

  • If the executable could not be located.



145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/fourflusher/executable.rb', line 145

def self.capture_command(executable, command, capture: :merge)
  bin = which(executable)
  fail Fourflusher::Informative, "Unable to locate the executable `#{executable}`" unless bin

  require 'open3'
  command = command.map(&:to_s)
  case capture
  when :merge then Open3.capture2e(bin, *command)
  when :both then Open3.capture3(bin, *command)
  when :out then Open3.capture2(bin, *command)
  when :err then Open3.capture3(bin, *command).drop(1)
  when :none then Open3.capture2(bin, *command).last
  end
end

.execute_command(executable, command, raise_on_failure = true) ⇒ String

Executes the given command displaying it if in verbose mode.

Parameters:

  • bin (String)

    The binary to use.

  • command (Array<#to_s>)

    The command to send to the binary.

  • raise_on_failure (Bool) (defaults to: true)

    Whether it should raise if the command fails.

Returns:

  • (String)

    the output of the command (STDOUT and STDERR).

Raises:

  • If the executable could not be located.

  • If the command fails and the ‘raise_on_failure` is set to true.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/fourflusher/executable.rb', line 79

def self.execute_command(executable, command, raise_on_failure = true)
  bin = which(executable)
  fail Fourflusher::Informative, "Unable to locate the executable `#{executable}`" unless bin

  command = command.map(&:to_s)
  full_command = "#{bin} #{command.join(' ')}"

  if Config.instance.verbose?
    UI.message("$ #{full_command}")
    stdout = Indenter.new(STDOUT)
    stderr = Indenter.new(STDERR)
  else
    stdout = Indenter.new
    stderr = Indenter.new
  end

  status = popen3(bin, command, stdout, stderr)
  stdout = stdout.join
  stderr = stderr.join
  output = stdout + stderr
  unless status.success?
    if raise_on_failure
      fail Fourflusher::Informative, "#{full_command}\n\n#{output}"
    else
      UI.message("[!] Failed: #{full_command}".red)
    end
  end

  output
end

.which(program) ⇒ String, Nil

Returns the absolute path to the binary with the given name on the current ‘PATH`, or `nil` if none is found.

Parameters:

  • program (String)

    The name of the program being searched for.

Returns:

  • (String, Nil)

    The absolute path to the given program, or ‘nil` if it wasn’t found in the current ‘PATH`.



119
120
121
122
123
124
125
126
# File 'lib/fourflusher/executable.rb', line 119

def self.which(program)
  program = program.to_s
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    bin = File.expand_path(program, path)
    return bin if File.file?(bin) && File.executable?(bin)
  end
  nil
end

Instance Method Details

#executable(name) ⇒ void

This method returns an undefined value.

Creates the methods for the executable with the given name.

Parameters:

  • name (Symbol)

    the name of the executable.



52
53
54
55
56
57
58
59
60
# File 'lib/fourflusher/executable.rb', line 52

def executable(name)
  define_method(name) do |*command|
    Executable.execute_command(name, Array(command).flatten, false)
  end

  define_method(name.to_s + '!') do |*command|
    Executable.execute_command(name, Array(command).flatten, true)
  end
end