Class: Terratrash

Inherits:
Object
  • Object
show all
Defined in:
lib/terratrash.rb

Instance Method Summary collapse

Constructor Details

#initialize(logger: nil, remove_warnings: true, remove_notes: true, remove_pipe_blocks: true, add_final_newline: true) ⇒ Terratrash

Returns a new instance of Terratrash.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/terratrash.rb', line 6

def initialize(
  logger: nil,
  remove_warnings: true,
  remove_notes: true,
  remove_pipe_blocks: true,
  add_final_newline: true
)
  @log = logger || setup_logger
  @remove_warnings = remove_warnings # if true, remove terraform warnings
  @remove_notes = remove_notes # if true, remove terraform notes
  @remove_pipe_blocks = remove_pipe_blocks # if true, remove terraform blocks with piped characters
  @add_final_newline = add_final_newline # if true, add a newline to the end of the output
end

Instance Method Details

#clean(terraform) ⇒ Object

The main method of the terratrash class This method takes a string, and removes all the unwanted terraform text The goal of this method is to be left with a minimal, human-readable output :param terraform: a string of the terraform output :return: a string of the cleaned terraform output



39
40
41
42
43
44
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
# File 'lib/terratrash.rb', line 39

def clean(terraform)
  # split the output into an array of lines
  terraform_array = terraform.split("\n")

  # remove any items from the array that include any bits of the following strings
  terraform_array = remove_terraform_loading_messages!(terraform_array)
  terraform_array = remove_github_actions_output!(terraform_array)

  # if @remove_pipe_blocks is true, remove any items from the array that include pipes
  terraform_array = remove_pipe_blocks!(terraform_array) if @remove_pipe_blocks

  # find what position in the array the line "Initializing plugins and modules..." is at
  initializing_position = terraform_array.index("Initializing plugins and modules...")

  # if the line is not found, print a warning
  unless initializing_position.nil?
    # remove all the lines from the array up to that position
    terraform_array.slice!(0..initializing_position)
    # if the very first line is now empty or a newline, remove it as well
    terraform_array.delete_at(0) if terraform_array[0].strip == ""
  end

  # re-join the array into a string (text is what we will call this string)
  text = terraform_array.join("\n")

  # terraform warnings and notes cleanup
  text = remove_warnings!(text) if @remove_warnings
  text = remove_notes!(text) if @remove_notes

  # whitespace cleanup
  text = top_and_bottom_cleanup!(text)

  return text
end

#clean!(terraform) ⇒ Object

Like the clean method, but it will never raise an error If an error is raised, it will return the original input :param terraform: a string of the terraform output :return: a string of the cleaned terraform output or the original input



24
25
26
27
28
29
30
31
32
# File 'lib/terratrash.rb', line 24

def clean!(terraform)
  return clean(terraform)
rescue StandardError => e
  # :nocov:
  @log.error("error cleaning terraform output: #{e} - returning original input")
  @log.error("backtrace: #{e.backtrace.join("\n")}")
  return terraform
  # :nocov:
end