Module: CemAcpt::Utils::Shell

Defined in:
lib/cem_acpt/utils/shell.rb

Overview

Generic utilities for running local shell commands

Class Method Summary collapse

Class Method Details

.run_cmd(cmd, env = {}, output: $stdout, raise_on_fail: true) ⇒ String

Runs a command in a subshell and returns the Process::Status and the string output of the command.

Parameters:

  • cmd (String)

    The command to run

  • env (Hash) (defaults to: {})

    A hash of environment variables to set

  • output (IO) (defaults to: $stdout)

    An IO object that implements #:<< to write the output of the command to in real time. Typically this is a Logger object. Defaults to $stdout. If the object responds to #:debug, the command will be logged at the debug level.

  • raise_on_fail (Boolean) (defaults to: true)

    Whether to raise an error if the command fails

Returns:

  • (String)

    The string output of the command

Raises:



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
# File 'lib/cem_acpt/utils/shell.rb', line 23

def self.run_cmd(cmd, env = {}, output: $stdout, raise_on_fail: true)
  io_outerr = StringIO.new
  if output.respond_to?(:debug)
    output.debug('CemAcpt::Utils::Shell') { "Running command:\n\t#{cmd}\nWith environment:\n\t#{env}" }
  elsif output
    output << "Running command:\n\t#{cmd}\nWith environment:\n\t#{env}\n"
  end
  val = Open3.popen2e(env, cmd) do |stdin, outerr, wait_thr|
    stdin.close
    outerr.sync = true
    output_thread = Thread.new do
      while (line = outerr.gets("\n"))
        output << line if output
        io_outerr.write(line) unless line.chomp.empty?
      end
    end
    wait_thr.join
    output_thread.exit
    wait_thr.value
  end
  io_string = io_outerr.string
  raise CemAcpt::ShellCommandError, "Error running command: #{cmd}\n#{io_string}" if raise_on_fail && !val.success?

  io_string
end

.which(cmd, include_ruby_bin: false, raise_if_not_found: false) ⇒ String?

Mimics the behavior of the ‘which` command.

Parameters:

  • cmd (String)

    The command to find

  • include_ruby_bin (Boolean) (defaults to: false)

    Whether to include Ruby bin directories in the search. Setting this to true can cause errors to be raised if cem_acpt attempts to use a Ruby command that is not available to cem_acpt, such as when running with ‘bundle exec`.

  • raise_if_not_found (Boolean) (defaults to: false)

    Whether to raise an error if the command is not found

Returns:

  • (String, nil)

    The path to the command or nil if not found

Raises:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cem_acpt/utils/shell.rb', line 57

def self.which(cmd, include_ruby_bin: false, raise_if_not_found: false)
  return cmd if File.executable?(cmd) && !File.directory?(cmd)

  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    next if path.include?('/ruby') && !include_ruby_bin

    exts.each do |ext|
      exe = File.join(path, "#{cmd}#{ext}")
      return exe if File.executable?(exe) && !File.directory?(exe)
    end
  end
  raise CemAcpt::ShellCommandNotFoundError, "Command #{cmd} not found in PATH" if raise_if_not_found

  nil
end