Class: Swat::CommandExecution

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

Overview

Represents the whole execution pipeline of a command

Returns a hashmap that contains the following keys

execution_mode: self descriptive
prepare, precheck, execute: the different stages of execution
  each stage will include a hash with:
    succesful: boolean, indicating if it was successful or not
    output: the stdout in case the stage was successfull, stderr otherwise

Instance Method Summary collapse

Constructor Details

#initialize(command, execution_mode = "dryrun") ⇒ CommandExecution

Returns a new instance of CommandExecution.



41
42
43
44
45
46
# File 'lib/swat.rb', line 41

def initialize(command, execution_mode = "dryrun")
  @command = command
  @stages = stages(execution_mode)
  @result = { execution_mode: execution_mode }
  @context = {}
end

Instance Method Details

#execute_stage(stage) ⇒ Object



56
57
58
59
60
# File 'lib/swat.rb', line 56

def execute_stage(stage)
  @result[stage] = { successful: true, output: @command.public_send(stage, @context) }
rescue => e
  @result[stage] = { successful: false, output: e.to_s }
end

#runObject



48
49
50
51
52
53
54
# File 'lib/swat.rb', line 48

def run
  @stages.each do |stage|
    execute_stage(stage)
    break unless @result[stage][:successful]
  end
  @result
end

#stages(execution_mode) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/swat.rb', line 62

def stages(execution_mode)
  case execution_mode
  when "dryrun"
    %i(prepare pre_check)
  when "execute"
    %i(prepare pre_check execute)
  else
    fail "Invalid execution mode '#{execution_mode}'"
  end
end