module FPM::Fry::Exec

Public Class Methods

[](*args) click to toggle source

@!method [](*cmd, options = {}) Runs a command and returns its stdout as string. This method is preferred if the expected output is short. @param [Array<String>] cmd command to run @param [Hash] options @option options [Cabin::Channel] :logger @option options [String] :description human readable string to describe what the command is doing @option options [String] :stdin_data data to write to stding @option options [String] :chdir directory to change to @return [String] stdout @raise [FPM::Fry::Exec::Failed] when exitcode != 0

# File lib/fpm/fry/exec.rb, line 30
def [](*args)
  cmd, options, description = extract_options_and_log(args)
  stdout, stderr, status = Open3.capture3(*cmd, options)
  if status.exitstatus != 0
    raise Exec.const_get("ExitCode#{status.exitstatus}").new("#{description} failed", exitstatus: status.exitstatus, stderr: stderr, stdout: stdout, command: cmd)
  end
  return stdout
end
Also aliased as: exec
exec(*args)
Alias for: []
popen(*args) click to toggle source

@!method popen(*cmd, options = {}) Runs a command and returns its stdout as IO. @param [Array<String>] cmd command to run @param [Hash] options @option options [Cabin::Channel] :logger @option options [String] :description human readable string to describe what the command is doing @option options [String] :chdir directory to change to @return [IO] stdout

# File lib/fpm/fry/exec.rb, line 48
def popen(*args)
  cmd, options, _description = extract_options_and_log(args)
  return IO.popen(cmd, options)
end

Private Class Methods

const_missing(name) click to toggle source
Calls superclass method
# File lib/fpm/fry/exec.rb, line 64
def const_missing(name)
  if name.to_s =~ /\AExitCode\d+\z/
    klass = Class.new(Failed)
    const_set(name, klass)
    return klass
  end
  super
end
extract_options_and_log(args) click to toggle source
# File lib/fpm/fry/exec.rb, line 53
def extract_options_and_log(args)
  options = args.last.kind_of?(Hash) ? args.pop.dup : {}
  cmd = args
  logger = options.delete(:logger)
  description = options.delete(:description) || "Running #{cmd.join(' ')}"
  if logger
    logger.debug(description, command: args)
  end
  return cmd, options, description
end