Skip to content
.bashrc 13.8 KiB
Newer Older
# shellcheck disable=SC2148 shell=bash
nimrod's avatar
nimrod committed
# If not running interactively, don't do anything
[ -z "$PS1" ] && return

nimrod's avatar
nimrod committed
export PS1='\u@\h:\w\$ '
export LANG=en_US.UTF8
export HISTFILE="$HOME/Documents/.history"
export HISTCONTROL=ignoreboth:erasedups
export HISTSIZE=100000
export HISTFILESIZE=100000
export EDITOR=vim
export GOPATH="$HOME/.local/golang"
nimrod's avatar
nimrod committed
export PATH="$GOPATH/bin:/usr/lib/go/bin/:$PATH"
export PATH="$HOME/Repositories/Shore/ssh-ca:$PATH"
export PATH="$HOME/Repositories/Shore/ssl-ca:$PATH"
nimrod's avatar
nimrod committed
export PATH="$HOME/.cargo/bin:$PATH"
export PATH="$HOME/.cabal/bin:$PATH"
nimrod's avatar
nimrod committed
export PATH="$HOME/.local/bin:$PATH"
nimrod's avatar
nimrod committed
export PATH="$HOME/Documents/bin:$PATH"
export PYTHONSTARTUP=~/.config/pythonrc.py
nimrod's avatar
nimrod committed
PIPENV_DEFAULT_PYTHON_VERSION="$(python3 --version | grep -i '3\.[0-9]*')" > /dev/null 2>&1
nimrod's avatar
nimrod committed
export PIPENV_DEFAULT_PYTHON_VERSION
nimrod's avatar
nimrod committed
export AWS_DEFAULT_PROFILE='shore'
export ANSIBLE_VERBOSITY=2
export ANSIBLE_COMMAND_WARNINGS=True
export ANSIBLE_DEPRECATION_WARNINGS=True
nimrod's avatar
nimrod committed
export ANSIBLE_SYSTEM_WARNINGS=True
export ANSIBLE_RETRY_FILES_SAVE_PATH=/tmp/
nimrod's avatar
nimrod committed
export ANSIBLE_PIPELINING=True
export ANSIBLE_GATHERING=smart
export ANSIBLE_CACHE_PLUGIN=jsonfile
export ANSIBLE_CACHE_PLUGIN_CONNECTION="$HOME/.ansible/facts"
nimrod's avatar
nimrod committed
export ANSIBLE_CALLBACK_WHITELIST="profile_tasks, timer"
export ANSIBLE_SSH_CONTROL_PATH="/tmp/ssh-%%h"
export ANSIBLE_INVENTORY_ANY_UNPARSED_IS_FAILED=True
nimrod's avatar
nimrod committed
export ANSIBLE_PYTHON_INTERPRETER=auto
nimrod's avatar
nimrod committed
export ANSIBLE_FORKS=5
nimrod's avatar
nimrod committed
export LYNX_SAVE_SPACE="$HOME/Downloads"
export LYNX_TEMP_SPACE="$HOME/.cache/lynx"
nimrod's avatar
nimrod committed
export VAGRANT_DEFAULT_PROVIDER="virtualbox"
export PIPENV_MAX_DEPTH=5
nimrod's avatar
nimrod committed
# Blinking (red).
nimrod's avatar
nimrod committed
export LESS_TERMCAP_mb=$'\E[01;31m'
nimrod's avatar
nimrod committed
# Double bright (purple).
nimrod's avatar
nimrod committed
export LESS_TERMCAP_md=$'\E[01;35m'
export LESS_TERMCAP_me=$'\E[0m'
nimrod's avatar
nimrod committed
# Standout (grey).
nimrod's avatar
nimrod committed
export LESS_TERMCAP_so=$'\E[01;33m'
export LESS_TERMCAP_se=$'\E[0m'
nimrod's avatar
nimrod committed
# Underline (dark grey).
nimrod's avatar
nimrod committed
export LESS_TERMCAP_us=$'\E[01;32m'
export LESS_TERMCAP_ue=$'\E[0m'
nimrod's avatar
nimrod committed
export HELM_HOME="$HOME/.helm"
export DOCKER_BUILDKIT=1
export PGSSLROOTCERT=/etc/ssl/certs/ca-certificates.crt
nimrod's avatar
nimrod committed
export CLOUDSDK_ACTIVE_CONFIG_NAME='shore'
export GNUPGHOME="$HOME/Documents/.gnupg"
export REDISCLI_HISTFILE="$HOME/Documents/.rediscli_history"
alias ll='ls -lha'
nimrod's avatar
nimrod committed
alias la='ls -AF'
alias l='ls -F'
alias lsblk='lsblk --output=NAME,RM,RO,SIZE,TYPE,FSTYPE,LABEL,UUID,MODEL,TRAN,MOUNTPOINT'
alias gcc='gcc --std=c99 -Wall -Wextra -Werror -pedantic'
alias dpkglog="grep -v 'status\\|trigproc\\|configure' /var/log/dpkg.log"
alias deborphan='deborphan -a --no-show-section --ignore-suggests'
nimrod's avatar
nimrod committed
alias aptitude='aptitude --display-format %p --quiet'
alias obsolete='aptitude search ?obsolete'
alias missing-recommends="aptitude search '~RBrecommends:~i'"
nimrod's avatar
nimrod committed
alias missing-suggests="aptitude search '~RBsuggests:~i'"
nimrod's avatar
nimrod committed
# shellcheck disable=SC2142
alias deinstalled="dpkg --get-selections | awk 'BEGIN {exitcode=1}; \$2==\"deinstall\" {print \$1; exitcode=0}; END {exit exitcode}'"
nimrod's avatar
nimrod committed
alias ansible-local='ansible localhost -c local -i localhost, -e "ansible_python_interpreter=$(which python3)"'
alias ansible-local-playbook='ansible-playbook -i localhost, -c local -e "ansible_python_interpreter=$(which python3)"'
alias concat="perl -pe 's/\\n/\\\\n/g'"
alias deconcat="perl -pe 's/\\\\n/\\n/g'"
alias hostlocal='docker run --rm --privileged --net=host gliderlabs/hostlocal'
nimrod's avatar
nimrod committed
alias cadvisor='docker run --rm   --volume=/:/rootfs:ro --volume=/var/run:/var/run:rw --volume=/sys:/sys:ro --volume=/var/lib/docker/:/var/lib/docker:ro --volume=/dev/disk/:/dev/disk:ro --publish=8080:8080 --detach=true --name=cadvisor google/cadvisor:latest'
alias cdtemp='cd $(mktemp -d)'
alias 0-day-cleanup='ssh kodi.shore.co.il "sudo -u debian-transmission find /srv/library/Comics -name *.part -path *0-Day\ Week\ of* -delete"'
nimrod's avatar
nimrod committed
alias httpbin='gunicorn httpbin:app --bind 0.0.0.0:8080'
alias update-requirements='find -name "*requirements*.txt" -exec pur --requirement {} \;'
alias restart-kodi='ssh kodi.shore.co.il "sudo systemctl kill --kill-who=all --signal=9 xorg.service"'
nimrod's avatar
nimrod committed
# shellcheck disable=SC2142
alias tolower='awk "{print tolower(\$0)}"'
nimrod's avatar
nimrod committed
# shellcheck disable=SC2142
alias toupper='awk "{print toupper(\$0)}"'
alias wifi-portal='curl --silent --fail --write-out "%{redirect_url}" --output /dev/null http://detectportal.firefox.com/success.txt'
alias transmission-remote='forward kodi.shore.co.il 9091:localhost:9091 && transmission-remote'
alias kpcli='kpcli --kdb ~/Documents/Database.kdbx'
alias gen-mac="hexdump -n5 -e '\"02\" 5/1 \":%02X\" \"\\n\"' /dev/urandom"
alias clean-swp="find \$HOME/ -name '*.swp' -delete"
alias unssh="ssh -o \"UserKnownHostsFile /dev/null\" -o \"StrictHostKeyChecking no\""
alias todo="vim \$HOME/Documents/TODO.yml"
alias xargs="xargs "
nimrod's avatar
nimrod committed
alias monitor="monitor "
alias sudome="sudome "
nimrod's avatar
nimrod committed
alias presentation='docker dev adarnimrod/presentation'
nimrod's avatar
nimrod committed
# shellcheck disable=SC1004
nimrod's avatar
nimrod committed
alias netdata='docker run --detach \
                          --name netdata \
                          --cap-add SYS_PTRACE \
                          --volume netdatalib:/var/lib/netdata \
                          --volume netdatacache:/var/cache/netdata \
                          --volume /etc/os-release:/host/etc/os-release:ro \
                          --volume /etc/passwd:/host/etc/passwd:ro \
                          --volume /etc/group:/host/etc/group:ro \
                          --volume /proc:/host/proc:ro \
                          --volume /sys:/host/sys:ro \
                          --volume /var/run/docker.sock:/var/run/docker.sock \
                          --publish 19999:19999 \
                          --security-opt apparmor=unconfined \
                          netdata/netdata'
alias newman='docker run --rm -u "$(id -u):$(id -g)" -v "$PWD:/etc/newman" -t postman/newman_alpine33'
alias http-server='python3 -m http.server 8080'
alias smtp-server='python3 -m smtpd -ndc DebuggingServer'
alias dd='dd status=progress'
alias screenshot-cleanup='find "$HOME/Pictures" -name "Screenshot from *.png" -delete'
alias black='black --line-length 79'
alias torrent_off='ssh kodi.shore.co.il sudo systemctl stop transmission-{rss,daemon}.service'
alias torrent_on='ssh kodi.shore.co.il sudo systemctl start transmission-{daemon,rss}.service'
command -v notify-send > /dev/null || alias notify-send='bell'
nimrod's avatar
nimrod committed
alias detectproxy='w3m http://detectportal.firefox.com/success.txt'
alias color='less --raw-control-chars -p'
alias pip2='python2 -m pip'
alias pip3='python3 -m pip'
nimrod's avatar
nimrod committed
# shellcheck disable=SC2139
nimrod's avatar
nimrod committed
alias rc_make="make --directory $HOME --always-make"
alias rc_update="rc_make vendored generated"
alias gen-ssh-config="rc_make .ssh/config"
alias bfg='java -jar $HOME/.local/share/bfg/bfg.jar'
alias close='ssh -fnNTS ~/.ssh/%C.sock -O exit'
nimrod's avatar
nimrod committed
alias jjb='jenkins-jobs'
nimrod's avatar
nimrod committed
alias diff='diff --unified'
alias check_tcp='nc -vzw10'
alias check_unix='nc -Uvzw3'
nimrod's avatar
nimrod committed
alias listen_tcp='nc -vlk 0.0.0.0'
alias listen_udp='nc -uvlk 0.0.0.0'
alias listen_unix='nc -Uvlk'
nimrod's avatar
nimrod committed
# shellcheck disable=SC2032
alias rm='rm --dir'
nimrod's avatar
nimrod committed
alias nextcloudcmd='flatpak run --command=nextcloudcmd org.nextcloud.Nextcloud'
alias tfa='terraform apply tfplan'
alias tfvf='tfv && terraform fmt -diff'
nimrod's avatar
nimrod committed

if ! command -v notify-send > /dev/null
then
    alias notify-send='bell'
elif [ -n "$GIO_LAUNCHED_DESKTOP_FILE" ]
then
    # shellcheck disable=SC2139
    alias notify-send="notify-send --hint \"string:desktop-entry:$(basename "$GIO_LAUNCHED_DESKTOP_FILE")\""
fi

tfp () {
nimrod's avatar
nimrod committed
    workspace="$(terraform workspace show)"
    if [ "$workspace" = "default" ] || [ ! -f "$workspace.tfvars" ]
nimrod's avatar
nimrod committed
    then
        terraform plan -out tfplan "$@"
    else
        terraform plan -out tfplan -var-file "$workspace.tfvars" "$@"
    fi
}

tfaa () {
nimrod's avatar
nimrod committed
    workspace="$(terraform workspace show)"
    if [ "$workspace" = "default" ] || [ ! -f "$workspace.tfvars" ]
nimrod's avatar
nimrod committed
    then
        terraform apply -auto-approve "$@"
    else
        terraform apply -auto-approve -var-file "$workspace.tfvars" "$@"
    fi
}

tfr () {
nimrod's avatar
nimrod committed
    workspace="$(terraform workspace show)"
    if [ "$workspace" = "default" ] || [ ! -f "$workspace.tfvars" ]
nimrod's avatar
nimrod committed
    then
        terraform refresh "$@"
    else
        terraform refresh -var-file "$workspace.tfvars" "$@"
    fi
tfi () {
nimrod's avatar
nimrod committed
    workspace="$(terraform workspace show)"
    if [ "$workspace" = "default" ] || [ ! -f "$workspace.tfvars" ]
nimrod's avatar
nimrod committed
    then
        terraform import "$@"
    else
        terraform import -var-file "$workspace.tfvars" "$@"
    fi
nimrod's avatar
nimrod committed
}
nimrod's avatar
nimrod committed

tfv () {
    workspace="$(terraform workspace show)"
    if [ "$workspace" = "default" ] || [ ! -f "$workspace.tfvars" ]
    then
        terraform validate "$@"
    else
        terraform validate -var-file "$workspace.tfvars" "$@"
    fi
}

nimrod's avatar
nimrod committed
genpass () {
    bytes="${1:-32}"
    head --bytes="$bytes" /dev/urandom | base64 --wrap=0
nimrod's avatar
nimrod committed
jt () {
    if command -v pygmentize > /dev/null
nimrod's avatar
nimrod committed
        python3 -m json.tool "$@" | pygmentize -l javascript
nimrod's avatar
nimrod committed
        python3 -m json.tool "$@"
nimrod's avatar
nimrod committed
bold () {
nimrod's avatar
nimrod committed
    printf '\e[1m' || true
nimrod's avatar
nimrod committed
    echo "$@"
nimrod's avatar
nimrod committed
    printf '\e[0m' || true
nimrod's avatar
nimrod committed
}

red () {
nimrod's avatar
nimrod committed
    printf '\e[1;91m' || true
nimrod's avatar
nimrod committed
    echo "$@"
nimrod's avatar
nimrod committed
    printf '\e[0m' || true
nimrod's avatar
nimrod committed
}

green () {
nimrod's avatar
nimrod committed
    printf '\e[1;92m' || true
nimrod's avatar
nimrod committed
    echo "$@"
nimrod's avatar
nimrod committed
    printf '\e[0m' || true
nimrod's avatar
nimrod committed
}

yellow () {
nimrod's avatar
nimrod committed
    printf '\e[1;93m' || true
nimrod's avatar
nimrod committed
    echo "$@"
nimrod's avatar
nimrod committed
    printf '\e[0m' || true
nimrod's avatar
nimrod committed
}

blue () {
nimrod's avatar
nimrod committed
    printf '\e[1;94m' || true
nimrod's avatar
nimrod committed
    echo "$@"
nimrod's avatar
nimrod committed
    printf '\e[0m' || true
nimrod's avatar
nimrod committed
}

magenta () {
nimrod's avatar
nimrod committed
    printf '\e[1;95m' || true
nimrod's avatar
nimrod committed
    echo "$@"
nimrod's avatar
nimrod committed
    printf '\e[0m' || true
nimrod's avatar
nimrod committed
}

cyan () {
nimrod's avatar
nimrod committed
    printf '\e[1;96m' || true
nimrod's avatar
nimrod committed
    echo "$@"
nimrod's avatar
nimrod committed
    printf '\e[0m' || true
nimrod's avatar
nimrod committed
}
    ssh-keyscan "$@" >> "$HOME/.ssh/known_hosts"
    sort -uo "$HOME/.ssh/known_hosts" "$HOME/.ssh/known_hosts"
nimrod's avatar
nimrod committed
    name="${1:-site}"
    openssl req -new -newkey rsa:4096 -nodes -out "$name.csr" -keyout "$name.key"
nimrod's avatar
nimrod committed
}
nimrod's avatar
nimrod committed
    local this_month last_month format
    format='+kodi.shore.co.il:/srv/library/Comics/0-Day\ Week\ of\ %Y.%m.*'
nimrod's avatar
nimrod committed
    this_month="$( date "$format" )"
    last_month="$( date --date '1 month ago' "$format" )"
nimrod's avatar
nimrod committed
    rsync --prune-empty-dirs --ignore-missing-args --recursive --compress --progress --exclude "*.part" "$last_month" "$this_month" "$HOME/Downloads/Comics/"
nimrod's avatar
nimrod committed
    # shellcheck disable=SC2033
    find "$HOME/Downloads/Comics/" -name "$(date --date '2 month ago' +'0-Day\ Week\ of\ %Y.%m.*')" -exec rm -r {} +
sync_podcasts () (
    cd || exit 1
    unison podcasts
)

nimrod's avatar
nimrod committed
    w3m "https://duckduckgo.com/lite/?q=$(echo "$@" | urlencode)"
match_ssl_pair () {
    if [ "$#" -ne 2 ]
    then
        echo "Usage: match_ssl_pair private_key certificate"
        return 1
    fi
    tempkey="$(mktemp)"
    tempcert="$(mktemp)"
    openssl pkey -pubout -outform PEM -in "$1" > "$tempkey"
    openssl x509 -pubkey -noout -in "$2" > "$tempcert"
    cmp "$tempkey" "$tempcert" > /dev/null
    exitcode="$?"
    rm "$tempkey" "$tempcert"
    return "$exitcode"
}

nimrod's avatar
nimrod committed
flatpak_kill () {
    if [ "$#" -lt 1 ]
    then
nimrod's avatar
nimrod committed
        echo "You must specify application name." >&2
        false
    else
        name="$1"
        shift
        for pid in $(flatpak ps --columns=application,pid | awk "tolower(\$2) ~ /$name/ {print \$3}")
        do
            pkill "$@" "$pid"
        done
    fi
}

# shellcheck disable=SC2120
prune_ssh_sockets () {
nimrod's avatar
nimrod committed
    { [ "${1:-}" != '-f' ] && [ "${1:-}" != '--force' ]; } || killall -v ssh || true
    find ~/.ssh/ \
        -maxdepth 1 \
        -type s \
        \! -name 'cm_*.sock' \
        -execdir sh -c 'lsof -t "$1" >/dev/null || rm "$1"' _ {} \;
__prompt () {
    local exitstatus="$?"
nimrod's avatar
nimrod committed
    local runduration endtime pre_prompt
    ! [ "$(type history 2> /dev/null)" = 'history is a shell builtin' ] || history -a
    if [ -n "${starttime:-}" ]
        runduration="$(( endtime - starttime))"
nimrod's avatar
nimrod committed
        pre_prompt=''
        [ "$exitstatus" -eq '0' ] || [ -z "${run_command:-}" ] || pre_prompt="$pre_prompt\\e[1;91m[Exit status: $exitstatus]\\e[0m "
        [ "$runduration" -lt '5' ] || pre_prompt="$pre_prompt\\e[1;96m[Run duration: $runduration]\\e[0m "
        [ -z "$pre_prompt" ] || echo -e "$pre_prompt"
    last_command='__prompt'
    unset starttime
nimrod's avatar
nimrod committed
    trap __command_notifier debug
}

__command_notifier () {
    local exitstatus="$?"
    local runduration endtime
    endtime="$(date +%s)"
    if [ "${last_command:-}" = '__prompt' ]
    then
        starttime="$(date +%s)"
    elif [ -n "${last_finish:-}" ]
nimrod's avatar
nimrod committed
    then
        if [ "${last_command:-}" != '_direnv_hook' ]
        then
            run_command='1'
        fi
        runduration="$(( endtime - last_finish ))"
        if [ "$runduration" -gt '10' ]
nimrod's avatar
nimrod committed
        then
            if [ "$exitstatus" -eq '0' ]
nimrod's avatar
nimrod committed
            then
                notify-send "$last_command has finished."
            else
nimrod's avatar
nimrod committed
                notify-send --urgency=critical "$last_command has failed."
nimrod's avatar
nimrod committed
            fi
        fi
    fi
    last_command="$BASH_COMMAND"
    last_finish="$(date +%s)"
    trap - debug
}

# shellcheck disable=SC1090
. "$HOME/Repositories/Shore/bundle_certs/bundle_certs"
nimrod's avatar
nimrod committed

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    alias grep='grep --color=auto'
nimrod's avatar
nimrod committed
    alias diff='diff --color=auto --unified'
nimrod's avatar
nimrod committed
    alias ip='ip -color'
    alias less='less --raw-control-chars'
fi

    export CDPATH=".:$HOME:$HOME/Documents:$HOME/Documents/Shore:$HOME/Repositories/Shore:$HOME/Repositories/Endless:$HOME/Documents/Endless"
nimrod's avatar
nimrod committed
    # shellcheck disable=SC2016
    export PROMPT_COMMAND='__prompt'
    shopt -s checkwinsize
    shopt -s cmdhist
    [ -f /etc/bash_completion ] && . /etc/bash_completion

    # shellcheck disable=SC1090
nimrod's avatar
nimrod committed
    for sourcefile in "$HOME"/.bash_completion.d/*
    do
        [ ! -f "$sourcefile" ] || . "$sourcefile"
    done
    ! command -v direnv > /dev/null || eval "$(direnv hook bash)"
# shellcheck disable=SC2119
prune_ssh_sockets