Class: Toys::Utils::Exec::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/toys/utils/exec.rb

Overview

The result returned from a subcommand execution. This includes the identifying name of the execution (if any), the result status of the execution, and any captured stream output.

Possible result statuses are:

  • The process failed to start. #failed? will return true, and #exception will return an exception describing the failure (often an errno).
  • The process executed and exited with a normal exit code. Either #success? or #error? will return true, and #exit_code will return the numeric exit code.
  • The process executed but was terminated by an uncaught signal. #signaled? will return true, and #term_signal will return the numeric signal code.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#captured_errString? (readonly)

The captured error string.

Returns:

  • (String)

    The string captured from stderr.

  • (nil)

    if the command was not configured to capture stderr.



834
835
836
# File 'lib/toys/utils/exec.rb', line 834

def captured_err
  @captured_err
end

#captured_outString? (readonly)

The captured output string.

Returns:

  • (String)

    The string captured from stdout.

  • (nil)

    if the command was not configured to capture stdout.



826
827
828
# File 'lib/toys/utils/exec.rb', line 826

def captured_out
  @captured_out
end

#exceptionException? (readonly)

The exception raised if a process couldn't be started.

Exactly one of #exception and #status will be non-nil.

Returns:

  • (Exception)

    The exception raised from process start.

  • (nil)

    if the process started successfully.



856
857
858
# File 'lib/toys/utils/exec.rb', line 856

def exception
  @exception
end

#nameObject (readonly)

The subcommand's name.

Returns:

  • (Object)


818
819
820
# File 'lib/toys/utils/exec.rb', line 818

def name
  @name
end

#statusProcess::Status? (readonly)

The Ruby process status object, providing various information about the ending state of the process.

Exactly one of #exception and #status will be non-nil.

Returns:

  • (Process::Status)

    The status, if the process was successfully spanwed and terminated.

  • (nil)

    if the process could not be started.



846
847
848
# File 'lib/toys/utils/exec.rb', line 846

def status
  @status
end

Instance Method Details

#error?Boolean

Returns true if the subprocess terminated with a nonzero status, or false if the process failed to start, terminated due to a signal, or returned a zero status.

Returns:

  • (Boolean)


926
927
928
929
# File 'lib/toys/utils/exec.rb', line 926

def error?
  code = exit_code
  !code.nil? && !code.zero?
end

#exit_codeInteger?

The numeric status code for a process that exited normally,

Exactly one of #exception, #exit_code, and #term_signal will be non-nil.

Returns:

  • (Integer)

    the numeric status code, if the process started successfully and exited normally.

  • (nil)

    if the process did not start successfully, or was terminated by an uncaught signal.



869
870
871
# File 'lib/toys/utils/exec.rb', line 869

def exit_code
  status&.exitstatus
end

#failed?Boolean

Returns true if the subprocess failed to start, or false if the process was able to execute.

Returns:

  • (Boolean)


893
894
895
# File 'lib/toys/utils/exec.rb', line 893

def failed?
  status.nil?
end

#signaled?Boolean

Returns true if the subprocess terminated due to an unhandled signal, or false if the process failed to start or exited normally.

Returns:

  • (Boolean)


903
904
905
# File 'lib/toys/utils/exec.rb', line 903

def signaled?
  !term_signal.nil?
end

#success?Boolean

Returns true if the subprocess terminated with a zero status, or false if the process failed to start, terminated due to a signal, or returned a nonzero status.

Returns:

  • (Boolean)


914
915
916
917
# File 'lib/toys/utils/exec.rb', line 914

def success?
  code = exit_code
  !code.nil? && code.zero?
end

#term_signalInteger?

The numeric signal code that caused process termination.

Exactly one of #exception, #exit_code, and #term_signal will be non-nil.

Returns:

  • (Integer)

    The signal that caused the process to terminate.

  • (nil)

    if the process did not start successfully, or executed and exited with a normal exit code.



883
884
885
# File 'lib/toys/utils/exec.rb', line 883

def term_signal
  status&.termsig
end