From 52b0cb0d84ad06a7e266b6221ee75ed57117a130 Mon Sep 17 00:00:00 2001 From: Tony Blyler Date: Fri, 6 Dec 2019 16:31:10 -0500 Subject: [PATCH] Cleanup a bunch of scripts and add an installer script --- home/.config/nvim/init.vim | 7 + home/.scripts/auto-install.sh | 19 --- home/.scripts/fup.sh | 34 ---- home/.scripts/note.sh | 26 --- home/.scripts/piknik.sh | 12 -- home/.zshrc | 158 ++++++++++++++++++ home/.zshrc.d/00_go.sh | 13 ++ .../01_update_go_apps.sh} | 10 +- home/.zshrc.d/02_upgrade_system.sh | 35 ++++ home/.zshrc.d/fup.sh | 37 ++++ home/{.scripts => .zshrc.d}/ip-int-convert.sh | 8 +- home/{.scripts => .zshrc.d}/sshtmux.sh | 2 +- home/.zshrc.d/weather.sh | 17 ++ install.sh | 103 ++++++++++++ 14 files changed, 382 insertions(+), 99 deletions(-) delete mode 100755 home/.scripts/auto-install.sh delete mode 100755 home/.scripts/fup.sh delete mode 100644 home/.scripts/note.sh delete mode 100644 home/.scripts/piknik.sh create mode 100644 home/.zshrc create mode 100755 home/.zshrc.d/00_go.sh rename home/{.scripts/update_go_apps.sh => .zshrc.d/01_update_go_apps.sh} (66%) create mode 100755 home/.zshrc.d/02_upgrade_system.sh create mode 100755 home/.zshrc.d/fup.sh rename home/{.scripts => .zshrc.d}/ip-int-convert.sh (74%) rename home/{.scripts => .zshrc.d}/sshtmux.sh (95%) create mode 100755 home/.zshrc.d/weather.sh create mode 100755 install.sh diff --git a/home/.config/nvim/init.vim b/home/.config/nvim/init.vim index 510598f..6d81ade 100644 --- a/home/.config/nvim/init.vim +++ b/home/.config/nvim/init.vim @@ -10,6 +10,13 @@ function GoPostUpdate() :GoUpdateBinaries endfunction +" automatically install vim-plug if it doesn't exist +if empty(glob('~/.vim/autoload/plug.vim')) + silent !curl -fLo ~/.vim/autoload/plug.vim --create-dirs + \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim + autocmd VimEnter * PlugInstall --sync | source $MYVIMRC +endif + call plug#begin($HOME.'/.nvim/plugged') if s:uname == "Linux" diff --git a/home/.scripts/auto-install.sh b/home/.scripts/auto-install.sh deleted file mode 100755 index 73ccd08..0000000 --- a/home/.scripts/auto-install.sh +++ /dev/null @@ -1,19 +0,0 @@ -# auto install oh-my-zsh and tpm -if which git > /dev/null 2>&1; then - # if oh-my-zsh is not installed, autoinstall it - if [ ! -e "${ZSH}" ]; then - echo 'oh-my-zsh is missing... autoinstalling now' - sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)" - fi - - local TPM="${HOME}/.tmux/plugins/tpm" - # if tpm (Tmux Plugin Manager) is not installed, autoinstall it - if [ ! -e "${TPM}" ]; then - git clone https://github.com/tmux-plugins/tpm "${TPM}" - fi - - if ! egrep -q 'run.*tpm' "${HOME}/.tmux.conf" 2>&1; then - echo 'adding TPM to ~/.tmux.conf' - echo "run '${HOME}/.tmux/plugins/tpm/tpm'" >> "${HOME}/.tmux.conf" - fi -fi diff --git a/home/.scripts/fup.sh b/home/.scripts/fup.sh deleted file mode 100755 index cf87176..0000000 --- a/home/.scripts/fup.sh +++ /dev/null @@ -1,34 +0,0 @@ -# fup uploads a given file to file.io -fup() { - if [ -z "${1}" -o ! -f "${1}" ]; then - echo 'provide a valid file path' - return 1 - fi - - local cmd="-F 'file=@${1}' https://file.io/" - - if [ ! -z "${2}" ]; then - cmd="${cmd}\?expires=${2}" - fi - - local json_return="$(eval "curl ${cmd}")" - if [ $? -ne 0 ]; then - echo 'Curl failed' - return 1 - fi - - if [ $(echo "${json_return}" | grep -c '"success":false') -ne 0 ]; then - echo "${json_return}" - return 1 - fi - - echo "https://file.io/$(echo "${json_return}" | sed 's/.*key":"//' | sed 's/".*//')" - - if [ $(echo "${json_return}" | grep -c '"expiry":') -eq 0 ]; then - echo 'Expires in 14 days' - else - echo "Expires in $(echo "${json_return}" | sed 's/.*expiry":"//' | sed 's/".*//')" - fi - - return 0 -} diff --git a/home/.scripts/note.sh b/home/.scripts/note.sh deleted file mode 100644 index ea8833f..0000000 --- a/home/.scripts/note.sh +++ /dev/null @@ -1,26 +0,0 @@ -note() { - local DATE="$(date '+%s')" - local BASE_NOTE_PATH="${NOTE_PATH:-$(pwd)}" - local IS_DATE_PATH=1 - if [[ "${1}" =~ '^ *[-+]{0,1}[0-9]+ *$' ]]; then - local DAY=86400 - local DATE=$(( DATE + (DAY * ${1}) )) - - elif [[ -n "${1}" ]]; then - local IS_DATE_PATH='' - local NOTE_PATH="${BASE_NOTE_PATH}/${1}" - if ! [[ "${1}" =~ '\.[mM][dD]$' ]]; then - local NOTE_PATH="${NOTE_PATH}.md" - fi - fi - - if [[ ${IS_DATE_PATH} ]]; then - local NOTE_PATH="${BASE_NOTE_PATH}/$(date --date "@${DATE}" '+%Y/%m')" - mkdir -p "${NOTE_PATH}" || return 1 - local NOTE_PATH="${NOTE_PATH}/$(date --date "@${DATE}" '+%d').md" - else - mkdir -p "$(dirname "${NOTE_PATH}")" || return 2 - fi - - vim "${NOTE_PATH}" || return 3 -} diff --git a/home/.scripts/piknik.sh b/home/.scripts/piknik.sh deleted file mode 100644 index 95c3404..0000000 --- a/home/.scripts/piknik.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash -# pkc : read the content to copy to the clipboard from STDIN -alias pkc='piknik -copy' - -# pkp : paste the clipboard content -alias pkp='piknik -paste' - -# pkm : move the clipboard content -alias pkm='piknik -move' - -# pkz : delete the clipboard content -alias pkz='piknik -copy < /dev/null' diff --git a/home/.zshrc b/home/.zshrc new file mode 100644 index 0000000..99eee24 --- /dev/null +++ b/home/.zshrc @@ -0,0 +1,158 @@ +export PATH="/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:${PATH}" + +# oh-my-zsh { +export ZSH="${HOME}/.oh-my-zsh" + +ZSH_THEME="candy" +HYPHEN_INSENSITIVE="true" +export UPDATE_ZSH_DAYS=7 +DISABLE_UPDATE_PROMPT="true" +HIST_STAMPS="mm/dd/yyyy" +plugins=(battery catimg docker fzf git go golang grunt helm kube-ps1 kubectl minikube node npm pip ssh-agent sudo tmux) + +source $ZSH/oh-my-zsh.sh +# } + +# zsh { +HISTFILE="${HOME}/.histfile" +HISTSIZE=1048576 +SAVEHIST=1048576 +setopt \ + appendhistory \ + autocd \ + beep \ + extendedglob \ + nomatch \ + notify \ + HIST_IGNORE_DUPS \ + INC_APPEND_HISTORY \ + SHARE_HISTORY \ + HIST_REDUCE_BLANKS +# } + +# general { +export LANG='en_US.UTF-8' +export TERM='xterm-256color' +export EDITOR='vim' +# if nvim is installed, effectively replace vim +if command -v nvim &> /dev/null; then + export EDITOR='nvim' + alias vi='nvim' + alias vim='nvim' + alias vimdiff="nvim -d" + alias view="nvim -R" +fi +export VISUAL="${EDITOR}" + +# use reflink cp if supported (yay CoW) +if cp --help | grep -q reflink; then + alias cp='cp -i --reflink=auto' +else + alias cp='cp -i' +fi +alias mv='mv -i' +alias dco='docker-compose' +alias mk='minikube' +alias k='kubectl' +alias kctx='kubectx' +alias kns='kubens' + +REPOS="${HOME}/repos" + +PHPCS_BIN="${REPOS}/phpcs/scripts" +if [ -d "${PHPCS_BIN}" ]; then + export PATH="${PHPCS_BIN}:${PATH}" +fi + +CARGO_ENV="${HOME}/.cargo/env" +if [ -f "${CARGO_ENV}" ]; then + source "${CARGO_ENV}" +fi + +if [ -x "${REPOS}/termpdf/termpdf" ]; then + alias termpdf="${REPOS}/termpdf/termpdf" +fi + +# add awless autocompletion if available +if command -v awless &> /dev/null; then + source <(awless completion zsh) +fi + +# add kubectl autocompletion if available +if command -v kubectl &> /dev/null; then + source <(kubectl completion zsh) +fi + +# add helm autocompletion if available +if command -v helm &> /dev/null; then + source <(helm completion zsh) +fi + +# add aws autocompletion if available +if command -v aws &> /dev/null; then + complete -C aws_completer aws +fi + +if command -v fzf &> /dev/null; then + export FZF_DEFAULT_COMMAND='ag --skip-vcs-ignores --nocolor -g "" -l' + export FZF_CTRL_T_COMMAND="${FZF_DEFAULT_COMMAND}" +fi + +NIX_SH="${HOME}/.nix-profile/etc/profile.d/nix.sh" +if [ -f "${NIX_SH}" ]; then + source "${NIX_SH}" +fi + +export GOAPPS=( + 'github.com/dgraph-io/badger/...' + 'github.com/golang/protobuf/protoc-gen-go' + 'github.com/google/huproxy/huproxyclient' + 'github.com/jedisct1/piknik' + 'github.com/junegunn/fzf' + 'github.com/schachmat/wego' + 'github.com/tomnomnom/gron' + 'github.com/wallix/awless' + 'google.golang.org/grpc' +) + +case `uname` in + "Darwin") + pdf_join() { + join_py="/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py" + echo -n "Name of output file: " + read output_file && "$join_py" -o $output_file $@ && open $output_file + } + + if command -v gtar &> /dev/null; then + alias tar='gtar' + fi + + export VISUAL='nvim' + + #archey -c + ;; + "Linux") + #screenfetch + alias open="xdg-open" + + # swap escape and caps lock keys if gnome + if command -v dconf &> /dev/null; then + if ! dconf read /org/gnome/desktop/input-sources/xkb-options | grep -q 'caps:swapescape'; then + dconf write /org/gnome/desktop/input-sources/xkb-options "['caps:swapescape']" + fi + fi + ;; +esac +# } + +# load any file that ends with .zsh or .sh +for SCRIPT in "${HOME}/.zshrc.d"/**/(.|?)*(.zsh|.sh); do + if ! [ -x "${SCRIPT}" ]; then + continue + fi + + source "${SCRIPT}" +done + +source "${HOME}/.homesick/repos/homeshick/homeshick.sh" +homeshick refresh 5 -q diff --git a/home/.zshrc.d/00_go.sh b/home/.zshrc.d/00_go.sh new file mode 100755 index 0000000..b3bb4b7 --- /dev/null +++ b/home/.zshrc.d/00_go.sh @@ -0,0 +1,13 @@ +#!/bin/bash +export GOROOT="${HOME}/.goroot" +export PATH="${GOROOT}/bin:${PATH}" + +if command -v go &> /dev/null; then + GOPATH="$(go env GOPATH)" + if [ -n "${GOPATH}" ]; then + export GOPATH + export PATH="${GOPATH}/bin:${PATH}" + fi +else + >&2 echo '"go" is missing from the PATH, you should run "install.sh"' +fi diff --git a/home/.scripts/update_go_apps.sh b/home/.zshrc.d/01_update_go_apps.sh similarity index 66% rename from home/.scripts/update_go_apps.sh rename to home/.zshrc.d/01_update_go_apps.sh index b6afafc..768b176 100755 --- a/home/.scripts/update_go_apps.sh +++ b/home/.zshrc.d/01_update_go_apps.sh @@ -1,20 +1,22 @@ +#!/bin/bash # updates go binary installs that you care about. # if the GOAPPS environment variable is set, it is expected to be # an array update_go_apps() { - local APPS=($@) + local APPS=("${@}") - if [ -z "${APPS}" ]; then + if [ -z "${APPS[0]}" ]; then if [ -z "${GOAPPS}" ]; then return fi - APPS=(${GOAPPS[@]}) + APPS=("${GOAPPS[@]}") fi + local APP for APP in "${APPS[@]}"; do echo "Updating ${APP}" - go get -u "${APP}" + go get -u "${APP}" || return $? done } diff --git a/home/.zshrc.d/02_upgrade_system.sh b/home/.zshrc.d/02_upgrade_system.sh new file mode 100755 index 0000000..a2063ec --- /dev/null +++ b/home/.zshrc.d/02_upgrade_system.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +upgrade_system() { + if command -v apt &> /dev/null; then + sudo sh -c 'apt update && apt upgrade' || return $? + fi + + if command -v dnf &> /dev/null; then + sudo dnf upgrade || return $? + fi + + if command -v update_go_apps &> /dev/null; then + update_go_apps || return $? + fi + + if command -v vim &> /dev/null; then + vim -c 'PlugUpgrade | q' || return $? + vim -c 'PlugUpdate | sleep 3 | qa' || return $? + fi + + ( + cd "${HOME}/.tmux/plugins/tpm" || exit 0 + if [ -x ./clean_plugins ]; then + ./clean_plugins || exit $? + fi + + if [ -x ./install_plugins ]; then + ./install_plugins || exit $? + fi + + if [ -x ./update_plugins ]; then + ./update_plugins all || exit $? + fi + ) || return $? +} diff --git a/home/.zshrc.d/fup.sh b/home/.zshrc.d/fup.sh new file mode 100755 index 0000000..53e0bfa --- /dev/null +++ b/home/.zshrc.d/fup.sh @@ -0,0 +1,37 @@ +#!/bin/bash +# fup uploads a given file to file.io +fup() { + local -r FILE_PATH="${1}" + local -r EXPIRATION="${2}" + if ! [ -f "${FILE_PATH}" ]; then + echo 'provide a valid file path' + return 1 + fi + + local URL='https://file.io/' + + if [ -n "${EXPIRATION}" ]; then + URL="${URL}?expires=${EXPIRATION}" + fi + + local JSON_RETURN + if ! JSON_RETURN="$(curl -SsfLF "file=@${FILE_PATH}" "${URL}")"; then + echo 'Curl failed' + return 1 + fi + + if echo "${JSON_RETURN}" | grep -q '"success":false'; then + echo "${JSON_RETURN}" + return 2 + fi + + echo "https://file.io/$(echo "${JSON_RETURN}" | sed 's/.*key":"//' | sed 's/".*//')" + + if echo "${JSON_RETURN}" | grep -q '"expiry":'; then + echo "Expires in $(echo "${JSON_RETURN}" | sed 's/.*expiry":"//' | sed 's/".*//')" + else + echo 'Expires in 14 days' + fi + + return 0 +} diff --git a/home/.scripts/ip-int-convert.sh b/home/.zshrc.d/ip-int-convert.sh similarity index 74% rename from home/.scripts/ip-int-convert.sh rename to home/.zshrc.d/ip-int-convert.sh index ad4c4b8..15016f6 100755 --- a/home/.scripts/ip-int-convert.sh +++ b/home/.zshrc.d/ip-int-convert.sh @@ -1,6 +1,7 @@ +#!/bin/bash # convert integer to IP int2ip () { - local ip dec=$@ + local ip dec=("$@") for e in {3..0} do ((octet = dec / (256 ** e) )) @@ -14,7 +15,8 @@ int2ip () { # convert IP to integer ip2int () { - local a b c d ip=$@ - IFS=. read -r a b c d <<< "$ip" + local a b c d ip + ip=("$@") + IFS=. read -r a b c d <<< "${ip[*]}" printf '%d\n' "$((a * 256 ** 3 + b * 256 ** 2 + c * 256 + d))" } diff --git a/home/.scripts/sshtmux.sh b/home/.zshrc.d/sshtmux.sh similarity index 95% rename from home/.scripts/sshtmux.sh rename to home/.zshrc.d/sshtmux.sh index 5d9e015..42d7d73 100755 --- a/home/.scripts/sshtmux.sh +++ b/home/.zshrc.d/sshtmux.sh @@ -11,7 +11,7 @@ sshtmux() { fi tmux new-window "ssh" - for i in ${@}; do + for i in "${@}"; do tmux split-window -h "ssh -o StrictHostKeyChecking=no $i" tmux select-layout tiled > /dev/null sleep .1s diff --git a/home/.zshrc.d/weather.sh b/home/.zshrc.d/weather.sh new file mode 100755 index 0000000..02d3216 --- /dev/null +++ b/home/.zshrc.d/weather.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +weather() { + if ! command -v wego &> /dev/null; then + return 1 + fi + + local -r LOCK_DATE="/tmp/wego-$(date '+%F-%H')" + if [ -e "${LOCK_DATE}" ]; then + return 0 + fi + + touch "${LOCK_DATE}" + wego +} + +weather diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..173f9ce --- /dev/null +++ b/install.sh @@ -0,0 +1,103 @@ +#!/bin/bash +check_required_cmds() { + local -r REQUIRED_CMDS=( + 'curl' + 'git' + 'sha256sum' + 'tar' + 'tmux' + 'vim' + 'zsh' + ) + + local RETURN=0 + local REQUIRED_CMD + for REQUIRED_CMD in ${REQUIRED_CMDS[*]}; do + if ! command -v "${REQUIRED_CMD}" &> /dev/null; then + >&2 echo "Missing '${REQUIRED_CMD}' from PATH" + RETURN=1 + fi + done + + return "${RETURN}" +} + +install_homeshick() { + local -r HOMESHICK_DIR="${HOME}/.homesick/repos/homeshick" + if [ -d "${HOMESHICK_DIR}" ]; then + echo 'homeshick is already installed' + return + fi + + git clone https://github.com/andsens/homeshick.git "${HOMESHICK_DIR}" +} + +install_ohmyzsh() { + if [ -d "${HOME}/.oh-my-zsh" ]; then + echo 'oh-my-zsh is already installed' + return + fi + + /bin/bash -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)" +} + +install_tpm() { + local -r TPM="${HOME}/.tmux/plugins/tpm" + if [ -d "${TPM}" ]; then + echo 'tpm is already installed' + return + fi + + git clone https://github.com/tmux-plugins/tpm "${TPM}" +} + +install_go() { + local -r GO_VERSION='1.13.5' + local -r GO_SHA256='512103d7ad296467814a6e3f635631bd35574cab3369a97a323c9a585ccaa569' + source "./home/.zshrc.d/00_go.sh" + + if command -v go &> /dev/null; then + echo 'go is already installed' + return + fi + + echo 'Installing Go 1.13.5, ensure that this is the version you want' + local -r GO_TAR_FILE="$(mktemp)" + if ! [ -f "${GO_TAR_FILE}" ]; then + >&2 echo 'failed to create a file for the go tar' + return 1 + fi + + if ! curl -L "https://dl.google.com/go/go${GO_VERSION}.linux-amd64.tar.gz" | + tee "${GO_TAR_FILE}" | + sha256sum | + grep -Fq "${GO_SHA256}"; then + + >&2 echo 'failed to download go tar' + return 2 + fi + + mkdir -p "${GOROOT}" || return $? + + ( + cd "${GOROOT}" || exit $? + tar -xzf "${GO_TAR_FILE}" || exit $? + mv go/* ./ || exit $? + rmdir go || exit $? + ) || return $? + + rm "${GO_TAR_FILE}" || true +} + +check_required_cmds || exit $? + +echo 'installing homeshick' +install_homeshick +echo 'installing go' +install_go || exit $? +echo 'installing tpm' +install_tpm || exit $? +echo 'installing oh-my-zsh' +install_ohmyzsh || exit $? + +echo 'now in zsh, run "upgrade_system"'