Class: Terraspace::Hooks::Runner

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/terraspace/hooks/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::Pretty

#pretty_path, #pretty_time

Methods included from Util::Sure

#sure?

Methods included from Util::Logging

#logger

Constructor Details

#initialize(mod, hook) ⇒ Runner

Returns a new instance of Runner.



16
17
18
19
# File 'lib/terraspace/hooks/runner.rb', line 16

def initialize(mod, hook)
  @mod, @hook = mod, hook
  @execute = @hook["execute"]
end

Instance Attribute Details

#hookObject (readonly)

exposing mod and hook so terraspace hooks have access to them via runner context. IE:

class EnvExporter
  def call(runner)
    puts "runner.hook #{runner.hook}"
  end
end

Docs: terraspace.cloud/docs/config/hooks/ruby/#method-argument



15
16
17
# File 'lib/terraspace/hooks/runner.rb', line 15

def hook
  @hook
end

#modObject (readonly)

exposing mod and hook so terraspace hooks have access to them via runner context. IE:

class EnvExporter
  def call(runner)
    puts "runner.hook #{runner.hook}"
  end
end

Docs: terraspace.cloud/docs/config/hooks/ruby/#method-argument



15
16
17
# File 'lib/terraspace/hooks/runner.rb', line 15

def mod
  @mod
end

Instance Method Details

#runObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/terraspace/hooks/runner.rb', line 21

def run
  case @execute
  when String
    Terraspace::Shell.new(@mod, @execute, exit_on_fail: @hook["exit_on_fail"]).run
  when -> (e) { e.respond_to?(:public_instance_methods) && e.public_instance_methods.include?(:call) }
    executor = @execute.new
  when -> (e) { e.respond_to?(:call) }
    executor = @execute
  else
    logger.warn "WARN: execute option not set for hook: #{@hook.inspect}"
  end

  return unless executor

  meth = executor.method(:call)
  case meth.arity
  when 0
    executor.call # backwards compatibility
  when 1
    executor.call(self)
  else
    raise "The #{executor} call method definition has been more than 1 arguments and is not supported"
  end
end