class Terraform::RakeTask

Provides a method to define a Rake task that checks and optionally fixes Terraform configuration format

Attributes

diff[RW]

Show diffs of the changes? (default: `true`)

fail_on_error[RW]

Whether or not to fail Rake when an error occurs. If `write` is disabled, this include formatting issues.

name[RW]

Name of task (default: `:terraform`)

project_root[RW]

Path to the project root (default: `.`)

verbose[RW]

Use verbose output. If this is set to true, the task will print the executed commands. Defaults to `true`.

write[RW]

Fix the formatting instead of just verifying? (default: `false`)

Public Class Methods

new(*args, &task_block) click to toggle source
# File lib/terraform/rake_task.rb, line 33
def initialize(*args, &task_block)
  @name = args.shift || :terraform

  @project_root = '.'

  @diff  = true
  @write = false

  @fail_on_error = true
  @verbose       = true

  define(args, &task_block)
end

Public Instance Methods

define(args) { |*[self, task_args].slice(0, arity)| ... } click to toggle source
# File lib/terraform/rake_task.rb, line 51
def define(args, &task_block)
  desc('Check Terraform file formatting') if !Rake.application.last_description

  task(name, *args) do |_, task_args|
    RakeFileUtils.verbose(verbose) do
      yield(*[self, task_args].slice(0, task_block.arity)) if block_given?
      run_task
    end
  end
end
paths() click to toggle source
# File lib/terraform/rake_task.rb, line 72
def paths
  paths = [project_root]
  paths += terraform.find_tfvars_files if terraform.pre_0_12?
  paths
end
print_commands_proc() click to toggle source
run_task() click to toggle source
# File lib/terraform/rake_task.rb, line 62
def run_task
  rake_output_message("#{write ? 'Auto-correcting' : 'Inspecting'}" \
                      " Terraform formatting for #{project_root}")

  return if terraform.fmt(paths, tf_flags, &print_commands_proc)

  warn('Terraform file formatting check failed')
  exit 1 if fail_on_error
end
terraform() click to toggle source
# File lib/terraform/rake_task.rb, line 47
def terraform
  @terraform ||= Terraform.new
end
tf_flags() click to toggle source
# File lib/terraform/rake_task.rb, line 78
def tf_flags
  {}.tap do |flags|
    flags[:diff]  = !!diff  # rubocop:disable Style/DoubleNegation
    flags[:write] = !!write # rubocop:disable Style/DoubleNegation
    flags[:check] = !write
    flags[:recursive] = true if !terraform.pre_0_12?
  end
end