module Dependabot::SharedHelpers

def self.run_shell_command(command,

def self.run_shell_command(command,
                           allow_unsafe_shell_command: false,
                           cwd: nil,
                           env: {},
                           fingerprint: nil,
                           stderr_to_stdout: true,
                           timeout: CommandHelpers::TIMEOUTS::DEFAULT)
  start = Time.now
  cmd = allow_unsafe_shell_command ? command : escape_command(command)
  puts cmd if ENV["DEBUG_HELPERS"] == "true"
  opts = {}
  opts[:chdir] = cwd if cwd
  env_cmd = [env || {}, cmd, opts].compact
  if Experiments.enabled?(:enable_shared_helpers_command_timeout)
    stdout, stderr, process = CommandHelpers.capture3_with_timeout(
      env_cmd,
      stderr_to_stdout: stderr_to_stdout,
      timeout: timeout
    )
  elsif stderr_to_stdout
    stdout, process = Open3.capture2e(env || {}, cmd, opts)
  else
    stdout, stderr, process = Open3.capture3(env || {}, cmd, opts)
  end
  time_taken = Time.now - start
  # Raise an error with the output from the shell session if the
  # command returns a non-zero status
  return stdout || "" if process&.success?
  error_context = {
    command: cmd,
    fingerprint: fingerprint,
    time_taken: time_taken,
    process_exit_value: process.to_s
  }
  check_out_of_disk_memory_error(stderr, error_context)
  raise SharedHelpers::HelperSubprocessFailed.new(
    message: stderr_to_stdout ? (stdout || "") : "#{stderr}\n#{stdout}",
    error_context: error_context
  )
end