Created
March 30, 2024 22:41
-
-
Save johnhpatton/962dd3ca2391e2346490cf656e0b55b4 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
logbashrc() { | |
[[ $- =~ i ]] && echo "$1" | |
return 0 | |
} | |
pathadd() { | |
local option="" | |
local retval=0 | |
if [[ "${1:0:1}" = "-" ]]; then | |
option=$1 && shift | |
fi | |
# assign newpath, remove any trailing / character | |
local newpath="${1%/}" | |
local pathvar="$2" | |
# default to PATH | |
[ -z "$pathvar" ] && pathvar="PATH" | |
local pathval=${!pathvar} | |
if [ -d "$newpath" ] && [[ ":$pathval:" != *":$newpath:"* ]]; then | |
if [ "$option" == "-p" ]; then | |
# prepend | |
pathval="$newpath${pathval:+":$pathval"}" | |
else | |
pathval="${pathval:+"$pathval:"}$newpath" | |
fi | |
eval $pathvar=$pathval | |
export $pathvar | |
(( DEBUG )) && logbashrc "DEBUG: $newpath added to $pathvar." | |
elif [ ! -d "$newpath" ]; then | |
(( DEBUG )) && logbashrc "DEBUG: $newpath folder does not exist, not updating $pathvar." | |
retval=1 | |
else | |
(( DEBUG )) && logbashrc "DEBUG: $newpath already in $pathvar, not updating." | |
fi | |
return $(( retval )) | |
} | |
# Global function | |
function akamaicli { | |
if [[ `docker ps | grep akamai-cli$ | wc -l` -eq 1 ]]; then | |
docker exec -it akamai-cli akamai $@; | |
elif docker start akamai-cli > /dev/null 2>&1 && sleep 3 && docker exec -it akamai-cli akamai $@; then | |
return 0; | |
else | |
echo "Creating new docker container" | |
mkdir -p $HOME/.akamai-cli-docker | |
docker exec -it --mount source=vol-akamai-devops,target=$HOME/akamai --mount source=/cli,target=$HOME/.akamai-cli-docker --name akamai-cli akamai/cli > /dev/null 2>&1 && akamaicli $@; | |
fi; | |
} | |
configure_command_line_completion() { | |
[[ $- =~ i ]] || return | |
local -a enabled=() | |
# Enable bash programmable completion features in interactive shells | |
if [ -f /usr/share/bash-completion/bash_completion ]; then | |
. /usr/share/bash-completion/bash_completion | |
elif [ -f /etc/bash_completion ]; then | |
. /etc/bash_completion | |
elif [ -f /usr/local/etc/bash_completion ]; then | |
. /usr/local/etc/bash_completion | |
fi | |
# aws command line completion | |
type -P aws_completer &>/dev/null && enabled+=("aws") && complete -C $(which aws_completer) aws | |
# terraform command line completion | |
type -P terraform &>/dev/null && enabled+=("terraform") && complete -C $(which terraform) terraform | |
# vault command line completion | |
type -P vault &>/dev/null && enabled+=("vault") && complete -C $(which vault) vault | |
# consul command line completion | |
type -P consul &>/dev/null && enabled+=("consul") && complete -C $(which consul) consul | |
# k3d command line completion | |
type k3d &>/dev/null && enabled+=("k3d") && source <(k3d completion bash) | |
# helm command line completion | |
type helm &>/dev/null && enabled+=("helm") && source <(helm completion bash) | |
# kubectl command line completion | |
type kubectl &>/dev/null && enabled+=("kubectl") && source <(kubectl completion bash) | |
# eksctl command line completion | |
type eksctl &>/dev/null && enabled+=("eksctl") && source <(eksctl completion bash) | |
if (( ${#enabled[@]} )); then | |
logbashrc " INFO: Command Line Completion Enabled:" | |
printf "%s\n" "${enabled[@]/#/ - }" | |
fi | |
} | |
configure_tools() { | |
if [ -d "${HOME}/.nvm" ]; then | |
export NVM_DIR="$HOME/.nvm" | |
[ -s "${NVM_DIR}/nvm.sh" ] && . "${NVM_DIR}/nvm.sh" # This loads nvm | |
[ -s "${NVM_DIR}/bash_completion" ] && . "${NVM_DIR}/bash_completion" # This loads nvm bash_completion | |
logbashrc " INFO: Node $(nvm version) loaded from nvm" | |
fi | |
if [[ -s "$HOME/.rvm/scripts/rvm" ]]; then | |
source "$HOME/.rvm/scripts/rvm" | |
local v=$(ruby -v) | |
logbashrc " INFO: Ruby v${v:5:5} loaded via rvm" | |
fi | |
# added by travis gem | |
if [ -f "~/.travis/travis.sh" ] && [ -z "${TRAVIS_ENABLED}" ]; then | |
source ~/.travis/travis.sh | |
export TRAVIS_ENABLED=1 | |
logbashrc " INFO: TravisCI is enabled" | |
fi | |
} | |
setup_brew() { | |
declare -a pkgs=() | |
pkgs+=("bash") | |
pkgs+=("coreutils") | |
pkgs+=("binutils") | |
pkgs+=("diffutils") | |
pkgs+=("findutils") | |
pkgs+=("gnu-tar") | |
pkgs+=("gnu-sed") | |
pkgs+=("gnu-which") | |
pkgs+=("gnu-indent") | |
pkgs+=("gnutls") | |
pkgs+=("grep") | |
pkgs+=("gzip") | |
pkgs+=("screen") | |
pkgs+=("watch") | |
pkgs+=("wget") | |
pkgs+=("wdiff") | |
pkgs+=("curl") | |
pkgs+=("jq") | |
pkgs+=("git") | |
pkgs+=("readline") | |
pkgs+=("vim") | |
pkgs+=("openssh") | |
pkgs+=("unzip") | |
if ! type -P brew &>/dev/null; then | |
logbashrc "Installing homebrew..." | |
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" | |
fi | |
for pkg in "${pkgs[@]}"; do | |
! brew list "${pkg% *}" &>/dev/null && brew install ${pkg} | |
done | |
} | |
get_os_info() { | |
[[ $- =~ i ]] || return | |
if [ -f /etc/os-release ]; then | |
# freedesktop.org and systemd | |
. /etc/os-release | |
OS=$NAME | |
VER=$VERSION_ID | |
elif type lsb_release >/dev/null 2>&1; then | |
# linuxbase.org | |
OS=$(lsb_release -si) | |
VER=$(lsb_release -sr) | |
elif [ -f /etc/lsb-release ]; then | |
# For some versions of Debian/Ubuntu without lsb_release command | |
. /etc/lsb-release | |
OS=$DISTRIB_ID | |
VER=$DISTRIB_RELEASE | |
elif [ -f /etc/debian_version ]; then | |
# Older Debian/Ubuntu/etc. | |
OS=Debian | |
VER=$(cat /etc/debian_version) | |
elif [ -f /etc/SuSe-release ]; then | |
# Older SuSE/etc. | |
... | |
elif [ -f /etc/redhat-release ]; then | |
# Older Red Hat, CentOS, etc. | |
... | |
else | |
# Fall back to uname, e.g. "Linux <version>", also works for BSD, etc. | |
OS=$(uname -s) | |
if type -P sw_vers &>/dev/null; then | |
VER=$(sw_vers -productVersion) | |
else | |
VER=$(uname -r) | |
fi | |
fi | |
} | |
configure_colors() { | |
# no need for colors if non-interactive | |
[[ $- =~ i ]] || return | |
(( DEBUG )) && logbashrc "DEBUG: configuring colors..." | |
# colored highlighting is awesome, enable if available | |
if type dircolors &>/dev/null; then | |
if [ ! -f "~/.dir_colors" ]; then | |
(( DEBUG )) && logbashrc "DEBUG: generating dir colors" | |
if brew list gnu-sed &>/dev/null && [[ $PATH =~ gnu-sed\/libexec\/gnubin ]]; then | |
dircolors -p | sed '/# directory/c DIR 01;33 # directory for dark theme' > ~/.dir_colors | |
else | |
dircolors -p | sed -E 's@DIR.*directory@DIR 01;33 # directory for dark theme@' > ~/.dir_colors | |
fi | |
fi | |
(( DEBUG )) && logbashrc "DEBUG: loading colors" | |
eval "`dircolors -b ~/.dir_colors`" | |
elif [ "${OS}" == "Darwin" ]; then | |
export LSCOLORS='DxGxcxdxCxcgcdabagacad' | |
export CLICOLOR=1 | |
else | |
export LS_COLORS='rs=0:di=01;33:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:'; | |
fi | |
# Color for manpages in less makes manpages a little easier to read | |
export LESS_TERMCAP_mb=$'\E[01;31m' | |
export LESS_TERMCAP_md=$'\E[01;31m' | |
export LESS_TERMCAP_me=$'\E[0m' | |
export LESS_TERMCAP_se=$'\E[0m' | |
export LESS_TERMCAP_so=$'\E[01;44;33m' | |
export LESS_TERMCAP_ue=$'\E[0m' | |
export LESS_TERMCAP_us=$'\E[01;32m' | |
} | |
# command_prompt() | |
# | |
# Function that is executed each time a command is run to update the PS1 | |
# variable (ie: prompt). To configure, let's put some fonts in place. | |
# Download and install Gabriele Lana's Awesome Fonts: | |
# | |
# https://github.com/gabrielelana/awesome-terminal-fonts/archive/master.zip | |
# | |
# Unzip the archive and run the install.sh from the resulting | |
# awesome-terminal-fonts-master folder to install some fonts. Some .sh files | |
# will also be installed in ~/.fonts to provide constants for each font that | |
# can be used in scripts. | |
# | |
# If using git, install the git-prompt.sh script in your home folder and add | |
# to .bashrc: | |
# | |
# curl -L -o ~/.git-prompt.sh https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh | |
# chown +x ~/.git-prompt.sh | |
# echo 'source ~/.git-prompt.sh' >> ~/.bashrc | |
# | |
# You'll need to log out and back in to pull that in, or you'll need to source | |
# the ~/.git-prompt.sh in your current session to test it. | |
# | |
# Color scheme is based on a dark background. If using a light background, | |
# you'll want to experiment with the colors. | |
command_prompt() { | |
#========================================================= | |
# Capture EXIT_CODE MUST BE FIRST!!! | |
#========================================================= | |
local EXIT_CODE="$?" | |
[[ $- =~ i ]] || return | |
local debug=0 | |
local prompt_var="PS1" && PS1="" | |
(( EUID == 0 )) && prompt_var="SUDO_PS1" && SUDO_PS1="" | |
# exit code output variables | |
local exit_code="" | |
local exit_code_color="" | |
# CONSTANTS | |
# Colors for bash prompt | |
# foreground colors, use with echo -e | |
local RESET='\[\e[m\]' # No Color, reset | |
# normal bright high intensity bright + high intensity | |
local DKGREY='\[\e[0;30m\]' | |
local LTGREY='\[\e[1;30m\]' | |
local RED='\[\e[0;31m\]' BRED='\[\e[1;31m\]' IRED='\[\e[0;91m\]' BIRED='\[\e[1;91m\]' | |
local GREEN='\[\e[0;32m\]' BGREEN='\[\e[1;32m\]' IGREEN='\[\e[0;92m\]' BIGREEN='\[\e[1;92m\]' | |
local YELLOW='\[\e[0;33m\]' BYELLOW='\[\e[1;33m\]' IYELLOW='\[\e[0;93m\]' BIYELLOW='\[\e[1;93m\]' | |
local BLUE='\[\e[0;34m\]' BBLUE='\[\e[1;34m\]' IBLUE='\[\e[0;94m\]' BIBLUE='\[\e[1;94m\]' | |
local PURPLE='\[\e[0;35m\]' BPURPLE='\[\e[1;35m\]' IPURPLE='\[\e[0;95m\]' BIPURPLE='\[\e[1;95m\]' | |
local CYAN='\[\e[0;36m\]' BCYAN='\[\e[1;36m\]' ICYAN='\[\e[0;96m\]' BICYAN='\[\e[1;96m\]' | |
local GREY='\[\e[0;37m\]' BGREY='\[\e[1;37m\]' | |
local WHITE='\[\e[0;97m\]' BWHITE='\[\e[1;97m\]' | |
local sym_radioactive=$(echo -e '\u2622') | |
local sym_warning=$(echo -e '\u26A0') | |
local sym_check_mark=$(echo -e '\u2714') | |
local sym_cancel=$(echo -e '\u2718') | |
local sym_sparkle="$(echo -e '\u2728')" | |
local sym_arrow="$(echo -e '\u2937')" | |
local sym_line="$(echo -e '\u2574')" | |
local sym_left_arrow_up="$(echo -e '\u2b11')" | |
local sym_background_process="$(echo -e '\u2749')" | |
local sym_colon="$(echo -e '\u003A')" | |
local user="${CYAN}\u${RESET}" | |
(( EUID == 0 )) && user="${RED}\u${RESET}" | |
local host="${ICYAN}\h${RESET}" | |
local workdir="${YELLOW}\W${RESET}" | |
# is user in an SSH session? | |
local bracket_color="" # default | |
if [[ $SSH_CLIENT ]] || [[ $SSH2_CLIENT ]]; then | |
bracket_color="${BPURPLE}" | |
fi | |
local info_bracket_color="${GREY}" | |
local git_branch="" | |
local git_symbols="" | |
# check if user has ~/.git-prompt.sh loaded and run | |
# __git_ps1 if possible | |
local git_ps1="" | |
if declare -F __git_ps1 &>/dev/null; then | |
git_ps1=$(__git_ps1) | |
git_ps1="${git_ps1//[(]}" | |
git_ps1="${git_ps1//[]]}" | |
git_ps1="${git_ps1//[)]}" | |
if [ -n "${git_ps1}" ]; then | |
git_ps1="${git_ps1##*( )}" | |
# colour branch name depending on state | |
# make the symbols easier to see. | |
local git_branch_color="${GREY}" | |
# staged files, committed but not pushed | |
if [[ "${git_ps1}" =~ "+" ]]; then | |
git_branch_color="${CYAN}" | |
git_ps1="${git_ps1//[+]}" | |
git_symbols+=" ${CYAN}staged${RESET}" | |
fi | |
# if there is something stashed | |
if [[ "${git_ps1}" =~ "$" ]]; then | |
git_branch_color="${GREEN}" | |
git_ps1="${git_ps1//[$]}" | |
git_symbols+=" ${GREEN}stashed${RESET}" | |
fi | |
# unstaged/uncommitted files (dirty) | |
if [[ "${git_ps1}" =~ "*" ]]; then | |
git_branch_color="${YELLOW}" | |
git_ps1="${git_ps1//[*]}" | |
git_symbols+=" ${YELLOW}unstaged${RESET}" | |
fi | |
# branch is diverged | |
if [[ "${git_ps1}" =~ "<>" ]]; then | |
git_branch_color="${RED}" | |
git_ps1="${git_ps1//[<>]}" | |
git_symbols+=" ${RED}diverged${RESET}" | |
# branch is ahead | |
elif [[ "${git_ps1}" =~ ">" ]]; then | |
git_branch_color="${YELLOW}" | |
git_ps1="${git_ps1//[>]}" | |
git_symbols+=" ${YELLOW}ahead${RESET}" | |
# branch is behind | |
elif [[ "${git_ps1}" =~ "<" ]]; then | |
git_branch_color="${YELLOW}" | |
git_ps1="${git_ps1//[<]}" | |
git_symbols+=" ${YELLOW}behind${RESET}" | |
fi | |
# untracked files | |
if [[ "${git_ps1}" =~ "%" ]]; then | |
git_branch_color="${RED}" | |
git_ps1="${git_ps1//[%]}" | |
git_symbols+=" ${RED}untracked${RESET}" | |
fi | |
# no changes | |
if [ -z "${git_symbols}" ]; then | |
git_symbols=" up-to-date" | |
fi | |
git_ps1="${git_ps1//=} ${info_bracket_color}=>${RESET}" | |
git_branch="${info_bracket_color}[[${RESET}${git_branch_color}${git_ps1}${RESET}" | |
git_branch+="${git_symbols}${RESET}${info_bracket_color}]]${RESET}" | |
fi | |
fi | |
if [ -d ".terraform" ]; then | |
local terraform_workspace="default" | |
local terraform_workspace_color="${GREY}" | |
if [ -f ".terraform/environment" ]; then | |
[ -n "${TF_WORKSPACE}" ] && terraform_workspace=${TF_WORKSPACE} || terraform_workspace=$(cat .terraform/environment) | |
fi | |
case ${terraform_workspace} in | |
*dev*|*qa*|*test*) terraform_workspace_color=${CYAN};; | |
*uat*|*stag*) terraform_workspace_color=${YELLOW};; | |
*prod*) terraform_workspace_color=${YELLOW};; | |
*) terraform_workspace_color=${GREEN};; | |
esac | |
terraform_workspace="${info_bracket_color}tf:${RESET}${terraform_workspace_color}${terraform_workspace}${RESET}" | |
fi | |
local commercetools_project="${GREY}" | |
local commercetools_project_color="${GREY}" | |
if [ -n "${CTP_PROJECT_KEY}" ]; then | |
case ${CTP_PROJECT_KEY} in | |
*dev*|*qa*|*test*) commercetools_project_color=${CYAN};; | |
*uat*|*stg*|*stag*) commercetools_project_color=${YELLOW};; | |
*prd*|*prod*) commercetools_project_color=${YELLOW};; | |
*) commercetools_project_color=${GREEN};; | |
esac | |
commercetools_project="${info_bracket_color}ct:${RESET}${commercetools_project_color}${CTP_PROJECT_KEY}${RESET}" | |
fi | |
local kube_context="" | |
if type kubectl &>/dev/null; then | |
if [ -f "${HOME}/.kube/config" ] || [ -n "${KUBECONFIG}" ]; then | |
kube_context=$(kubectl config current-context) | |
local kube_context_color="${GREY}" | |
case ${kube_context} in | |
*dev*|*qa*|*test*|*nonprod*) kube_context_color=${CYAN};; | |
*uat*|*stag*) kube_context_color=${YELLOW};; | |
*prod*) kube_context_color=${YELLOW};; | |
esac | |
kube_context="${info_bracket_color}{k8s:${RESET}${kube_context_color}${kube_context}${RESET}${info_bracket_color}}${RESET}" | |
fi | |
fi | |
case ${EXIT_CODE} in | |
0) exit_code="${sym_check_mark}" | |
exit_code_color="${BGREEN}" | |
;; | |
127) exit_code="${sym_warning}" # command not found | |
exit_code_color="${BIRED}" | |
;; | |
130) exit_code="${sym_cancel}" # control-c | |
exit_code_color="${BYELLOW}" | |
;; | |
148) exit_code="${sym_sparkle}" # backgrounded command | |
exit_code_color="${BCYAN}" | |
;; | |
*) exit_code="${EXIT_CODE}" # other error code | |
exit_code_color="${RED}" | |
;; | |
esac | |
# backgrounded and paused jobs in the current session. | |
local jobs_details="" | |
local bg_jobs=$(jobs -r | wc -l ) | |
(( bg_jobs > 0 )) && jobs_details="${YELLOW}${sym_background_process}${RESET}${sym_colon}${WHITE}${bg_jobs}${RESET} " | |
(( bg_jobs > 3 )) && jobs_details="${RED}${sym_background_process}${RESET}${sym_colon}${WHITE}${bg_jobs}${RESET} " | |
local st_jobs=$(jobs -s | wc -l ) | |
(( st_jobs > 0 )) && (( st_jobs <= 3 )) && jobs_details+="${LTGREY}${sym_background_process}${RESET}${sym_colon}${WHITE}${st_jobs}${RESET}" | |
(( st_jobs > 3 )) && jobs_details+="${LTGREY}${sym_background_process}${RESET}${sym_colon}${WHITE}${st_jobs}${RESET}" | |
# assemble the prompt | |
local prompt="" | |
[ -n "${kube_context}" ] && PS1+="${kube_context}" | |
[ -n "${git_branch}" ] && prompt+="${git_branch}" && (( debug )) && echo "git_branch set" | |
[ -n "${terraform_workspace}" ] && { [ -n "${prompt}" ] && prompt+=" "; prompt+="${terraform_workspace}"; (( debug )) && echo "terraform_workspace set"; } | |
[ -n "${commercetools_project}" ] && { [ -n "${prompt}" ] && prompt+=" "; prompt+="${commercetools_project}"; (( debug )) && echo "commercetools_project set"; } | |
[ -n "${jobs_details}" ] && { [ -n "${prompt}" ] && prompt+=" "; prompt+="${jobs_details}"; (( debug )) && echo "job_details set"; } | |
[ -n "${prompt}" ] && prompt+="\n" | |
prompt+="${exit_code_color}${sym_arrow} ${exit_code}${RESET}" | |
prompt+="${bracket_color}[${RESET}${user}@${host}${bracket_color}][${RESET}${workdir}${bracket_color}]${RESET}$ " | |
#(( EUID == 0 )) && SUDO_PS1=$prompt || PS1=$prompt | |
PS1=$prompt | |
} | |
configure_ps1() { | |
[[ $- =~ i ]] || return | |
# configure PROMPT_COMMAND which is executed each time before PS1 | |
export PROMPT_COMMAND='command_prompt; history -a; history -c; history -r;' | |
} | |
configure_aliases() { | |
# no need for aliases if non-interactive | |
[[ $- =~ i ]] || return | |
export CMD_OPTS="--color=auto" | |
export LS_OPTS="-F ${CMD_OPTS}" | |
# set aliases if unset | |
## Colorize the ls output ## | |
alias ls &>/dev/null || alias ls="$(which ls) ${LS_OPTS}" | |
## Use a long listing format ## | |
alias ll &>/dev/null || alias ll="$(which ls) -la ${LS_OPTS}" | |
## Show hidden files ## | |
alias l. &>/dev/null || alias l.="$(which ls) -d .* ${LS_OPTS}" | |
alias dir &>/dev/null || alias dir="$(which ls) --format=vertical ${LS_OPTS}" | |
alias vdir &>/dev/null || alias vdir="$(which ls) --format=long ${LS_OPTS}" | |
alias grep &>/dev/null || alias grep="$(which grep) ${CMD_OPTS}" | |
alias fgrep &>/dev/null || alias fgrep="$(which fgrep) ${CMD_OPTS}" | |
alias egrep &>/dev/null || alias egrep="$(which egrep) ${CMD_OPTS}" | |
# Terraform | |
alias tf &>/dev/null || alias tf="terraform" | |
alias tf12 &>/dev/null || alias tf="terraform12" | |
# Show current network connections to servers | |
alias ipview="netstat -anpl | grep :80 | awk {'print \$5'} | cut -d\":\" -f1 | sort | uniq -c | sort -n | sed -e 's/^ *//' -e 's/ *\$//'" | |
# Show local open listeners | |
alias openports='netstat -nape --inet' | |
# Weather | |
alias weather &>/dev/null || alias weather="curl http://wttr.in/" | |
# Public IP | |
alias whatismyip="dig +short myip.opendns.com @resolver1.opendns.com" | |
if [ "${OS}" == "Linux" ]; then | |
alias ubupdate="sudo apt update && sudo apt dist-upgrade -y --auto-remove" | |
fi | |
} | |
configure_git() { | |
# no need for bash completion or branch in prompt if non-interactive | |
[[ $- =~ i ]] || return | |
local git_prompt_sh="" | |
# Git | |
if type git &>/dev/null; then | |
if [ ! "$(git config --global credential.helper)" == "cache" ]; then | |
git config --global credential.helper 'cache --timeout=3600' ${git_credential_type} | |
alias gitdisablecache="git config --global --unset credential.helper" | |
fi | |
if [ -f /usr/lib/git-core/git-sh-prompt ]; then | |
git_prompt_sh="/usr/lib/git-core/git-sh-prompt" | |
else | |
git_prompt_sh="${HOME}/.git-prompt.sh" | |
if [ ! -f "${git_prompt_sh}" ]; then | |
logbashrc " INFO: git-prompt.sh not found, installing... this is only needed once..." | |
curl -sL -o "${git_prompt_sh}" "https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh" | |
chmod +x "${git_prompt_sh}" | |
fi | |
fi | |
# if .git-prompt.sh exists, set options and execute it | |
if [ -n "${git_prompt_sh}" ]; then | |
GIT_PS1_SHOWDIRTYSTATE=true # this can get sluggish for large repositories | |
GIT_PS1_SHOWUNTRACKEDFILES=true # this runs a git ls-files command and can be rather sluggish | |
GIT_PS1_SHOWSTASHSTATE=true | |
GIT_PS1_SHOWUPSTREAM="auto" | |
GIT_PS1_HIDE_IF_PWD_IGNORED=true | |
GIT_PS1_SHOWCOLORHINTS=true | |
GIT_PS1_STATESEPARATOR="" | |
. "${git_prompt_sh}" | |
fi | |
fi | |
} | |
configure_path() { | |
local work_dir | |
local -a bin_vers=() | |
# Configure common paths | |
if [ "${OS}" == "Darwin" ]; then | |
local homebrew_prefix="/usr/local" | |
[ "${MACHINE_ARCH}" == "arm64" ] && homebrew_prefix="/opt/homebrew" | |
if [ -d "${homebrew_prefix}/bin" ]; then | |
export HOMEBREW_PREFIX="${homebrew_prefix}" | |
export HOMEBREW_REPOSITORY="${HOMEBREW_PREFIX}" | |
export HOMEBREW_CELLAR="${HOMEBREW_PREFIX}/Cellar" | |
pathadd -p "${HOMEBREW_PREFIX}/bin" | |
pathadd "${HOMEBREW_PREFIX}/share/man" "MANPATH" | |
pathadd "${HOMEBREW_PREFIX}/share/info" "INFOPATH" | |
# Core Utils, if installed | |
work_dir="${HOMEBREW_PREFIX}/opt/coreutils/libexec/gnubin" | |
pathadd -p "${work_dir}" && pathadd -p "${work_dir%/*}/gnuman" "MANPATH" | |
# GNU Diff Utils, if installed | |
work_dir="${HOMEBREW_PREFIX}/opt/diffutils/bin" | |
pathadd -p "${work_dir}" && pathadd -p "${work_dir%/*}/share/man" "MANPATH" | |
# GNU Tar, if installed | |
work_dir="${HOMEBREW_PREFIX}/opt/gnu-tar/libexec/gnubin" | |
pathadd -p "${work_dir}" && pathadd -p "${work_dir%/*}/gnuman" "MANPATH" | |
# Ruby installed by brew | |
work_dir="${HOMEBREW_PREFIX}/opt/ruby/bin" | |
pathadd -p "${work_dir}" && pathadd -p "${work_dir%/*}/share/man" "MANPATH" | |
bin_vers+=("$(ruby --version | awk '{printf "%s %s\n", $1, $2}')") | |
fi | |
# Maven, if installed | |
export MAVEN_HOME=/usr/local/apache-maven | |
pathadd "${MAVEN_HOME}/bin" || unset MAVEN_HOME | |
# Android Debug Bridge | |
work_dir=$(readlink -f ~/Applications/platform-tools) | |
pathadd -p "${work_dir}" | |
git_credential_type="osxkeychain" | |
else | |
# Android Debug Bridge | |
work_dir=/opt/local/platform-tools | |
pathadd -p "${work_dir}" | |
export LOCAL_BIN=~/.local/bin | |
pathadd "${LOCAL_BIN}" || unset LOCAL_BIN | |
fi | |
# Python Config | |
if type -P python3 &>/dev/null; then | |
if [ ! "$(readlink -f $(type -P python))" == "$(readlink -f $(type -P python3))" ]; then | |
alias python="python3" | |
fi | |
fi | |
type -P python &>/dev/null && bin_vers+=("$(python --version)") | |
# MySQL, if installed | |
work_dir=/usr/local/mysql/bin | |
pathadd "${work_dir}" | |
export GOROOT=/usr/local/go | |
pathadd "${GOROOT}/bin" && bin_vers+=("$(go version | awk '{print $3}' | sed 's/go\(.*\)/go \1/g')") || unset GOROOT | |
pathadd "${HOME}/bin" | |
if (( ${#bin_vers[@]} > 0 )); then | |
logbashrc " INFO: Installed tools:" | |
for v in "${bin_vers[@]}"; do | |
logbashrc " - $v" | |
done | |
fi | |
} | |
configure_vpn() { | |
[[ $- =~ i ]] || return | |
# Open Connect Client :: AnyConnect | |
if [ "${MACHINE_TYPE}" == "vm" ]; then | |
alias vpnconnect="sudo /usr/local/sbin/openconnect.ctl -a start vpn" | |
alias vpndisconnect="sudo /usr/local/sbin/openconnect.ctl -a stop vpn" | |
logbashrc " INFO: vpnconnect configured." | |
fi | |
} | |
MACHINE_ARCH=$(uname -m) | |
init_bash() { | |
get_os_info | |
# Disable the bell in interactive shell | |
if [[ $- =~ i ]]; then | |
MACHINE_TYPE="host" | |
if [ "${OS}" == "Darwin" ]; then | |
# setup_brew | |
# type -P brew && setup_brew | |
ioreg -l | grep -q -E "Manufacturer" | grep -E "VirtualBox|Oracle|VMware|Parallels" && MACHINE_TYPE="vm" | |
elif type -P facter &>/dev/null && [ ! "$(facter virtual)" == "physical" ] || grep -q '^flags.*\ hypervisor\ ' /proc/cpuinfo; then | |
MACHINE_TYPE="vm" | |
fi | |
logbashrc "Initializing ${MACHINE_TYPE}" | |
bind "set bell-style visible" | |
[[ $SSH_CLIENT ]] || [[ $SSH2_CLIENT ]] && REMOTE=1 || REMOTE=0 | |
[ -n "${LANG}" ] || export LANG="en_US.UTF-8" | |
# Get DPI of screen | |
if type -P xrandr &>/dev/null && [ "${MACHINE_TYPE}" == "host" ] && (( REMOTE == 0 )); then | |
read -r X Y < <(xrandr | grep -w connected | awk '{print $(NF-2)" "$(NF)}') | |
X="${X//[[:alpha:]]}" | |
Y="${Y//[[:alpha:]]}" | |
fi | |
read -r ROWS COLUMNS < <(stty size) | |
[ -z "$COLUMNS" ] && COLUMNS=140 | |
# Make bash check it's window size after a process completes | |
shopt -s checkwinsize | |
# Make bash fix common folder name spelling mistakes (ect vs. etc) | |
shopt -s cdspell | |
## History settings | |
# Expand the history size | |
export HISTFILESIZE=20000 | |
export HISTSIZE=5000 | |
# Don't put duplicate lines in the history and do not add lines that start with a space | |
export HISTCONTROL=erasedups:ignoredups:ignorespace | |
# Set the default editor | |
type vim &>/dev/null && export EDITOR=vim || export EDITOR=vi | |
export VISUAL=$EDITOR | |
fi | |
configure_path | |
configure_colors | |
configure_command_line_completion | |
configure_tools | |
configure_aliases | |
configure_git | |
configure_ps1 | |
configure_vpn | |
# remove bashrc functions from env to avoid clutter | |
unset -f pathadd | |
unset -f get_os_info | |
unset -f configure_vpn | |
unset -f configure_colors | |
unset -f configure_command_line_completion | |
unset -f configure_tools | |
unset -f configure_aliases | |
unset -f configure_git | |
unset -f configure_ps1 | |
unset -f configure_path | |
unset -f logbashrc | |
[[ $- =~ i ]] || unset -f command_prompt | |
unset MACHINE_TYPE | |
unset MACHINE_ARCH | |
} | |
init_bash | |
unset -f init_bash |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment