Skip to content

Instantly share code, notes, and snippets.

@carlosonunez
Created February 8, 2016 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlosonunez/668a9cd5e689206eb7f6 to your computer and use it in GitHub Desktop.
Save carlosonunez/668a9cd5e689206eb7f6 to your computer and use it in GitHub Desktop.
export TERM="xterm-color"
export CLICOLOR=1
export LSCOLORS=GxFxCxDxBxegedabagaced
# ==================================================================
# Most of this was taken from the example .bash_profile on tldp.org.
# ==================================================================
# =================
# COLORS
# =================
# Normal Colors
Black='\e[0;30m' # Black
Red='\e[0;31m' # Red
Green='\e[0;32m' # Green
Yellow='\e[0;33m' # Yellow
Blue='\e[0;34m' # Blue
Purple='\e[0;35m' # Purple
Cyan='\e[0;36m' # Cyan
White='\e[0;37m' # White
# Bold
BBlack='\e[1;30m' # Black
BRed='\e[1;31m' # Red
BGreen='\e[1;32m' # Green
BYellow='\e[1;33m' # Yellow
BBlue='\e[1;34m' # Blue
BPurple='\e[1;35m' # Purple
BCyan='\e[1;36m' # Cyan
BWhite='\e[1;37m' # White
# Background
On_Black='\e[40m' # Black
On_Red='\e[41m' # Red
On_Green='\e[42m' # Green
On_Yellow='\e[43m' # Yellow
On_Blue='\e[44m' # Blue
On_Purple='\e[45m' # Purple
On_Cyan='\e[46m' # Cyan
On_White='\e[47m' # White
NC="\e[m" # Color Reset
ALERT=${BWhite}${On_Red} # Bold White on red background
# ===================
# Configures Ansible locally
# ===================
export PRJ_DIR=~/source
export ANSIBLE_DIR=$PRJ_DIR/ansible
# Ansible setup
echo $PATH | grep ansible || {
echo "Looks like Ansible isn't configured on this machine. Let's fix that."
which ansible || . $ANSIBLE_DIR/hacking/env-setup 1>/dev/null
}
# =================
# FUNCTIONS
# =================
kill_all_matching_pids() {
pgrep $@ | while read pid; do sudo kill -9 $pid; done
}
generate_password() {
if [[ $# -ne 2 ]]; then
echo "generate_password [email_address] [domain_for_account]"
return
fi
email_address=$1
domain=$2
password=`echo "$email_address $domain" | md5 | shasum | cut -f1 -d '-' | head -c 12`
echo $password | pbcopy
echo "Password: $password. It has been copied into your clipboard."
}
extract_from_lpass_note() {
print_usage() {
printf "usage: extract_from_lpass_note [note_name] [search_term]\n"
}
if [[ $# -ne 2 ]]; then
printf "${BRed}Error: Two arguments required, note name and field name.\n${NC}"
print_usage
return
fi
which lpass 2>/dev/null 1>/dev/null || {
printf "${BRed}Error: LastPass isn't installed. Run this to do so: 'brew install lastpass-cli'.${NC}\n"
print_usage
return 1
}
[[ -f ~/.lpass/session_privatekey ]] || {
printf "${BRed}Error: You're not logged into LastPass.${NC}\n"
return 1
}
note_name=$1
predicate=$2
lpass show "$note_name" | \
egrep "$predicate\:" | \
sed "s/^$predicate\: \(.*\)$/\1/"
}
ec2() {
if [[ $# -eq 0 ]]; then
printf "${BRed}Error: Commands required.${NC}\n"
return
fi
export JAVA_HOME=$(/usr/libexec/java_home 2>/dev/null)
[[ -z $JAVA_HOME ]] && {
printf "${BRed}Error: Java JDK is not installed. Click 'More info' to install.\n${NC}"
/usr/libexec/java_home --request
return 1
}
aws_command_with_args=`echo $@ | sed 's/^ec2\-\(.*\)$/\1/'`
aws_access_key_lpass_predicate="Username"
aws_secret_key_lpass_predicate="Password"
aws_credentials_lpass_note_name="AWS: Access Key"
aws_cli_bin_path='/usr/local/ec2'
if [[ ! -d $aws_cli_bin_path ]]; then
printf "${BRed}Error: AWS CLI not found. Install it, then try again.${NC}\n"
return 1
fi
aws_cli_bin_path=`find $aws_cli_bin_path -type d -maxdepth 1 | sort | tail -n 1`
[[ -z $EC2_HOME ]] && export EC2_HOME="$aws_cli_bin_path"
aws_cli_bin_path="$aws_cli_bin_path/bin"
if [[ `echo $@ | egrep -q '^ls' ; echo $?` == "0" ]]; then
maybe_search_terms=`echo $@ | sed 's/^ls \(.*\)/\1/' | tr -d ' '`
find $aws_cli_bin_path -type f -maxdepth 1 | egrep -v "\.cmd$" | \
grep --color "/bin/ec2-" | \
grep --color "$maybe_search_terms"
return
fi
aws_access_key=`extract_from_lpass_note "$aws_credentials_lpass_note_name" $aws_access_key_lpass_predicate`
aws_secret_key=`extract_from_lpass_note "$aws_credentials_lpass_note_name" $aws_secret_key_lpass_predicate`
if [[ -z $aws_access_key || -z $aws_secret_key ]]; then
printf "${BRed}Error: LastPass couldn't find the AWS access or secret key.${NC}\n"
return 1
fi
export AWS_ACCESS_KEY=`echo $aws_access_key`
export AWS_SECRET_KEY=`echo $aws_secret_key`
printf "Running: ${BYellow}$aws_command_with_args${NC}\n"
eval $aws_cli_bin_path/ec2-$aws_command_with_args
unset AWS_ACCESS_KEY
unset AWS_SECRET_KEY
printf "Finished running: ${BYellow}$aws_command_with_args${NC}\n"
return
}
ec2_help() {
aws ls
}
# =================
# ALIASES
# =================
alias killmatch='kill_all_matching_pids'
alias clip='pbcopy'
# ===========================================
# Display last error code, when applicable
# ===========================================
# ==========================================
# Check if I'm root or someone else.
# ==========================================
my_username="carlosnunez"
fmtd_username=$my_username
if [ $USER != $my_username ]; then
fmtd_username="\[$On_Purple\]\[$BWhite\]$my_username\[$NC\]"
elif [ $EUID -eq 0 ]; then
fmtd_username="\[$ALERT\]$my_username\[$NC\]"
else
fmtd_username="\[$On_Green\]\[$BBlack\]$my_username\[$NC\]"
fi
# ===================
# Machine name
# ===================
hostname_fmtd="\[$Yellow\]$HOSTNAME\[$NC\]"
if [ ! -z $SSH_CLIENT ]; then
hostname_fmtd="\[$On_Yellow\]\[$BBlack\]$HOSTNAME\[$NC\]"
fi
# Load RVM
# =========
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" > /dev/null # Load RVM into a shell session *as a function*
# Update brew
# ============
brew update
# ===================
# Finally, set PS1.
# ===================
PROMPT_COMMAND='e=$?; if [ $e -ne 0 ]; then printf "${On_Red}${BWhite}ERROR CODE $e${NC}\n"; fi;'
string="[\[$BCyan\]$(date "+%Y-%m-%d %H:%M:%S")\[$NC\] $fmtd_username@$hostname_fmtd]:\[$BCyan\]\W \[$NC\]\[$Yellow\]\$\[$NC\] "
PS1=$string
# Then start tmux and bind to the first session available.
# ===============================================
which tmux > /dev/null || brew install tmux
tmux ls || tmux new-session && tmux a -t 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment