Class: Terraspace::Hooks::Builder

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
DslEvaluator, Dsl, Util
Defined in:
lib/terraspace/hooks/builder.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

Methods included from Dsl

#after, #before, #each_hook

Constructor Details

#initialize(mod, file, name) ⇒ Builder

Returns a new instance of Builder.



10
11
12
13
# File 'lib/terraspace/hooks/builder.rb', line 10

def initialize(mod, file, name)
  @mod, @file, @name = mod, file, name
  @hooks = {before: {}, after: {}}
end

Instance Attribute Details

#nameObject

IE: dsl_file: config/hooks/terraform.rb



9
10
11
# File 'lib/terraspace/hooks/builder.rb', line 9

def name
  @name
end

Instance Method Details

#buildObject



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

def build
  evaluate_file("#{Terraspace.root}/config/hooks/#{@file}")
  evaluate_file("#{@mod.root}/config/hooks/#{@file}")
  @hooks.deep_stringify_keys!
end

#run?(hook) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/terraspace/hooks/builder.rb', line 47

def run?(hook)
  !!hook["execute"]
end

#run_each_hook(type) ⇒ Object



30
31
32
33
34
35
# File 'lib/terraspace/hooks/builder.rb', line 30

def run_each_hook(type)
  hooks = @hooks.dig(type, @name) || []
  hooks.each do |hook|
    run_hook(type, hook)
  end
end

#run_hook(type, hook) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/terraspace/hooks/builder.rb', line 37

def run_hook(type, hook)
  return unless run?(hook)

  command = File.basename(@file).sub('.rb','') # IE: terraform or terraspace
  id = "#{command} #{type} #{@name}"
  label = " label: #{hook["label"]}" if hook["label"]
  logger.info  "Hook: Running #{id} hook.#{label}".color(:cyan) if Terraspace.config.hooks.show
  Runner.new(@mod, hook).run
end

#run_hooksObject



22
23
24
25
26
27
28
# File 'lib/terraspace/hooks/builder.rb', line 22

def run_hooks
  build
  run_each_hook("before")
  out = yield if block_given?
  run_each_hook("after")
  out
end