Class: TerraformWrapper::Tasks::Clean

Inherits:
Rake::TaskLib
  • Object
show all
Includes:
Shared::Logging
Defined in:
lib/terraform-wrapper/tasks/clean.rb

Constant Summary collapse

@@glob_directories =
{
  'Terraform Modules' => '**/.terraform'
}
@@glob_files =
{
  'Crash Logs' => '**/crash.log',
  'Graph Images' => '**/graph.png',
  'Terraform States' => '**/*.tfstate',
  'Terraform State Backups' => '**/*.tfstate.backup'
}

Instance Method Summary collapse

Methods included from Shared::Logging

configure_logger_for, logger_for

Constructor Details

#initialize(code:) {|_self| ... } ⇒ Clean

Returns a new instance of Clean.

Yields:

  • (_self)

Yield Parameters:



35
36
37
38
39
40
41
# File 'lib/terraform-wrapper/tasks/clean.rb', line 35

def initialize(code:)
  @code = code

  yield self if block_given?

  clean_task
end

Instance Method Details

#clean_taskObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/terraform-wrapper/tasks/clean.rb', line 45

def clean_task
  desc 'Cleans a Terraform infrastructure component workspace of any downloaded providers and modules.'
  task :clean do |_t, _args|
    logger.info("Cleaning Terraform component: #{@code.name}...")

    directory_counter = 0
    @@glob_directories.each do |key, value|
      logger.info("Cleaning directories: #{key}")
      directory_item_counter = 0

      directories = Dir.glob(File.join(@code.path, value))
      directories.each do |directory|
        if delete_directory(directory: directory)
          directory_item_counter += 1
          directory_counter      += 1
        end
      end
      logger.success("Cleaned: #{directory_item_counter} directories.")
    end

    file_counter = 0
    @@glob_files.each do |key, value|
      logger.info("Cleaning files: #{key}")
      file_item_counter = 0

      files = Dir.glob(File.join(@code.path, value))
      files.each do |file|
        if delete_file(file: file)
          file_item_counter += 1
          file_counter      += 1
        end
      end
      logger.success("Cleaned: #{file_item_counter} files.")
    end

    logger.success("Cleaned total: #{directory_counter} directories and: #{file_counter} files.")
  end
end