terratrash 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/lib/terratrash.rb +151 -0
  3. data/lib/version.rb +7 -0
  4. metadata +49 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 713aba792eee558047dca021febbdca3c57ced109c9544108547b74a469689c7
4
+ data.tar.gz: aa71a496bb8e83fcfe0d5377ff6ad0abf765663b7368368626422ba83698a490
5
+ SHA512:
6
+ metadata.gz: 2bc5639b326cbe48260cd87ffc1ceec483a9dcead26fa98fc354e575dbcef8e73fce0700c929ac88284cbc3eeaa764aae054250a9413eba6a7e0222f144a5453
7
+ data.tar.gz: 9fb816b459d65183d09fba4bff83bcbc4ed8ca55c1049ca0cfb988127b1c7b01612910f232e83e3909a0bd79e2f3ca556dd24265da886b18108becd728d17c31
data/lib/terratrash.rb ADDED
@@ -0,0 +1,151 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "logger"
4
+
5
+ class Terratrash
6
+ def initialize(
7
+ logger: nil,
8
+ remove_warnings: true,
9
+ remove_notes: true,
10
+ remove_pipe_blocks: true,
11
+ add_final_newline: true
12
+ )
13
+ @log = logger || Logger.new($stdout, level: ENV.fetch("LOG_LEVEL", "INFO").upcase)
14
+ @remove_warnings = remove_warnings # if true, remove terraform warnings
15
+ @remove_notes = remove_notes # if true, remove terraform notes
16
+ @remove_pipe_blocks = remove_pipe_blocks # if true, remove terraform blocks with piped characters
17
+ @add_final_newline = add_final_newline # if true, add a newline to the end of the output
18
+ end
19
+
20
+ # Like the clean method, but it will never raise an error
21
+ # If an error is raised, it will return the original input
22
+ # :param terraform: a string of the terraform output
23
+ # :return: a string of the cleaned terraform output or the original input
24
+ def clean!(terraform)
25
+ return clean(terraform)
26
+ rescue StandardError => e
27
+ # :nocov:
28
+ @log.error("error cleaning terraform output: #{e} - returning original input")
29
+ @log.error("backtrace: #{e.backtrace.join("\n")}")
30
+ return terraform
31
+ # :nocov:
32
+ end
33
+
34
+ # The main method of the terratrash class
35
+ # This method takes a string, and removes all the unwanted terraform text
36
+ # The goal of this method is to be left with a minimal, human-readable output
37
+ # :param terraform: a string of the terraform output
38
+ # :return: a string of the cleaned terraform output
39
+ def clean(terraform)
40
+ # split the output into an array of lines
41
+ terraform_array = terraform.split("\n")
42
+
43
+ # remove any items from the array that include any bits of the following strings
44
+ terraform_array = remove_terraform_loading_messages!(terraform_array)
45
+ terraform_array = remove_github_actions_output!(terraform_array)
46
+
47
+ # if @remove_pipe_blocks is true, remove any items from the array that include pipes
48
+ terraform_array = remove_pipe_blocks!(terraform_array) if @remove_pipe_blocks
49
+
50
+ # find what position in the array the line "Initializing plugins and modules..." is at
51
+ initializing_position = terraform_array.index("Initializing plugins and modules...")
52
+
53
+ # if the line is not found, print a warning
54
+ unless initializing_position.nil?
55
+ # remove all the lines from the array up to that position
56
+ terraform_array.slice!(0..initializing_position)
57
+ # if the very first line is now empty or a newline, remove it as well
58
+ terraform_array.delete_at(0) if terraform_array[0].strip == ""
59
+ end
60
+
61
+ # re-join the array into a string (text is what we will call this string)
62
+ text = terraform_array.join("\n")
63
+
64
+ # terraform warnings and notes cleanup
65
+ text = remove_warnings!(text) if @remove_warnings
66
+ text = remove_notes!(text) if @remove_notes
67
+
68
+ # whitespace cleanup
69
+ text = top_and_bottom_cleanup!(text)
70
+
71
+ return text
72
+ end
73
+
74
+ private
75
+
76
+ # Helper function to remove GitHub Actions output / debug messages
77
+ # :input input_array: an array of strings
78
+ # :return input_array: the same array of strings, but with GitHub Actions output removed
79
+ def remove_github_actions_output!(input_array)
80
+ @log.debug("removing GitHub Actions output")
81
+ input_array.delete_if { |item| item.include?("::debug::") }
82
+ input_array.delete_if { |item| item.include?("[command]/home/runner/work/") }
83
+ return input_array
84
+ end
85
+
86
+ # Helper function to remove Terraform loading messages
87
+ # :input input_array: an array of strings
88
+ # :return input_array: the same array of strings, but with loading messages removed
89
+ def remove_terraform_loading_messages!(input_array)
90
+ @log.debug("removing Terraform 'loading' messages")
91
+ input_array.delete_if { |item| item.include?("Reading...") }
92
+ input_array.delete_if { |item| item.include?("Refreshing state...") }
93
+ input_array.delete_if { |item| item.include?("Read complete") }
94
+ input_array.delete_if { |item| item.include?("Still reading...") }
95
+ return input_array
96
+ end
97
+
98
+ # Helper function to remove pipe blocks
99
+ # :input input_array: an array of strings
100
+ # :return input_array: the same array of strings, but with pipe blocks removed
101
+ def remove_pipe_blocks!(input_array)
102
+ @log.debug("removing pipe blocks")
103
+ input_array.delete_if { |item| item.include?("╷") }
104
+ input_array.delete_if { |item| item.include?("│") }
105
+ input_array.delete_if { |item| item.include?("╵") }
106
+ return input_array
107
+ end
108
+
109
+ # Helper function to clean up the top and bottom of the 'output' before it is returned
110
+ # :input text: a string of text
111
+ # :return text: the same string of text, but with the top and bottom cleaned up
112
+ def top_and_bottom_cleanup!(text)
113
+ @log.debug("cleaning up the top and bottom of the output")
114
+ # remove any leading newline(s) characters from the beginning of the string
115
+ text.gsub!(/\A\n*/, "")
116
+ # remove any trailing newline(s) characters from the end of the string
117
+ text.gsub!(/\n*\z/, "")
118
+ # if the text ends with three or more '─' characters, remove them
119
+ text.gsub!(/─{3,}\z/, "")
120
+ # again, remove any trailing newline(s) characters from the end of the string
121
+ text.gsub!(/\n*\z/, "")
122
+ # if three or more consecutive newline characters are found, replace them with two newline characters
123
+ text.gsub!(/\n{3,}/, "\n\n")
124
+
125
+ if @add_final_newline && (text[-1] != "\n")
126
+ # if the text does not end with a newline character, add one
127
+ text += "\n"
128
+ end
129
+
130
+ return text
131
+ end
132
+
133
+ # Helper function to remove Terraform notes
134
+ # :input text: a string of text
135
+ # :return text: the same string of text, but with notes removed
136
+ def remove_notes!(text)
137
+ @log.debug("removing Terraform 'notes'")
138
+ # removing terraform plan -out note
139
+ text.gsub!(/Note: You didn't use the -out option.*?actions if you run "terraform apply" now./m, "")
140
+ return text
141
+ end
142
+
143
+ # Helper function to remove Terraform warnings
144
+ # :input text: a string of text
145
+ # :return text: the same string of text, but with warnings removed
146
+ def remove_warnings!(text)
147
+ @log.debug("removing Terraform 'warnings'")
148
+ text.gsub!(/Warning:.*?similar warnings elsewhere\)/m, "")
149
+ return text
150
+ end
151
+ end
data/lib/version.rb ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Terratrash
4
+ module Version
5
+ VERSION = "1.0.0"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: terratrash
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Grant Birkinbine
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-08-03 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: 'A Ruby gem to discard (trash) unwanted Terraform output for humans
14
+
15
+ '
16
+ email: grant.birkinbine@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/terratrash.rb
22
+ - lib/version.rb
23
+ homepage: https://github.com/grantbirki/terratrash
24
+ licenses:
25
+ - MIT
26
+ metadata:
27
+ source_code_uri: https://github.com/grantbirki/terratrash
28
+ documentation_uri: https://github.com/grantbirki/terratrash
29
+ bug_tracker_uri: https://github.com/grantbirki/terratrash/issues
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: 3.0.0
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubygems_version: 3.4.10
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: A Ruby gem to discard (trash) unwanted Terraform output for humans
49
+ test_files: []