Created
April 26, 2021 09:29
-
-
Save jaysonsantos/34473882ad050d2dbf81e06b6bf8cdba to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
""" | |
Generate colored entries for the extension AWS Extend Switch Roles [1]. | |
[1] https://chrome.google.com/webstore/detail/aws-extend-switch-roles/jpmkfafbacpgapdghgdpembnojdlgkdl | |
""" | |
from pathlib import Path | |
from configparser import ConfigParser | |
CONFIG = Path("~/.aws/config").expanduser() | |
COLOR_RED = "ff0000" | |
COLOR_BLACK = "000000" | |
COLOR_YELLOW = "ffcf24" | |
COLOR_GREEN = "1ca800" | |
def main(): | |
cp = ConfigParser() | |
with CONFIG.open() as f: | |
cp.read_file(f) | |
for section in cp.values(): | |
if not section.name.startswith("profile "): | |
continue | |
name = section.name.split()[-1] | |
role_arn = section.get("role_arn") | |
if not role_arn: | |
continue | |
account_id = role_arn.split(":")[4] | |
color = guess_color(name) | |
print(f"[{name}]") | |
print(f"role_arn = {role_arn}") | |
print(f"account_id = {account_id}") | |
print(f"color = {color}") | |
print() | |
def guess_color(name): | |
if "prod" in name: | |
return COLOR_RED | |
if "test" in name or "playground" in name: | |
return COLOR_GREEN | |
if "stag" in name: | |
return COLOR_YELLOW | |
return COLOR_BLACK | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment