class Terraspace::CLI::Fmt::Runner

Constants

SKIP_PATTERN

Public Class Methods

new(dir) click to toggle source
# File lib/terraspace/cli/fmt/runner.rb, line 7
def initialize(dir)
  @dir = dir
end

Public Instance Methods

format!() click to toggle source
# File lib/terraspace/cli/fmt/runner.rb, line 11
def format!
  logger.info @dir.color(:green)
  Dir.chdir(@dir) do
    skip_rename
    begin
      terraform_fmt
    ensure
      restore_rename
    end
  end
end
restore_rename() click to toggle source
# File lib/terraspace/cli/fmt/runner.rb, line 43
def restore_rename
  tf_files.each do |path|
    if skip?(path) && erb?(path)
      FileUtils.mv(path, path.sub(SKIP_PATTERN, '')) # original name
    end
  end
end
sh(command) click to toggle source
# File lib/terraspace/cli/fmt/runner.rb, line 35
def sh(command)
  logger.debug("=> #{command}")
  success = system(command)
  return if success
  logger.info "WARN: There were some errors running terraform fmt for files in #{@dir}:".color(:yellow)
  logger.info "The errors are shown above"
end
skip_rename() click to toggle source
# File lib/terraspace/cli/fmt/runner.rb, line 23
def skip_rename
  tf_files.each do |path|
    if !skip?(path) && erb?(path)
      FileUtils.mv(path, "#{path}.skip")
    end
  end
end
terraform_fmt() click to toggle source
# File lib/terraspace/cli/fmt/runner.rb, line 31
def terraform_fmt
  sh "terraform fmt"
end

Private Instance Methods

erb?(path) click to toggle source
# File lib/terraspace/cli/fmt/runner.rb, line 56
def erb?(path)
  IO.readlines(path).detect { |l| l.include?('<%') }
end
skip?(path) click to toggle source
# File lib/terraspace/cli/fmt/runner.rb, line 52
def skip?(path)
  !!(path =~ SKIP_PATTERN)
end
tf_files() click to toggle source
# File lib/terraspace/cli/fmt/runner.rb, line 60
def tf_files
  Dir.glob("#{Terraspace.root}/#{@dir}/**/*.{tf,skip}").select { |p| File.file?(p) }
end