1
0

generalize iac aliases

This commit is contained in:
2025-03-29 21:05:05 +01:00
parent bffcdd8460
commit ba2b1aa855

View File

@@ -13,18 +13,96 @@ alias egrep='egrep --color=auto'
alias d='dirs -v'
for index ({1..9}) alias "c$index"="cd +${index}"; unset index
alias tf="aws-vault exec feedzai-main -d 8h -- terraform"
alias tfi="aws-vault exec feedzai-main -d 8h -- terraform init"
alias tfp="aws-vault exec feedzai-main -d 8h -- terraform plan -lock=false"
alias tfa="aws-vault exec feedzai-main -d 8h -- terraform apply"
alias tfu="aws-vault exec feedzai-main -d 8h -- terraform get -update"
_get_aws_config_path() {
local config_path="${AWS_CONFIG_FILE:-$HOME/.aws/config}"
echo "$config_path"
}
alias tg="aws-vault exec feedzai-main -d 8h -- terragrunt"
alias tgi="aws-vault exec feedzai-main -d 8h -- terragrunt init"
alias tgp="aws-vault exec feedzai-main -d 8h -- terragrunt plan -lock=false"
alias tga="aws-vault exec feedzai-main -d 8h -- terragrunt apply"
alias tgu="aws-vault exec feedzai-main -d 8h -- terragrunt get -update"
alias tgg="aws-vault exec feedzai-main -d 8h -- terragrunt graph-dependencies"
# This label will be set in a comment in the ~/.aws/config file right before
# the definition of the profile we want to use as the default.
# The format will be `#[<label>]`
# Example, using "admin" as the default profile:
#
# > [default]
# > region = eu-south-2
# >
# > #[aws-vault-default] ---> Marking the profile with the label
# > [profile admin]
# > source_profile=default
# > region = eu-south-2
# > role_arn=arn:aws:iam::<account id>:role/MyAdminRole
# >
# > [profile readonly]
# > source_profile=default
# > region = eu-south-2
# > role_arn=arn:aws:iam::<account id>:role/MyReadOnlyRole
#
DEFAULT_AWS_PROFILE_LABEL="aws-vault-default"
_get_default_aws_profile() {
local profile="default"
if grep -q "^#\[$DEFAULT_AWS_PROFILE_LABEL\]" "$(_get_aws_config_path)"; then
profile="$(grep -A3 "^#\[$DEFAULT_AWS_PROFILE_LABEL\]" "$(_get_aws_config_path)" |
grep '^\[profile' |
sed -E 's/\[profile (.+)\]/\1/')"
fi
echo $profile
}
_terra_cmd() {
local aws_vault_profile="$(_get_default_aws_profile)"
local cmd_args=()
if [[ -z "$1" ]]; then
echo "Error: Missing required first argument"
echo "Usage: tf [terraform|terragrunt] [additional args]"
return 1
fi
if [[ "$1" != "terraform" && "$1" != "terragrunt" ]]; then
echo "Error: First argument must be either 'terraform' or 'terragrunt'"
echo "Usage: my_function [terraform|terragrunt] [additional args]"
return 1
fi
# If we get here, the first argument is valid
local -r cmd="$1"
shift
while [[ $# -gt 0 ]]; do
case $1 in
-p|--profile)
if [[ $# -gt 1 ]]; then
aws_vault_profile="$2"
shift 2
else
echo "Error: -p|--profile requires a profile name"
return 1
fi
;;
*)
cmd_args+=("$1")
shift
;;
esac
done
aws-vault exec "$aws_vault_profile" -d 8h -- "$cmd" ${cmd_args[@]}
}
alias tf="_terra_cmd terraform"
alias tfi="tf init"
alias tfp="tf plan -lock=false"
alias tfa="tf apply"
alias tfu="tf get -update"
alias tg="_terra_cmd terragrunt"
alias tgi="tg grunt init"
alias tgp="tg grunt plan -lock=false"
alias tga="tg grunt apply"
alias tgu="tg grunt get -update"
alias tgg="tg grunt graph-dependencies"
alias idot='dot -Tsvg -Goverlap=scale -Grankdir=RL -Gbgcolor="#282a36" -Ncolor="#f8f8f2" -Ecolor="#f8f8f2" -Nfontcolor="#f8f8f2" -Gfontname="PragmataPro Mono Liga Regular" -Gfontsize=13 -Nfontname="PragmataPro" -Nfontsize=13 -Nshape=box -Earrowhead=normal'