Created
November 8, 2024 18:06
-
-
Save jdhuntington/f649dc8182d028a66f95621cc185c917 to your computer and use it in GitHub Desktop.
Select aws credentials
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Colors for output | |
GREEN='\033[0;32m' | |
BLUE='\033[0;34m' | |
NC='\033[0m' # No Color | |
AWS_DIR="$HOME/.aws" | |
CREDENTIALS_LINK="$AWS_DIR/credentials" | |
# Function to show current credentials | |
show_current() { | |
if [ -L "$CREDENTIALS_LINK" ]; then | |
current=$(readlink "$CREDENTIALS_LINK") | |
# Extract just the profile name from the full path | |
profile=$(echo "$current" | sed "s|$AWS_DIR/||" | cut -d'/' -f1) | |
echo -e "${BLUE}Current credentials profile:${NC} ${GREEN}$profile${NC}" | |
else | |
echo "No credentials symlink currently set" | |
fi | |
} | |
# Function to list available credential files using fzf | |
select_credentials() { | |
# Find directories in ~/.aws that contain a credentials file | |
find "$AWS_DIR" -name "credentials" -not -path "$AWS_DIR/credentials" | \ | |
while read -r file; do | |
# Get just the immediate parent directory name | |
echo "$file" | sed "s|$AWS_DIR/||" | cut -d'/' -f1 | |
done | sort -u | \ | |
fzf --prompt="Select AWS credentials profile: " \ | |
--header="Use arrow keys to navigate, Enter to select" \ | |
--height=40% | |
} | |
# Show current credentials first | |
show_current | |
# Get user selection | |
selected=$(select_credentials) | |
if [ -n "$selected" ]; then | |
# Full path to the selected credentials file | |
selected_creds="$AWS_DIR/$selected/credentials" | |
# Remove existing symlink if it exists | |
if [ -L "$CREDENTIALS_LINK" ]; then | |
rm "$CREDENTIALS_LINK" | |
elif [ -f "$CREDENTIALS_LINK" ]; then | |
echo "Warning: $CREDENTIALS_LINK exists but is not a symlink" | |
echo "Please remove or rename it first" | |
exit 1 | |
fi | |
# Create new symlink | |
ln -s "$selected_creds" "$CREDENTIALS_LINK" | |
echo -e "\n${GREEN}Successfully switched to: $selected${NC}" | |
# Show new current credentials | |
echo -e "\nVerifying new credentials setup:" | |
show_current | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment