Class: Warg::Host::CommandOutcome

Inherits:
Object
  • Object
show all
Defined in:
lib/warg.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, command, &setup) ⇒ CommandOutcome

Returns a new instance of CommandOutcome.



923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
# File 'lib/warg.rb', line 923

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

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



910
911
912
# File 'lib/warg.rb', line 910

def command
  @command
end

#connection_error_codeObject (readonly)

Returns the value of attribute connection_error_code.



911
912
913
# File 'lib/warg.rb', line 911

def connection_error_code
  @connection_error_code
end

#connection_error_reasonObject (readonly)

Returns the value of attribute connection_error_reason.



912
913
914
# File 'lib/warg.rb', line 912

def connection_error_reason
  @connection_error_reason
end

#console_stateObject (readonly)

Returns the value of attribute console_state.



913
914
915
# File 'lib/warg.rb', line 913

def console_state
  @console_state
end

#exit_signalObject

Returns the value of attribute exit_signal.



914
915
916
# File 'lib/warg.rb', line 914

def exit_signal
  @exit_signal
end

#exit_statusObject

Returns the value of attribute exit_status.



915
916
917
# File 'lib/warg.rb', line 915

def exit_status
  @exit_status
end

#failure_reasonObject (readonly)

Returns the value of attribute failure_reason.



916
917
918
# File 'lib/warg.rb', line 916

def failure_reason
  @failure_reason
end

#finished_atObject (readonly)

Returns the value of attribute finished_at.



917
918
919
# File 'lib/warg.rb', line 917

def finished_at
  @finished_at
end

#hostObject (readonly)

Returns the value of attribute host.



918
919
920
# File 'lib/warg.rb', line 918

def host
  @host
end

#started_atObject (readonly)

Returns the value of attribute started_at.



919
920
921
# File 'lib/warg.rb', line 919

def started_at
  @started_at
end

#stderrObject (readonly)

Returns the value of attribute stderr.



920
921
922
# File 'lib/warg.rb', line 920

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



921
922
923
# File 'lib/warg.rb', line 921

def stdout
  @stdout
end

Instance Method Details

#collect_stderr(data) ⇒ Object



960
961
962
963
# File 'lib/warg.rb', line 960

def collect_stderr(data)
  @stderr << data
  @stderr_callback.call(data)
end

#collect_stdout(data) ⇒ Object



951
952
953
954
# File 'lib/warg.rb', line 951

def collect_stdout(data)
  @stdout << data
  @stdout_callback.call(data)
end

#command_finished!Object



1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
# File 'lib/warg.rb', line 1025

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!Object



1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
# File 'lib/warg.rb', line 1014

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) ⇒ Object



1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
# File 'lib/warg.rb', line 1043

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

#durationObject



1008
1009
1010
1011
1012
# File 'lib/warg.rb', line 1008

def duration
  if @finished_at && @started_at
    @finished_at - @started_at
  end
end

#failed?Boolean

Returns:

  • (Boolean)


969
970
971
# File 'lib/warg.rb', line 969

def failed?
  !successful?
end

#failure_summaryObject



1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
# File 'lib/warg.rb', line 1054

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?Boolean

Returns:

  • (Boolean)


977
978
979
# File 'lib/warg.rb', line 977

def finished?
  not @finished_at.nil?
end

#on_stderr(&block) ⇒ Object



956
957
958
# File 'lib/warg.rb', line 956

def on_stderr(&block)
  @stderr_callback = block
end

#on_stdout(&block) ⇒ Object



947
948
949
# File 'lib/warg.rb', line 947

def on_stdout(&block)
  @stdout_callback = block
end

#started?Boolean

Returns:

  • (Boolean)


973
974
975
# File 'lib/warg.rb', line 973

def started?
  not @started_at.nil?
end

#successful?Boolean

Returns:

  • (Boolean)


965
966
967
# File 'lib/warg.rb', line 965

def successful?
  exit_status && exit_status.zero?
end

#valueObject



943
944
945
# File 'lib/warg.rb', line 943

def value
  self
end