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, options) ⇒ Runner

Returns a new instance of Runner.



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

def initialize(dir, options)
  @dir, @options = dir, options
end

Instance Method Details

#argsObject



37
38
39
40
41
# File 'lib/terraspace/cli/fmt/runner.rb', line 37

def args
  mod = nil # not needed here
  pass = Terraspace::Terraform::Args::Pass.new(mod, "fmt", @options)
  pass.args.flatten.join(' ')
end

#format!Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/terraspace/cli/fmt/runner.rb', line 10

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

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

#rename_to_skip_fmtObject



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

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

#restore_renameObject



57
58
59
60
61
62
63
# File 'lib/terraspace/cli/fmt/runner.rb', line 57

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



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

def sh(command)
  logger.debug("=> #{command}")
  system(command)
  case $?.exitstatus
  when 2 # errors fmt
    logger.info "WARN: There were some errors running #{Terraspace.terraform_bin} fmt for files in #{@dir}:".color(:yellow)
    logger.info "The errors are shown above"
  when 3 # fmt ran and changes were made
    logger.debug "fmt ran and changes were made unless -check or -write=false"
  end
  $?.exitstatus
end

#terraform_fmtObject



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

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