Class: Terraspace::Terraform::Hooks::Builder

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
DslEvaluator, Dsl, Util
Defined in:
lib/terraspace/terraform/hooks/builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#pretty_path

Methods included from Util::Sure

#sure?

Methods included from Util::Sh

#sh

Methods included from Util::Logging

#logger

Methods included from Dsl

#after, #before, #each_hook

Constructor Details

#initialize(mod, name) ⇒ Builder

Returns a new instance of Builder.



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

def initialize(mod, name)
  @mod, @name = mod, name
  @file = "#{Terraspace.root}/config/cli/hooks.rb"
  @hooks = {before: {}, after: {}}
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#buildObject



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

def build
  return @hooks unless File.exist?(@file)
  evaluate_file(@file)
  @hooks.deep_stringify_keys!
end

#run_hook(type) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/terraspace/terraform/hooks/builder.rb', line 29

def run_hook(type)
  execute = @hooks.dig(type, @name.to_s, "execute")
  return unless execute

  exit_on_fail = @hooks.dig(type, @name.to_s, "exit_on_fail")
  exit_on_fail = exit_on_fail.nil? ? true : exit_on_fail

  logger.info "Running #{type} hook"
  sh(execute, exit_on_fail: exit_on_fail)
end

#run_hooksObject



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

def run_hooks
  build
  run_hook("before")
  yield if block_given?
  run_hook("after")
end