Class: Command::Runner::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/command/runner/message.rb

Overview

This contains information about the process that was run, such as its exit code, process id, and even time it took to run.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Message

Initialize the message with the given data about the process.

Options Hash (data):

  • :exit_code (Numeric) — default: 0

    the code the process exited with.

  • :process_id (Numeric) — default: -1

    the process id the process ran under.

  • :time (Numeric) — default: 0

    the amount of time the process took to run, in seconds.

  • :executed (Boolean) — default: false

    whether or not the process was actually executed.

  • :env (Hash) — default: {}

    the environment the process ran under.

  • :options (Hash) — default: {}

    the options that the process was run under.

  • :stdout (String) — default: "\n"

    the output the command outputted on stdout. This does not include output on stderr.

  • :stderr (String) — default: ""

    the output the command outputted on stderr. This does not include output on stdout.

  • :line (String) — default: ""

    the line that was executed.

  • :status (Process::Status) — default: nil

    the status of the process.


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/command/runner/message.rb', line 39

def initialize(data)
  {
    :executed   => false,
    :time       => 0,
    :process_id => -1,
    :exit_code  => 0,
    :env        => {},
    :options    => {},
    :stdout     => "\n",
    :stderr     => "",
    :line       => "",
    :status     => nil
  }.merge(data).each do |k, v|
    instance_variable_set(:"@#{k}", (v.dup rescue v))
  end
end

Instance Attribute Details

#exit_codeNumeric (readonly)

The code the process exited with.


118
119
120
121
# File 'lib/command/runner/message.rb', line 118

[:exit_code, :process_id, :time, :stdout, :stderr, :options,
  :line, :status].each do |key|
  define_method(key) { instance_variable_get(:"@#{key}") }
end

#lineString (readonly)

The exact line that was executed.


118
119
120
121
# File 'lib/command/runner/message.rb', line 118

[:exit_code, :process_id, :time, :stdout, :stderr, :options,
  :line, :status].each do |key|
  define_method(key) { instance_variable_get(:"@#{key}") }
end

#optionsHash (readonly)

The options that the user passed when executing the process.


118
119
120
121
# File 'lib/command/runner/message.rb', line 118

[:exit_code, :process_id, :time, :stdout, :stderr, :options,
  :line, :status].each do |key|
  define_method(key) { instance_variable_get(:"@#{key}") }
end

#process_idNumeric (readonly)

The process id the process ran under.


118
119
120
121
# File 'lib/command/runner/message.rb', line 118

[:exit_code, :process_id, :time, :stdout, :stderr, :options,
  :line, :status].each do |key|
  define_method(key) { instance_variable_get(:"@#{key}") }
end

#statusProcess::Status (readonly)

The status of the process.


118
119
120
121
# File 'lib/command/runner/message.rb', line 118

[:exit_code, :process_id, :time, :stdout, :stderr, :options,
  :line, :status].each do |key|
  define_method(key) { instance_variable_get(:"@#{key}") }
end

#stderrString (readonly)

The standard error of the process that was executed.


118
119
120
121
# File 'lib/command/runner/message.rb', line 118

[:exit_code, :process_id, :time, :stdout, :stderr, :options,
  :line, :status].each do |key|
  define_method(key) { instance_variable_get(:"@#{key}") }
end

#stdoutString (readonly)

The standard out of the process that was executed.


118
119
120
121
# File 'lib/command/runner/message.rb', line 118

[:exit_code, :process_id, :time, :stdout, :stderr, :options,
  :line, :status].each do |key|
  define_method(key) { instance_variable_get(:"@#{key}") }
end

#timeNumeric (readonly)

The amount of time the process took to run, in seconds.


118
119
120
121
# File 'lib/command/runner/message.rb', line 118

[:exit_code, :process_id, :time, :stdout, :stderr, :options,
  :line, :status].each do |key|
  define_method(key) { instance_variable_get(:"@#{key}") }
end

Class Method Details

.error(line) ⇒ Object

For when a message is created for a NoCommandError.

@ return [Message]


13
14
15
# File 'lib/command/runner/message.rb', line 13

def self.error(line)
  new(:line => line, :exit_code => 127)
end

Instance Method Details

#executed?Boolean

Whether or not the process was actually executed.


59
60
61
# File 'lib/command/runner/message.rb', line 59

def executed?
  @executed
end

#no_command?Boolean

Whether or not the command existed; or, if the exit code is 127.


82
83
84
# File 'lib/command/runner/message.rb', line 82

def no_command?
  exit_code == 127
end

#nonzero_exit?Boolean

Whether or not the exit code was non-zero.


66
67
68
# File 'lib/command/runner/message.rb', line 66

def nonzero_exit?
  exit_code != 0
end

#successful?Boolean

Returns if the command was successful or not. Success is defined as an exit code of 0.


74
75
76
# File 'lib/command/runner/message.rb', line 74

def successful?
  exit_code == 0
end