Module: CemAcpt::Utils::Shell

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

Overview

Generic utilities for running local shell commands

Defined Under Namespace

Classes: Output, OutputData

Class Method Summary collapse

Class Method Details

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

Runs a command in a subshell and returns the 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

  • combine_out_err (Boolean) (defaults to: true)

    Whether to combine the output and error streams into the output. If false, the stderr stream will not be written to the output stream or returned with the output string.

Returns:

  • (String)

    The string output of the command



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/cem_acpt/utils/shell.rb', line 160

def self.run_cmd(cmd, env = {}, output: $stdout, raise_on_fail: true, combine_out_err: true)
  env.transform_keys!(&:to_s)
  output = Output.new(output: output, raise_on_fail: raise_on_fail, combine_out_err: combine_out_err)
  output.log_title = 'CemAcpt::Utils::Shell'
  output.debug("Running command:\n\t#{cmd}\nWith environment:\n\t#{env}")
  begin
    val = Open3.popen3(env, cmd) do |i, o, e, t|
      i.close
      o.sync = true
      e.sync = true
      files = [o, e]
      until files.all?(&:eof?)
        ready = IO.select(files)
        if ready
          ready[0].each do |f|
            data = f.read_nonblock(4096)
            if f == o
              output.write(data, :stdout)
            elsif f == e
              output.write(data, :stderr)
            end
          rescue IO::WaitReadable, EOFError
            next
          end
        end
      end
      t.value
    end
  ensure
    output.close unless output.closed?
  end
  if raise_on_fail && !val.success?
    raise CemAcpt::ShellCommandError, "Error running command: #{cmd}\n#{output.stdout.string}\n#{output.stderr.string}"
  end
  output.stdout.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:



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/cem_acpt/utils/shell.rb', line 205

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