Last active
November 20, 2020 02:07
-
-
Save stevehodgkiss/3a41ec597c051432787fe7c9719f05d4 to your computer and use it in GitHub Desktop.
stack master fzf helper
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
stackmaster-stacks() { | |
ruby -ryaml -e 'YAML.load(File.read("stack_master.yml"))["stacks"].each { |region, stacks| stacks.each { |s, _| puts "#{region} #{s}" } }' | |
} | |
stackmaster-default-account() { | |
ruby -ryaml -e 'puts Array(YAML.load(File.read("stack_master.yml")).dig("stack_defaults", "allowed_accounts")).first' | |
} | |
awsvault-accounts() { | |
cat ~/.aws/config | \ | |
grep '^\[profile' | \ | |
cut -d' ' -f2 | \ | |
tr -d ']' | |
} | |
faws() { | |
awsvault-accounts | fzf --preview "cat ~/.aws/config | grep '^\[profile {}' -A 3" | |
} | |
sm-commands() { | |
local commands=(apply outputs validate diff compile events resources list lint nag status tidy delete drift) | |
printf '%s\n' "${commands[@]}" | |
} | |
sm() { | |
local sm_command="${1:-}" | |
if ! [ -f stack_master.yml ]; then | |
echo "stack_master.yml not found" | |
return | |
fi | |
region_and_stack=$(stackmaster-stacks | \ | |
fzf --header="Select stack" \ | |
--color 'fg:#bbccdd,fg+:#ddeeff,preview-bg:#223344,border:#778899' \ | |
--preview 'ruby -ryaml -e "region, stack = {}.split(\" \", 2) | |
stack = YAML.load(File.read(\"stack_master.yml\")).dig(\"stacks\", region, stack) | |
puts YAML.dump(stack) | |
puts | |
Array(stack[\"parameter_files\"]).each do |file| | |
path = \"parameters/#{file}\" | |
if File.exist?(path) | |
puts \"# #{path}\" | |
puts File.read(path) | |
end | |
end"') | |
if [[ "$region_and_stack" == "" ]]; then | |
return | |
fi | |
if [[ "$sm_command" == "" ]]; then | |
sm_command=$(sm-commands | fzf --header "Select stack_master command") | |
if [[ "$sm_command" == "" ]]; then | |
return | |
fi | |
fi | |
command=$(awsvault-accounts | \ | |
xargs -I % echo "aws-vault exec % -- bundle exec stack_master $sm_command $region_and_stack" | \ | |
fzf --header="Select command to execute" --query="$(stackmaster-default-account)") | |
if [[ "$command" != "" ]]; then | |
echo $command | |
eval $command | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment