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.



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.

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