Skip to content

Commit fd6dc39

Browse files
committed
dx: 🌟 improve aws-profile function
1 parent 750f02a commit fd6dc39

1 file changed

Lines changed: 97 additions & 5 deletions

File tree

zsh/.zshrc

Lines changed: 97 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,105 @@ ip() {
196196

197197
# Cloud aliases
198198
awsprofile() {
199-
local profile
200-
profile=$(aws configure list-profiles | fzf --prompt "Select AWS profile:") || {
201-
echo "❌ No profile selected."
199+
# Check if AWS CLI is installed
200+
if ! command -v aws &>/dev/null; then
201+
echo "Error: AWS CLI is not installed"
202+
return 1
203+
fi
204+
205+
# Get available profiles
206+
local profiles
207+
profiles=$(aws configure list-profiles 2>/dev/null) || {
208+
echo "Error: Failed to list AWS profiles"
209+
return 1
210+
}
211+
212+
if [[ -z "$profiles" ]]; then
213+
echo "No AWS profiles found"
214+
return 1
215+
fi
216+
217+
# Add clear option and format profiles with current indicator
218+
local profile_list=""
219+
local current_marker=""
220+
221+
# Add option to clear/unset profile
222+
profile_list="[Clear Profile]"
223+
224+
# Add each profile with current indicator
225+
while IFS= read -r prof; do
226+
if [[ "$AWS_PROFILE" == "$prof" ]]; then
227+
current_marker=""
228+
else
229+
current_marker=" "
230+
fi
231+
profile_list="$profile_list\n${current_marker}${prof}"
232+
done <<< "$profiles"
233+
234+
# Build prompt showing current profile
235+
local prompt="Select AWS profile"
236+
if [[ -n "$AWS_PROFILE" ]]; then
237+
prompt="Select AWS profile (current: $AWS_PROFILE)"
238+
fi
239+
240+
# Select profile with enhanced fzf UI
241+
local selection
242+
selection=$(echo -e "$profile_list" | fzf \
243+
--prompt "$prompt: " \
244+
--height 60% \
245+
--reverse \
246+
--border rounded \
247+
--border-label " AWS Profiles " \
248+
--header "● = current profile | ↑↓ navigate | enter select" \
249+
--color "border:#589df6,label:#89b4fa,header:#cba6f7" \
250+
--pointer "" \
251+
--marker "") || {
252+
echo "No profile selected"
202253
return 1
203254
}
204-
export AWS_PROFILE="$profile"
205-
echo "👷🏼 Working with AWS profile: $AWS_PROFILE"
255+
256+
# Handle clear profile option
257+
if [[ "$selection" == "[Clear Profile]" ]]; then
258+
if [[ -n "$AWS_PROFILE" ]]; then
259+
local old_profile="$AWS_PROFILE"
260+
unset AWS_PROFILE
261+
unset AWS_DEFAULT_REGION
262+
echo "Cleared AWS profile: $old_profile"
263+
else
264+
echo "No AWS profile currently set"
265+
fi
266+
return 0
267+
fi
268+
269+
# Remove the current indicator marker if present
270+
selection="${selection#● }"
271+
selection="${selection# }"
272+
273+
# Set the profile
274+
export AWS_PROFILE="$selection"
275+
276+
# Show profile details with formatting
277+
echo "╭─────────────────────────────────────────╮"
278+
echo "│ AWS Profile: $AWS_PROFILE"
279+
280+
# Get and display account details
281+
local account_id region user_arn
282+
account_id=$(aws sts get-caller-identity --query Account --output text 2>/dev/null)
283+
user_arn=$(aws sts get-caller-identity --query Arn --output text 2>/dev/null)
284+
region=$(aws configure get region 2>/dev/null)
285+
286+
if [[ -n "$account_id" ]]; then
287+
echo "│ Account ID: $account_id"
288+
fi
289+
if [[ -n "$region" ]]; then
290+
echo "│ Region: $region"
291+
fi
292+
if [[ -n "$user_arn" ]]; then
293+
# Extract just the user/role name from ARN
294+
local identity_name=$(echo "$user_arn" | awk -F'/' '{print $NF}')
295+
echo "│ Identity: $identity_name"
296+
fi
297+
echo "╰─────────────────────────────────────────╯"
206298
}
207299
alias aws-profile=awsprofile
208300

0 commit comments

Comments
 (0)