class Warg::Host::CommandOutcome

Attributes

command[R]
connection_error_code[R]
connection_error_reason[R]
console_state[R]
exit_signal[R]
exit_status[R]
failure_reason[R]
finished_at[R]
host[R]
started_at[R]
stderr[R]
stdout[R]

Public Class Methods

new(host, command, &setup) click to toggle source
# File lib/warg.rb, line 950
def initialize(host, command, &setup)
  @host = host
  @command = command

  @console_status = Console::HostStatus.new(host, Warg.console)

  @stdout = ""
  @stdout_callback = proc {}

  @stderr = ""
  @stderr_callback = proc {}

  @started_at = nil
  @finished_at = nil

  if setup
    instance_eval(&setup)
  end
end

Public Instance Methods

collect_stderr(data) click to toggle source
# File lib/warg.rb, line 987
def collect_stderr(data)
  @stderr << data
  @stderr_callback.call(data)
end
collect_stdout(data) click to toggle source
# File lib/warg.rb, line 978
def collect_stdout(data)
  @stdout << data
  @stdout_callback.call(data)
end
command_finished!() click to toggle source
# File lib/warg.rb, line 1052
def command_finished!
  if finished?
    $stderr.puts "[WARN] command already finished"
  else
    @stdout.freeze
    @stderr.freeze

    @finished_at = Time.now
    @finished_at.freeze

    if successful?
      @console_status.success!
    else
      @console_status.failed!(failure_summary)
    end
  end
end
command_started!() click to toggle source
# File lib/warg.rb, line 1041
def command_started!
  if @started_at
    $stderr.puts "[WARN] command already started"
  else
    @started_at = Time.now
    @started_at.freeze

    @console_status.started!
  end
end
connection_failed(code, reason) click to toggle source
# File lib/warg.rb, line 1070
def connection_failed(code, reason)
  @connection_error_code = code.freeze
  @connection_error_reason = reason.freeze

  @failure_reason = :connection_error

  unless started?
    @console_status.failed!(failure_summary)
  end
end
duration() click to toggle source
# File lib/warg.rb, line 1035
def duration
  if @finished_at && @started_at
    @finished_at - @started_at
  end
end
exit_signal=(value) click to toggle source
# File lib/warg.rb, line 1022
def exit_signal=(value)
  if finished?
    $stderr.puts "[WARN] cannot change `#exit_signal` after command has finished"
  else
    @exit_signal = value
    @exit_signal.freeze

    @failure_reason = :exit_signal
  end

  value
end
exit_status=(value) click to toggle source
# File lib/warg.rb, line 1008
def exit_status=(value)
  if finished?
    $stderr.puts "[WARN] cannot change `#exit_status` after command has finished"
  else
    @exit_status = value

    if failed?
      @failure_reason = :nonzero_exit_status
    end
  end

  value
end
failed?() click to toggle source
# File lib/warg.rb, line 996
def failed?
  !successful?
end
failure_summary() click to toggle source
# File lib/warg.rb, line 1081
      def failure_summary
        case failure_reason
        when :exit_signal, :nonzero_exit_status
          adjusted_stdout, adjusted_stderr = [stdout, stderr].map do |output|
            adjusted = output.each_line.map { |line| line.prepend("  ") }.join.chomp

            if adjusted.empty?
              adjusted = "(empty)"
            end

            adjusted
          end

          <<~OUTPUT
            STDOUT: #{adjusted_stdout}
            STDERR: #{adjusted_stderr}
          OUTPUT
        when :connection_error
          <<~OUTPUT
            Connection failed:
              Code: #{connection_error_code}
              Reason: #{connection_error_reason}
          OUTPUT
        end
      end
finished?() click to toggle source
# File lib/warg.rb, line 1004
def finished?
  not @finished_at.nil?
end
on_stderr(&block) click to toggle source
# File lib/warg.rb, line 983
def on_stderr(&block)
  @stderr_callback = block
end
on_stdout(&block) click to toggle source
# File lib/warg.rb, line 974
def on_stdout(&block)
  @stdout_callback = block
end
started?() click to toggle source
# File lib/warg.rb, line 1000
def started?
  not @started_at.nil?
end
successful?() click to toggle source
# File lib/warg.rb, line 992
def successful?
  exit_status && exit_status.zero?
end
value() click to toggle source
# File lib/warg.rb, line 970
def value
  self
end