class CLI::Kit::Executor

Public Class Methods

new(log_file:) click to toggle source
# File lib/cli/kit/executor.rb, line 8
def initialize(log_file:)
  FileUtils.mkpath(File.dirname(log_file))
  @log_file = log_file
end

Public Instance Methods

call(command, command_name, args) click to toggle source
# File lib/cli/kit/executor.rb, line 13
def call(command, command_name, args)
  with_traps do
    with_logging do |id|
      begin
        command.call(args, command_name)
      rescue => e
        begin
          $stderr.puts "This command ran with ID: #{id}"
          $stderr.puts "Please include this information in any issues/report along with relevant logs"
        rescue SystemCallError
          # Outputting to stderr is best-effort.  Avoid raising another error when outputting debug info so that
          # we can detect and log the original error, which may even be the source of this error.
          nil
        end
        raise e
      end
    end
  end
end

Private Instance Methods

info_handler(_sig) click to toggle source
# File lib/cli/kit/executor.rb, line 72
def info_handler(_sig)
  z = caller
  CLI::UI.raw do
    $stderr.puts('SIGINFO:')
    $stderr.puts(z)
  end
end
quit_handler(_sig) click to toggle source
# File lib/cli/kit/executor.rb, line 63
def quit_handler(_sig)
  z = caller
  CLI::UI.raw do
    $stderr.puts('SIGQUIT: quit')
    $stderr.puts(z)
  end
  exit(CLI::Kit::EXIT_FAILURE_BUT_NOT_BUG)
end
twrap(signal, handler) { || ... } click to toggle source
# File lib/cli/kit/executor.rb, line 52
def twrap(signal, handler)
  return yield unless Signal.list.key?(signal)

  begin
    prev_handler = trap(signal, handler)
    yield
  ensure
    trap(signal, prev_handler)
  end
end
with_logging() { || ... } click to toggle source
# File lib/cli/kit/executor.rb, line 35
def with_logging(&block)
  return yield unless @log_file
  CLI::UI.log_output_to(@log_file) do
    CLI::UI::StdoutRouter.with_id(on_streams: [CLI::UI::StdoutRouter.duplicate_output_to]) do |id|
      block.call(id)
    end
  end
end
with_traps() { || ... } click to toggle source
# File lib/cli/kit/executor.rb, line 44
def with_traps
  twrap('QUIT', method(:quit_handler)) do
    twrap('INFO', method(:info_handler)) do
      yield
    end
  end
end