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
22
23
24
# File 'lib/terraspace/cli/fmt/runner.rb', line 11

def format!
  logger.info @dir.color(:green)

  exit_status = nil
  Dir.chdir(@dir) do
    skip_rename
    begin
      exit_status = terraform_fmt
    ensure
      restore_rename
    end
  end
  exit_status
end

#restore_renameObject



48
49
50
51
52
53
54
# File 'lib/terraspace/cli/fmt/runner.rb', line 48

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



38
39
40
41
42
43
44
45
46
# File 'lib/terraspace/cli/fmt/runner.rb', line 38

def sh(command)
  logger.debug("=> #{command}")
  success = system(command)
  unless 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
  $?.exitstatus
end

#skip_renameObject



26
27
28
29
30
31
32
# File 'lib/terraspace/cli/fmt/runner.rb', line 26

def skip_rename
  tf_files.each do |path|
    if !skip?(path) && erb?(path)
      FileUtils.mv(path, "#{path}.skip")
    end
  end
end

#terraform_fmtObject



34
35
36
# File 'lib/terraspace/cli/fmt/runner.rb', line 34

def terraform_fmt
  sh "#{Terraspace.terraform_bin} fmt"
end