Skip to content

Instantly share code, notes, and snippets.

@tsprlng
Created July 15, 2024 14:20
Show Gist options
  • Save tsprlng/f49a0ed20ac9bb15dbe4ddb3c584c2ad to your computer and use it in GitHub Desktop.
Save tsprlng/f49a0ed20ac9bb15dbe4ddb3c584c2ad to your computer and use it in GitHub Desktop.
Summarize terraform planning output
#!/usr/bin/ruby
ErrorIfNonEmpty = ARGV.delete('-e') != nil
RemoveColors = ARGV.delete('-c') != nil
HeadersOnly = ARGV.delete('-l') != nil
def isHeader(l)
l.include? 'resource'
end
def isChange(l)
l =~ %r{^ *(~|-|\+)[^-]}
end
def isPointless(l)
l.include? '(known after apply)' and / +\+/ =~ l
end
$isPlanningFile = false
$foundActions = false
$foundError = false
ARGF.each_line do |l|
if not $isPlanningFile
$isPlanningFile = true if l.include? 'Refreshing Terraform state'
elsif not $foundActions and not $foundError
$foundActions = true if l.include? 'will perform'
$foundError = true if l.include? 'Error'
else
withoutColor = l.gsub %r{\x1b\[[0-9;]*[a-zA-Z]}, '' # Strip off vt100 control chars so we can parse the line (but retain them on `l` for output)
if isHeader withoutColor
puts (RemoveColors ? withoutColor : l).sub('resource "','').sub('" "','.').sub('" {','')
elsif withoutColor.include? 'forces' and not HeadersOnly
puts RemoveColors ? withoutColor : l
elsif not HeadersOnly and isChange withoutColor and not isPointless(withoutColor)
darkWhite = RemoveColors ? '' : "\x1b[2;37m"
puts "#{darkWhite}#{withoutColor}"
end
end
end
unless $isPlanningFile
$stderr.puts "Input file doesn't look like Terraform planning output... :\\"
exit 4
end
if $foundError
$stderr.puts "Plan failed :("
exit 4
end
$stderr.puts "Nothing to do! :)" unless $foundActions
exit ($foundActions ? 1 : 0) if ErrorIfNonEmpty
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment