Class: TerraspaceHooks::InfracostGenerator

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

Overview

generate infracost breakdown

Instance Method Summary collapse

Instance Method Details

#call(runner) ⇒ Object

rubocop:disable Metrics/MethodLength



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/terraspace_hooks/infracost_generator.rb', line 7

def call(runner)
  return if ENV['SKIP_TERRASPACE_HOOKS_ALL']
  return if ENV['SKIP_TERRASPACE_HOOKS_INFRACOST_GENERATOR']

  raise 'Infracost not available' unless infracost_available?
  raise 'Terraform not available' unless terraform_available?

  mod = runner.mod
  command = <<-COMMAND
    YELLOW='\033[0;33m'
    NC='\033[0m'
    cd #{mod.cache_dir} && \
    echo "${YELLOW}[INFO #{mod.name}]${NC} Convert to plan.json..." && \
    terraform show -json tfplan.binary > plan.json && \

    echo "${YELLOW}[INFO #{mod.name}]${NC} Infracost breakdown for #{mod.name}..." && \
    infracost breakdown --project-name #{mod.name} --path plan.json --format json --out-file infracost_breakdown.json && \
    infracost output --path infracost_breakdown.json --format table --show-skipped
  COMMAND

  system(command, exception: true)
end

#infracost_available?Boolean

rubocop:enable Metrics/MethodLength

Returns:

  • (Boolean)


31
32
33
# File 'lib/terraspace_hooks/infracost_generator.rb', line 31

def infracost_available?
  system('which infracost')
end

#terraform_available?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/terraspace_hooks/infracost_generator.rb', line 35

def terraform_available?
  system('which terraform')
end