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.



950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
# 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

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



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

def command
  @command
end

#connection_error_codeObject (readonly)

Returns the value of attribute connection_error_code.



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

def connection_error_code
  @connection_error_code
end

#connection_error_reasonObject (readonly)

Returns the value of attribute connection_error_reason.



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

def connection_error_reason
  @connection_error_reason
end

#console_stateObject (readonly)

Returns the value of attribute console_state.



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

def console_state
  @console_state
end

#exit_signalObject

Returns the value of attribute exit_signal.



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

def exit_signal
  @exit_signal
end

#exit_statusObject

Returns the value of attribute exit_status.



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

def exit_status
  @exit_status
end

#failure_reasonObject (readonly)

Returns the value of attribute failure_reason.



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

def failure_reason
  @failure_reason
end

#finished_atObject (readonly)

Returns the value of attribute finished_at.



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

def finished_at
  @finished_at
end

#hostObject (readonly)

Returns the value of attribute host.



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

def host
  @host
end

#started_atObject (readonly)

Returns the value of attribute started_at.



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

def started_at
  @started_at
end

#stderrObject (readonly)

Returns the value of attribute stderr.



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

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



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

def stdout
  @stdout
end

Instance Method Details

#collect_stderr(data) ⇒ Object



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

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

#collect_stdout(data) ⇒ Object



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

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

#command_finished!Object



1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
# 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!Object



1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
# 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) ⇒ Object



1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
# 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

#durationObject



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

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

#failed?Boolean

Returns:

  • (Boolean)


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

def failed?
  !successful?
end

#failure_summaryObject



1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
# 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?Boolean

Returns:

  • (Boolean)


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

def finished?
  not @finished_at.nil?
end

#on_stderr(&block) ⇒ Object



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

def on_stderr(&block)
  @stderr_callback = block
end

#on_stdout(&block) ⇒ Object



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

def on_stdout(&block)
  @stdout_callback = block
end

#started?Boolean

Returns:

  • (Boolean)


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

def started?
  not @started_at.nil?
end

#successful?Boolean

Returns:

  • (Boolean)


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

def successful?
  exit_status && exit_status.zero?
end

#valueObject



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

def value
  self
end