Class: PeerCommander::Command

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

Overview

A single command to be executed, with access to results statuses, output, and timings

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, stdin_data: nil, env: {}, opts: {}) ⇒ Command

The env specified will be passed directly to Open3.capture2e as the env to run with The opts provided will also be passed directly to Open3.capture2e as opts stdin_data will also be passed directly to Open3.capture2e



11
12
13
14
15
16
# File 'lib/peer_commander/command.rb', line 11

def initialize(command, stdin_data: nil, env: {}, opts: {})
  @command = command
  @stdin_data = stdin_data
  @env = env
  @opts = opts
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



6
7
8
# File 'lib/peer_commander/command.rb', line 6

def command
  @command
end

#envObject (readonly)

Returns the value of attribute env.



6
7
8
# File 'lib/peer_commander/command.rb', line 6

def env
  @env
end

#exceptionObject (readonly)

Returns the value of attribute exception.



6
7
8
# File 'lib/peer_commander/command.rb', line 6

def exception
  @exception
end

#optsObject (readonly)

Returns the value of attribute opts.



6
7
8
# File 'lib/peer_commander/command.rb', line 6

def opts
  @opts
end

#stdin_dataObject (readonly)

Returns the value of attribute stdin_data.



6
7
8
# File 'lib/peer_commander/command.rb', line 6

def stdin_data
  @stdin_data
end

Instance Method Details

#durationObject

How long the command took to run in seconds



59
60
61
62
63
# File 'lib/peer_commander/command.rb', line 59

def duration
  raise Errors::CommandNotExecutedError unless executed?

  timing
end

#executeObject

Execute the command, will capture any exceptions that inherit from StandardError raised by the execution of the given command and store it in the exception attribute, NOTE: This means it _will not_ propogate the exception upwards

Returns self



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/peer_commander/command.rb', line 23

def execute
  raise Errors::CommandAlreadyExecutedError if executed?

  start_time = Time.now

  begin
    @command_output, @status = Open3.capture2e(env, command, execution_options)
  rescue StandardError => e
    @exception = e
  ensure
    @timing = Time.now - start_time
  end

  self
end

#executed?Boolean

Returns a truthy object if the command has been executed, or nil if it has not.

If the command has been executed it will either return the status code or the exception that was raised

Returns:

  • (Boolean)


70
71
72
# File 'lib/peer_commander/command.rb', line 70

def executed?
  exception || status
end

#exit_codeObject



74
75
76
77
78
79
80
# File 'lib/peer_commander/command.rb', line 74

def exit_code
  raise Errors::CommandNotExecutedError unless executed?

  return nil if exception

  status.exitstatus
end

#failed?Boolean

Return true if the command failed

Returns:

  • (Boolean)


47
48
49
# File 'lib/peer_commander/command.rb', line 47

def failed?
  !success?
end

#outputObject

Return the output (stdout and stderr interleaved) of running the command



52
53
54
55
56
# File 'lib/peer_commander/command.rb', line 52

def output
  raise Errors::CommandNotExecutedError unless executed?

  command_output
end

#success?Boolean

Return true if the command was successful

Returns:

  • (Boolean)

Raises:



40
41
42
43
44
# File 'lib/peer_commander/command.rb', line 40

def success?
  raise Errors::CommandNotExecutedError unless executed?

  exception.nil? && status.success?
end