1
0
Files
dotfiles/zsh/.zsh_aliases

162 lines
4.2 KiB
Plaintext

alias tree='tree -CF --du -h'
alias cbonsai='cbonsai --live --infinite --time 0.3 --base 2 --wait 2 --leaf "&,#,$,*,@"'
alias tmux_main='tmux new-session -s main'
alias tmux_secondary_win='tmux new-session -t main -s secondary'
# enable color support of ls and also add handy aliases
alias ls='ls --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
alias d='dirs -v'
# shfmt needs to ignore this because it is a zsh specific syntax
# shfmt:ignore:start
for index ({1..9}) alias "c$index"="cd +${index}"; unset index
# shfmt:ignore:end
_get_aws_config_path() {
local config_path="${AWS_CONFIG_FILE:-$HOME/.aws/config}"
echo "$config_path"
}
# 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 value>]`
# Example, using "admin" as the default profile:
#
# > [default]
# > region = eu-south-2
# >
# > #[<label value>] ---> 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: _terra_cmd [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: _terra_cmd [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 init"
alias tgp="tg plan -lock=false"
alias tga="tg apply"
alias tgu="tg get -update"
alias curltime="curl -w \"@$HOME/.curl-format.txt\" -o /dev/null -s "
alias vi=nvim
alias vim=nvim
git_root() {
# Check if we're in a git repository
if ! git rev-parse --is-inside-work-tree &>/dev/null; then
echo "Error: Not in a git repository" >&2
return 1
fi
# Get the root of the git repository
local git_root
git_root=$(git rev-parse --show-toplevel)
if [ -n "$git_root" ]; then
echo "Changing directory to git root: $git_root"
cd "$git_root"
return 0
else
echo "Error: Could not determine git root directory" >&2
return 1
fi
}
alias groot="git_root"
git_push() {
# Check if we're in a git repository
if ! git rev-parse --is-inside-work-tree &>/dev/null; then
echo "Error: Not in a git repository" >&2
return 1
fi
local branch
branch=$(git branch --show-current)
git push origin "$branch" $@
}
alias gpush="git_push"
passgen() {
length=${1:-"12"}
if [[ "$length" == <-> ]]; then
echo $(cat /dev/urandom| base64 | head -c "$length")
else
echo "passgen() takes a positive integer as first argument"
echo "got $length"
return 1
fi
}