1
1
zsh-david/zsh-david.plugin.zsh

183 lines
4.8 KiB
Bash

# My personal ZSH plugin with custom aliases and functions.
# It might not be very useful to you.
# **** zsh options ****
set -o shwordsplit
setopt HIST_IGNORE_SPACE
# **** am i macos? ****
#if [ "$(uname)" == "Darwin" ]; then
#else
#fi
# **** variables ****
if $(command -v helix > /dev/null); then
export EDITOR=helix
elif $(command -v hx > /dev/null); then
export EDITOR=hx
else
export EDITOR=vim
fi
export VISUAL=$EDITOR
if [[ $LANG = '' ]]; then
export LANG=en_US.UTF-8
fi
# **** aliases ****
alias c="clear"
alias s="ssh"
alias m="mosh --family=prefer-inet6"
alias g="git"
alias zz='source $HOME/.zshrc'
alias vz='$EDITOR $HOME/.zshrc && source $HOME/.zshrc'
alias vp='$EDITOR $ZPREZTODIR/contrib/david/zsh-david.plugin.zsh && source $ZPREZTODIR/contrib/*/*.zsh'
alias {v,vi,vim,e}='$EDITOR'
alias vv='$EDITOR $HOME/.vimrc'
alias vh="sudo $EDITOR /etc/hosts; echo hosts updated"
alias vs="$EDITOR ~/.ssh/config"
alias serve="python2 -m SimpleHTTPServer"
alias push="git push origin main"
alias pull="git pull origin main"
alias commit="git commit -a"
#alias aec="sshuttle -vvr 90.146.8.233 192.168.6.0/24 90.146.8.0/26 10.9.0.0/26 10.12.0.0/16 10.2.0.0/26 192.168.3.0/24 10.12.4.0/22 192.168.42.0/24"
#alias home="sshuttle -vvr stargazer.socialnerds.org:2222 10.1.0.0/16"
#alias dmz="sshuttle -vvr 90.146.8.9 90.146.8.0/26"
alias cleardnscache="sudo killall -HUP mDNSResponder; sudo dscacheutil -flushcache; say DNS cache has been cleared"
alias vc="code -n ."
alias {systemctl,sc}='sudo systemctl'
alias {journalctl,jc}='sudo journalctl'
alias {myip,myip4}="curl http://4.ifconfig.pro"
alias myip6="curl http://6.ifconfig.pro"
alias wg='sudo wg'
alias {p,pacman}='sudo pacman'
alias {pu,pacu}='sudo pacman -Syu --noconfirm'
alias {pi,paci}='sudo pacman -S --noconfirm'
alias {a,apt}="sudo apt"
alias {ai,apti}="sudo apt install"
alias {au,aptu}="sudo apt update"
alias {u,ufw}="sudo ufw"
alias dc="docker compose"
alias d="docker"
alias {vanity,vanity-address}="docker run --rm -it socialnerds/wg-vanity-address:latest --in 5"
# Multipass
alias mp="multipass"
alias {phobos,pho}="multipass shell phobos"
# **** function definitions ****
## output handling
log() {
case $1 in
"error")
level="\033[31m✗\033[0m"
;;
"warn")
level="\033[33m!\033[0m"
;;
"debug")
level="\033[2md\033[0m"
;;
"success")
level="\033[32m✓\033[0m"
;;
"info")
level="i"
;;
*)
level="\033[31mLoglevel unknown. Programming error? ($1)\033[0m"
;;
esac
sleep 0.5; echo -e "[$level] ${@:2}"
}
## Update systems packages
update() {
bold=$(tput bold)
normal=$(tput sgr0)
if [ "$(uname)" = "Darwin" ]; then
log info "macOS detected. Using ${bold}brew${normal} to update packages."
brew update && brew upgrade
if [ $? -eq 0 ]; then
log success "All packages up-to-date."
else
log error "Something went wrong while running brew."
fi
else
#if not Darwin then its probably Linux
systems="ubuntu arch raspbian antergos elementary manjaro"
issue="$(cat /etc/issue)"
for item in $systems; do
echo $issue | grep -i $item >> /dev/null
if [ $? -eq 0 ]; then
system=$item
fi
done
case $system in
ubuntu|debian|raspbian|elementary)
log info "Debian based system detected. Using ${bold}apt${normal} to update packages."
sudo apt update && sudo apt dist-upgrade -y && sudo apt autoremove -y && sudo apt autoclean -y
if [ $? -eq 0 ]; then
log success "All packages up-to-date."
else
log error "Something went wrong while running ${bold}apt${normal}."
fi
;;
arch|antergos|manjaro)
log info "Arch Linux detected. Using ${bold}pacman${normal} to update packages."
sudo pacman -Syu --noconfirm
if [ $? -eq 0 ]; then
log success "All packages up-to-date."
else
log error "Something went wrong while running ${bold}pacman${normal}."
fi
;;
?)
log error "Unknown system. Aborting."
return 1
;;
esac
if [ $? -ne 0 ]; then
echo "error: something went wrong."
return 1
fi
fi
}
## update authorized_keys
update-keys() {
local SSH_PATH="$HOME/.ssh"
local SSH_KEYS="$SSH_PATH/authorized_keys"
local KEYS_URL="https://public.socialnerds.org/ssh/$USER.pub"
if [ ! -d $SSH_PATH ]; then
mkdir -p $SSH_PATH
log warn "Created new config folder [$SSH_PATH]"
fi
chmod 700 $SSH_PATH
log info "Downloading updated authorized_keys file. [$SSH_KEYS]"
curl -s -u "$USER" -o "$SSH_KEYS" "$KEYS_URL"
if [ $? -eq 0 ]; then
chmod 600 $SSH_KEYS
log success "Successfully updated authorized_keys file. [$SSH_KEYS]"
else
log error "Something went wrong while updating authorized_keys file. [$SSH_KEYS]"
fi
}