class Guard::Terraform

Guard plugin for checking Terraform configuration style

Constants

DEFAULT_OPTIONS

Attributes

options[R]
terraform[R]
tf_flags[R]

Public Class Methods

new(**options) click to toggle source
Calls superclass method
# File lib/guard/terraform.rb, line 24
def initialize(**options)
  super

  options = DEFAULT_OPTIONS.merge(options)

  @options = {
    all_on_start: options[:all_on_start],
  }

  # Enforce boolean values as they get passed to CLI
  @tf_flags = {
    diff:  !!options[:diff],  # rubocop:disable Style/DoubleNegation
    write: !!options[:write], # rubocop:disable Style/DoubleNegation
    check: !options[:write],
  }

  @terraform = ::Terraform.new
end

Public Instance Methods

mungle_paths(paths) click to toggle source

Terraform 0.12+ only supports checking directories, older versions accept also files

# File lib/guard/terraform.rb, line 114
def mungle_paths(paths)
  return paths if terraform.pre_0_12?

  paths.map do |path|
    File.directory?(path) ? path : File.dirname(path)
  end.uniq
end
notify_failure() click to toggle source
# File lib/guard/terraform.rb, line 104
def notify_failure
  Notifier.notify(
    'Terraform format check failed',
    title: self.class.to_s,
    image: :failed
  )
end
print_pre_run_message(paths) click to toggle source
run(paths, **extra_flags) click to toggle source
# File lib/guard/terraform.rb, line 74
def run(paths, **extra_flags)
  print_pre_run_message(paths)

  flags = tf_flags.merge(extra_flags)

  result = terraform.fmt(paths, flags) do |_, cmd|
    UI.debug(Shellwords.join(cmd))
  end

  return if result

  notify_failure
  throw(:task_has_failed)
end
run_all() click to toggle source
# File lib/guard/terraform.rb, line 52
def run_all
  paths = ['.']
  extra_flags = {}

  if terraform.pre_0_12?
    # Terraform v<0.12 does not check *.tfvars by default,
    # so find and check them separately
    paths += terraform.find_tfvars_files
  else
    # Terraform 0.12+ does not check recursively by default
    extra_flags[:recursive] = true
  end

  run(paths, extra_flags)
end
run_on_additions(paths)
run_on_modifications(paths) click to toggle source
# File lib/guard/terraform.rb, line 68
def run_on_modifications(paths)
  run(mungle_paths(paths))
end
Also aliased as: run_on_additions
start() click to toggle source
# File lib/guard/terraform.rb, line 43
def start
  UI.info "#{self.class} started"
  UI.info "Terraform version: #{terraform.version}"

  raise(StandardError, 'Terraform not found') if !terraform.version

  run_all if options[:all_on_start]
end