Sha256: 2423ed0ebd8aaf3226bffdb472307fe14f6dd6ab9ecf333f9ad642762531aff9

Contents?: true

Size: 1.83 KB

Versions: 3

Compression:

Stored size: 1.83 KB

Contents

class Terraspace::CLI::Fmt
  class Runner
    include Terraspace::CLI::Concerns::SourceDirs
    include Terraspace::Util::Logging

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

    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

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

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

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

    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

    SKIP_PATTERN = /\.skip$/
    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

  private
    def skip?(path)
      !!(path =~ SKIP_PATTERN)
    end

    def erb?(path)
      IO.readlines(path).detect { |l| l.include?('<%') }
    end

    def tf_files
      Dir.glob("#{Terraspace.root}/#{@dir}/**/*.{tf,skip}").select { |p| File.file?(p) }
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
terraspace-2.2.17 lib/terraspace/cli/fmt/runner.rb
terraspace-2.2.16 lib/terraspace/cli/fmt/runner.rb
terraspace-2.2.15 lib/terraspace/cli/fmt/runner.rb