From a47475877207ea7a63c788d181d75638300c22af Mon Sep 17 00:00:00 2001 From: Jason Swank Date: Mon, 20 Oct 2025 19:44:10 +0000 Subject: [PATCH] add zsh functions --- zsh/.local/share/zsh/functions/aws-eksc | 25 +++++++++++++++++++ zsh/.local/share/zsh/functions/aws-login | 4 +++ .../share/zsh/functions/aws-ssm-connect | 14 +++++++++++ zsh/.local/share/zsh/functions/ssh-keygen-new | 7 ++++++ zsh/.local/share/zsh/functions/tf-changes | 3 +++ zsh/.local/share/zsh/functions/tf-vars | 15 +++++++++++ 6 files changed, 68 insertions(+) create mode 100644 zsh/.local/share/zsh/functions/aws-eksc create mode 100644 zsh/.local/share/zsh/functions/aws-login create mode 100644 zsh/.local/share/zsh/functions/aws-ssm-connect create mode 100644 zsh/.local/share/zsh/functions/ssh-keygen-new create mode 100644 zsh/.local/share/zsh/functions/tf-changes create mode 100644 zsh/.local/share/zsh/functions/tf-vars diff --git a/zsh/.local/share/zsh/functions/aws-eksc b/zsh/.local/share/zsh/functions/aws-eksc new file mode 100644 index 0000000..07012c9 --- /dev/null +++ b/zsh/.local/share/zsh/functions/aws-eksc @@ -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 +} diff --git a/zsh/.local/share/zsh/functions/aws-login b/zsh/.local/share/zsh/functions/aws-login new file mode 100644 index 0000000..3e1145d --- /dev/null +++ b/zsh/.local/share/zsh/functions/aws-login @@ -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]') +} diff --git a/zsh/.local/share/zsh/functions/aws-ssm-connect b/zsh/.local/share/zsh/functions/aws-ssm-connect new file mode 100644 index 0000000..92cb2d4 --- /dev/null +++ b/zsh/.local/share/zsh/functions/aws-ssm-connect @@ -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" +} diff --git a/zsh/.local/share/zsh/functions/ssh-keygen-new b/zsh/.local/share/zsh/functions/ssh-keygen-new new file mode 100644 index 0000000..c98a53e --- /dev/null +++ b/zsh/.local/share/zsh/functions/ssh-keygen-new @@ -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" +} diff --git a/zsh/.local/share/zsh/functions/tf-changes b/zsh/.local/share/zsh/functions/tf-changes new file mode 100644 index 0000000..e3abcf5 --- /dev/null +++ b/zsh/.local/share/zsh/functions/tf-changes @@ -0,0 +1,3 @@ +tf-changes() { + tofu plan -json | jq -r 'select(.type == "planned_change") | "\(.change.resource.addr)\t\(.change.action)"' | sort -u +} diff --git a/zsh/.local/share/zsh/functions/tf-vars b/zsh/.local/share/zsh/functions/tf-vars new file mode 100644 index 0000000..d1e3a7f --- /dev/null +++ b/zsh/.local/share/zsh/functions/tf-vars @@ -0,0 +1,15 @@ +tf-vars() { + if [ -z "$1" ]; then + echo "Usage: tf_env " + 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}'." +}