Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AWS_VAULT to prompt - option #349

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 47 additions & 19 deletions functions/fish_prompt.fish
Original file line number Diff line number Diff line change
Expand Up @@ -655,32 +655,57 @@ end
# ==============================
# Cloud Tools
# ==============================

function __bobthefish_prompt_aws_vault_profile -S -d 'Show AWS Vault profile'
[ "$theme_display_aws_vault_profile" = 'yes' ]
function __bobthefish_prompt_aws_vault_profile -S -d 'Show AWS SSO profiles sorted by expiry'
[ "$theme_display_aws_vault_profile" = 'yes' -o "$theme_display_aws_profile" = 'yes' ]
or return

[ -n "$AWS_VAULT" -a -n "$AWS_SESSION_EXPIRATION" ]
or return
# Find all SSO cache files
set -l AWS_SSO_CLI_CACHE (grep -Ril --exclude "aws*" "startUrl" ~/.aws/sso/cache)

# Initialize current time
set -l now (date --utc +%s)

set -l profile $AWS_VAULT
# Initialize an array to store session details with a sorting key
set -l sessions

# Parse each file to extract PROFILENAME, expiry, and sort them by expiry time
for file in $AWS_SSO_CLI_CACHE
if test -f $file
# Extract expiresAt and startUrl fields from the file
set -l expiry_date (cat $file | jq -r .expiresAt)
set -l start_url (cat $file | jq -r .startUrl)

# Convert expiry_date to seconds and calculate time remaining
set -l expiry (date -d "$expiry_date" +%s)
set -l diff_secs (math "$expiry - $now")
set -l diff_mins (math "floor($diff_secs / 60)")

# Determine if the session is expired and format time remaining
set -l diff_time "$diff_mins"m
if test $diff_mins -le 0
set diff_time "0m"
else if test $diff_mins -ge 60
set -l hours (math "floor($diff_mins / 60)")
set -l minutes (math "$diff_mins % 60")
set diff_time "$hours"h"$minutes"m
end

set -l now (date --utc +%s)
set -l expiry (date -d "$AWS_SESSION_EXPIRATION" +%s)
set -l diff_mins (math "floor(( $expiry - $now ) / 60)")
# Extract PROFILENAME from startUrl
set -l profile_name (string match -r --groups-only 'https://([^.]+).awsapps.com' $start_url)

set -l diff_time $diff_mins"m"
[ $diff_mins -le 0 ]
and set -l diff_time '0m'
[ $diff_mins -ge 60 ]
and set -l diff_time (math "floor($diff_mins / 60)")"h"(math "$diff_mins % 60")"m"
# Store each session text with expiry time for sorting
set sessions "$sessions" "$expiry|AWS SSO $profile_name ($diff_time)"
end
end

set -l segment $profile ' (' $diff_time ')'
set -l status_color $color_aws_vault
[ $diff_mins -le 0 ]
and set -l status_color $color_aws_vault_expired
# Sort sessions by the expiry timestamp in descending order (furthest to closest expiry)
set -l sorted_sessions (printf "%s\n" $sessions | sort -t'|' -r -k1)

__bobthefish_start_segment $status_color
# Remove sort keys and combine session texts into a single segment string
set -l segment (printf "%s\n" $sorted_sessions | sed 's/^[^|]*|//' | string join " | ")

# Display the final segment with color
__bobthefish_start_segment $color_aws_vault
echo -ns $segment ' '
end

Expand Down Expand Up @@ -1202,3 +1227,6 @@ function fish_prompt -d 'bobthefish, a fish theme optimized for awesome'

__bobthefish_finish_segments
end
if set -q VIRTUAL_ENV
echo -n -s (set_color -b blue white) "(" (basename "$VIRTUAL_ENV") ")" (set_color normal) " "
end