Created
July 28, 2017 03:06
-
-
Save igorlg/0c38d88b463c9d45e0a662cf6d23e4a6 to your computer and use it in GitHub Desktop.
Small ruby script to print the Profiles from ~/.aws/config
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
require 'colorize' | |
require 'inifile' | |
show_details = ARGV.include? '-d' | |
filter = ARGV.select { |a| not a.start_with?('-') } | |
filter = '.*' if filter.empty? | |
ini = IniFile.load('/Users/igor/.aws/config') | |
output = [] | |
def random_colors | |
skip_colors = [:black, :light_black, :blue, :light_blue, :default] | |
return String.colors.shuffle.select { |c| not skip_colors.include? c} | |
end | |
def get_account(arn) | |
return arn.scan(/arn:aws:.*:(.*):.*/).last.first | |
end | |
def color_params(ini) | |
colors = random_colors | |
src = { } | |
ini.each do |s,k,v| | |
case k | |
when 'source_profile' then src[v] = v.colorize(colors[src.length]) | |
when 'role_arn' then | |
acct = get_account(v) | |
src[acct] = acct.colorize(colors[src.length % colors.length]) | |
end | |
end | |
return src | |
end | |
def color_profiles(ini) | |
colors = random_colors | |
tar = { } | |
ini.each_section do |s| | |
prof = s.gsub(/^profile /, '') | |
tar[prof] = prof.colorize(colors[tar.length % colors.length]) | |
end | |
return tar | |
end | |
profiles = color_profiles(ini) | |
params = color_params(ini) | |
sections = ini.sections.select { |s| s =~ /#{filter}/ } | |
sections.each do |s| | |
prof = s.gsub(/^profile /, '') | |
output << "Profile #{profiles[prof]}" | |
if show_details | |
if ini[s].has_key?('source_profile') | |
p = ini[s]['source_profile'] | |
output << " Source Profile: #{params[p]}" | |
end | |
if ini[s].has_key?('role_arn') | |
p = ini[s]['role_arn'] | |
output << " Account: #{params[get_account(p)]}" | |
end | |
end | |
end | |
output.each { |s| puts s } |
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
source 'https://rubygems.org' | |
gem 'colorize' | |
gem 'inifile' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment