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.



951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
# File 'lib/warg.rb', line 951

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.



938
939
940
# File 'lib/warg.rb', line 938

def command
  @command
end

#connection_error_codeObject (readonly)

Returns the value of attribute connection_error_code.



939
940
941
# File 'lib/warg.rb', line 939

def connection_error_code
  @connection_error_code
end

#connection_error_reasonObject (readonly)

Returns the value of attribute connection_error_reason.



940
941
942
# File 'lib/warg.rb', line 940

def connection_error_reason
  @connection_error_reason
end

#console_stateObject (readonly)

Returns the value of attribute console_state.



941
942
943
# File 'lib/warg.rb', line 941

def console_state
  @console_state
end

#exit_signalObject

Returns the value of attribute exit_signal.



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

def exit_signal
  @exit_signal
end

#exit_statusObject

Returns the value of attribute exit_status.



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

def exit_status
  @exit_status
end

#failure_reasonObject (readonly)

Returns the value of attribute failure_reason.



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

def failure_reason
  @failure_reason
end

#finished_atObject (readonly)

Returns the value of attribute finished_at.



945
946
947
# File 'lib/warg.rb', line 945

def finished_at
  @finished_at
end

#hostObject (readonly)

Returns the value of attribute host.



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

def host
  @host
end

#started_atObject (readonly)

Returns the value of attribute started_at.



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

def started_at
  @started_at
end

#stderrObject (readonly)

Returns the value of attribute stderr.



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

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



949
950
951
# File 'lib/warg.rb', line 949

def stdout
  @stdout
end

Instance Method Details

#collect_stderr(data) ⇒ Object



988
989
990
991
# File 'lib/warg.rb', line 988

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

#collect_stdout(data) ⇒ Object



979
980
981
982
# File 'lib/warg.rb', line 979

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

#command_finished!Object



1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
# File 'lib/warg.rb', line 1053

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



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

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



1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
# File 'lib/warg.rb', line 1071

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



1036
1037
1038
1039
1040
# File 'lib/warg.rb', line 1036

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

#failed?Boolean

Returns:

  • (Boolean)


997
998
999
# File 'lib/warg.rb', line 997

def failed?
  !successful?
end

#failure_summaryObject



1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
# File 'lib/warg.rb', line 1082

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)


1005
1006
1007
# File 'lib/warg.rb', line 1005

def finished?
  not @finished_at.nil?
end

#on_stderr(&block) ⇒ Object



984
985
986
# File 'lib/warg.rb', line 984

def on_stderr(&block)
  @stderr_callback = block
end

#on_stdout(&block) ⇒ Object



975
976
977
# File 'lib/warg.rb', line 975

def on_stdout(&block)
  @stdout_callback = block
end

#started?Boolean

Returns:

  • (Boolean)


1001
1002
1003
# File 'lib/warg.rb', line 1001

def started?
  not @started_at.nil?
end

#successful?Boolean

Returns:

  • (Boolean)


993
994
995
# File 'lib/warg.rb', line 993

def successful?
  exit_status && exit_status.zero?
end

#valueObject



971
972
973
# File 'lib/warg.rb', line 971

def value
  self
end