From fbdd73e0276807ff0cb6ae6925deb40e64ff2da0 Mon Sep 17 00:00:00 2001 From: David Tagatac Date: Sat, 21 Mar 2026 22:33:23 -0700 Subject: [PATCH] Add an AWS module with aws-info function --- modules/aws/README.md | 41 ++++++++++++++++++++++++++ modules/aws/functions/aws-info | 54 ++++++++++++++++++++++++++++++++++ modules/aws/init.zsh | 17 +++++++++++ 3 files changed, 112 insertions(+) create mode 100644 modules/aws/README.md create mode 100644 modules/aws/functions/aws-info create mode 100644 modules/aws/init.zsh diff --git a/modules/aws/README.md b/modules/aws/README.md new file mode 100644 index 0000000000..ed98acec96 --- /dev/null +++ b/modules/aws/README.md @@ -0,0 +1,41 @@ +# AWS + +Integrates the [AWS CLI][1] into the shell environment. + +## Completions + +AWS CLI v2 ships with `aws_completer`. This module enables it automatically when +present. + +## Functions + +- `aws-info` exposes information about the AWS environment via the `$aws_info` + associative array. + +## Theming + +To display the active AWS profile and region in a prompt, define the following +styles in the `prompt_name_setup` function. + +```sh +# %p - profile name. +zstyle ':prezto:module:aws:info:profile' format 'profile:%p ' + +# %r - region name. +zstyle ':prezto:module:aws:info:region' format 'region:%r ' +``` + +Then add `$aws_info[profile]` and `$aws_info[region]` to `$PROMPT` or `$RPROMPT` +and call `aws-info` in the `prompt_name_precmd` hook function. + +The region is read from `$AWS_REGION` or `$AWS_DEFAULT_REGION`; if neither is set, +it falls back to the value in `~/.aws/config` for the active profile. + +## Authors + +_The authors of this module should be contacted via the [issue tracker][2]._ + +- [David Tagatac](https://github.com/tagatac) + +[1]: https://aws.amazon.com/cli/ +[2]: https://github.com/sorin-ionescu/prezto/issues diff --git a/modules/aws/functions/aws-info b/modules/aws/functions/aws-info new file mode 100644 index 0000000000..bc327318c2 --- /dev/null +++ b/modules/aws/functions/aws-info @@ -0,0 +1,54 @@ +# +# Exposes information about the AWS environment via the $aws_info associative +# array. +# +# Authors: +# David Tagatac +# + +# function aws-info { + +local profile +local profile_format +local profile_formatted +local region +local region_format +local region_formatted + +# Clean up previous $aws_info. +unset aws_info +typeset -gA aws_info + +zstyle -s ':prezto:module:aws:info:profile' format 'profile_format' +zstyle -s ':prezto:module:aws:info:region' format 'region_format' + +# Format profile. +if [[ -n "$profile_format" ]]; then + profile="$AWS_PROFILE" + # Fall back to 'default' if the default profile exists in the config file. + if [[ -z "$profile" ]]; then + local config_file="${AWS_CONFIG_FILE:-$HOME/.aws/config}" + if [[ -r "$config_file" ]] && grep -q '^\[default\]' "$config_file"; then + profile='default' + fi + fi + if [[ -n "$profile" ]]; then + zformat -f profile_formatted "$profile_format" "p:$profile" + aws_info[profile]="$profile_formatted" + fi +fi + +# Format region. +if [[ -n "$region_format" ]]; then + region="${AWS_REGION:-$AWS_DEFAULT_REGION}" + # Fall back to the config file value for the active profile. + if [[ -z "$region" ]]; then + region="$(aws configure get region 2>/dev/null)" + fi + if [[ -n "$region" ]]; then + zformat -f region_formatted "$region_format" "r:$region" + aws_info[region]="$region_formatted" + fi +fi + +# } diff --git a/modules/aws/init.zsh b/modules/aws/init.zsh new file mode 100644 index 0000000000..34fb60e974 --- /dev/null +++ b/modules/aws/init.zsh @@ -0,0 +1,17 @@ +# +# Integrates the AWS CLI into the shell environment. +# +# Authors: +# David Tagatac +# + +# Return if requirements are not found. +if (( ! $+commands[aws] )); then + return 1 +fi + +# Load AWS CLI completions. +if (( $+commands[aws_completer] )); then + autoload -Uz bashcompinit && bashcompinit + complete -C aws_completer aws +fi