Last active
August 9, 2018 14:59
-
-
Save antimius/26ea431ba5711d3683b171a083f60ecc to your computer and use it in GitHub Desktop.
Format CloudFormation changesets in CLI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from __future__ import print_function | |
from terminaltables import DoubleTable | |
from termcolor import colored | |
import sys, json | |
def colorize(s): | |
colors = { | |
"Add": "green", | |
"Remove": "red", | |
"Modify": "yellow" | |
} | |
return colored(s.upper(), colors[s]) if colors[s] else s | |
def table_changeset(changeset): | |
table_data = [list(map(lambda x : colored(x, attrs=['bold']), ["Action", "Logical ID", "Physical ID", "Resource Type", "Replacement"]))] | |
for c in changeset["Changes"]: | |
if c["Type"] == "Resource": | |
r = c["ResourceChange"] | |
if (r["Action"] == "Add") or (r["Action"] == "Remove"): | |
table_data.append([colorize(r["Action"]), | |
r["LogicalResourceId"], "", r["ResourceType"], ""]) | |
elif r["Action"] == "Modify": | |
table_data.append([colorize(r["Action"]), | |
r["LogicalResourceId"], r["PhysicalResourceId"], | |
r["ResourceType"], r["Replacement"]]) | |
table_instance = DoubleTable(table_data) | |
return table_instance.table | |
def main(): | |
input = sys.stdin.read() | |
changeset = json.loads(input) | |
print (table_changeset(changeset)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
pip install terminaltables termcolor
to use. Reads STDIN and generates a table like this: