Created
January 12, 2020 04:53
-
-
Save cbadke/cd0711ea66a93185c02b865076c07476 to your computer and use it in GitHub Desktop.
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/zsh | |
function replace-default-with-profile { | |
#reads through an aws credentials file, | |
#takes the contents of the profile give in $1 | |
#and copies those credentials into the default profile | |
declare profileName=$1 | |
declare fileName=${2:-~/.aws/credentials} | |
declare profileBody="" | |
declare currentProfile="" | |
#read through file to extract profile data to copy | |
declare firstLine=0 | |
while read -r line; do | |
[[ -n $BASH_VERSION ]] && [[ $line =~ \[(.*)\] ]] && currentProfile="${BASH_REMATCH[1]}" && firstLine=1 && continue | |
[[ -n $ZSH_VERSION ]] && [[ $line =~ '\[(.*)\]' ]] && currentProfile="${match[1]}" && firstLine=1 && continue | |
[[ $firstLine == 1 ]] && [[ $currentProfile == $profileName ]] && profileBody="${line}" | |
[[ $firstLine == 0 ]] && [[ $currentProfile == $profileName ]] && profileBody="${profileBody}"$'\n'"${line}" | |
firstLine=0 | |
done < "$fileName" | |
#replace default profile with lines found in first pass | |
declare firstLine=1 | |
declare newfileContents='' | |
while read -r line; do | |
declare isHeader=0 | |
[[ -n $BASH_VERSION ]] && [[ $line =~ \[(.*)\] ]] && currentProfile="${BASH_REMATCH[1]}" && isHeader=1 | |
[[ -n $ZSH_VERSION ]] && [[ $line =~ '\[(.*)\]' ]] && currentProfile="${match[1]}" && isHeader=1 | |
[[ $isHeader == 1 ]] && [[ $firstLine == 1 ]] && newfileContents="${line}" | |
[[ $isHeader == 1 ]] && [[ $firstLine == 0 ]] && newfileContents="${newfileContents}"$'\n'"${line}" | |
[[ $isHeader == 1 ]] && [[ $currentProfile == 'default' ]] && newfileContents="${newfileContents}"$'\n'"${profileBody}"$'\n' | |
[[ $isHeader == 0 ]] && [[ $currentProfile != 'default' ]] && newfileContents="${newfileContents}"$'\n'"${line}" | |
firstLine=0 | |
done < "$fileName" | |
echo "$newfileContents" > "$fileName" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment