add zsh functions

This commit is contained in:
Jason Swank 2025-10-20 19:44:10 +00:00
parent 8fe8be5ad7
commit a474758772
6 changed files with 68 additions and 0 deletions

View File

@ -0,0 +1,25 @@
aws-eksc() {
local clusters cluster_name
# Fetch clusters into a zsh array; exit if the command fails.
clusters=(${(f)"$(aws eks list-clusters 2>/dev/null | jq -r '.clusters[]')"})
if (( ${#clusters[@]} == 0 )); then
echo "No EKS clusters found in the current region/profile." >&2
return 1
elif (( ${#clusters[@]} == 1 )); then
cluster_name="${clusters[1]}"
echo "Auto-selecting only cluster: ${cluster_name}"
else
# Prompt for selection if more than one cluster exists.
cluster_name=$(printf '%s\n' "${clusters[@]}" | fzf --prompt="Select EKS Cluster > ")
fi
# Proceed if a cluster name was selected (handles fzf cancellation).
if [[ -n "$cluster_name" ]]; then
aws eks update-kubeconfig --name "$cluster_name" 2>&1 >/dev/null
else
echo "No cluster selected." >&2
return 1
fi
}

View File

@ -0,0 +1,4 @@
aws-login() {
# eval $(okta-aws-cli web) && export AWS_ENVIRONMENT="$(aws iam list-account-aliases | jq -r '.AccountAliases[0]')"
export AWS_ENVIRONMENT=$(aws iam list-account-aliases | jq -r '.AccountAliases[0]')
}

View File

@ -0,0 +1,14 @@
aws-ssm-connect() {
local instance_id
instance_id=$(aws ec2 describe-instances \
--filters "Name=instance-state-name,Values=running" \
--query 'Reservations[].Instances[].[InstanceId,Tags[?Key==`Name`].Value|[0],PrivateIpAddress,InstanceType]' \
--output text \
| awk '{printf "%-20s %-40s %-15s %s\n", $1, $2, $3, $4}' \
| fzf --header="Instance ID Name IP Type" \
| awk '{print $1}')
[[ -z "$instance_id" ]] && return 1
aws ssm start-session --target "$instance_id"
}

View File

@ -0,0 +1,7 @@
function ssh-keygen-new() {
local key_type="${1:-ed25519}"
local key_file="${2:-$HOME/.ssh/id_${key_type}}"
local comment="${3:-$(printf '%s@%s' $(whoami) $(hostname))}"
ssh-keygen -t "$key_type" -f "$key_file" -C "$comment"
}

View File

@ -0,0 +1,3 @@
tf-changes() {
tofu plan -json | jq -r 'select(.type == "planned_change") | "\(.change.resource.addr)\t\(.change.action)"' | sort -u
}

View File

@ -0,0 +1,15 @@
tf-vars() {
if [ -z "$1" ]; then
echo "Usage: tf_env <environment>"
return 1
fi
local env_name="$1"
export TF_CLI_ARGS_init="-backend-config=config/backend-${env_name}.conf -reconfigure"
export TF_CLI_ARGS_plan="-var-file=config/${env_name}.tfvars"
export TF_CLI_ARGS_apply="-var-file=config/${env_name}.tfvars"
export TF_CLI_ARGS_import="-var-file=config/${env_name}.tfvars"
export TF_CLI_ARGS_destroy="-var-file=config/${env_name}.tfvars"
echo "Terraform environment variables set for '${env_name}'."
}