Class: Terraspace::CLI::Fmt::Runner

Inherits:
Object
  • Object
show all
Includes:
Concerns::SourceDirs, Util::Logging
Defined in:
lib/terraspace/cli/fmt/runner.rb

Constant Summary collapse

SKIP_PATTERN =
/\.skip$/

Instance Method Summary collapse

Methods included from Util::Logging

#logger

Methods included from Concerns::SourceDirs

#app_source_dirs, #source_dirs

Constructor Details

#initialize(dir) ⇒ Runner

Returns a new instance of Runner.



7
8
9
# File 'lib/terraspace/cli/fmt/runner.rb', line 7

def initialize(dir)
  @dir = dir
end

Instance Method Details

#format!Object



11
12
13
14
15
16
17
18
19
20
21
# 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_renameObject



43
44
45
46
47
48
49
# 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) ⇒ Object



35
36
37
38
39
40
41
# 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_renameObject



23
24
25
26
27
28
29
# 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_fmtObject



31
32
33
# File 'lib/terraspace/cli/fmt/runner.rb', line 31

def terraform_fmt
  sh "terraform fmt"
end