From 93351f4ff864bd1a811ea17c49010188250b76c1 Mon Sep 17 00:00:00 2001 From: Mike Date: Fri, 2 Aug 2024 15:04:05 +0200 Subject: [PATCH] feat: update git scripts to use the new shell template functions --- bin/automount.sh | 127 ++++++++++- bin/get_nvim.sh | 575 ++++++++++++++++++++++++++++++----------------- git/bin/git-co | 100 +++++++-- git/bin/git-dev | 151 ++++++++++--- git/bin/git-edit | 337 ++++++++++++++++++++++++--- git/bin/git-pla | 150 ++++++++++--- git/bin/git-user | 159 ++++++++++--- git/bin/git-wt | 102 ++++++--- 8 files changed, 1327 insertions(+), 374 deletions(-) diff --git a/bin/automount.sh b/bin/automount.sh index ec4547e..04f1e30 100755 --- a/bin/automount.sh +++ b/bin/automount.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash +# shellcheck disable=SC2317 # # -` @@ -22,6 +23,7 @@ # .` `/ VERBOSE=0 +QUIET=0 NOCOLOR=0 NOLOG=1 WARN_COUNT=0 @@ -87,10 +89,12 @@ case "$SHELL_PLATFORM" in linux) if [[ -f /etc/arch-release ]]; then OS='arch' - elif [[ "$(cat /etc/issue)" == Ubuntu* ]]; then + elif [[ -f /etc/redhat-release ]] && [[ "$(cat /etc/redhat-release)" == Red\ Hat* ]]; then + OS='redhat' + elif [[ -f /etc/issue ]] && [[ "$(cat /etc/issue)" == Ubuntu* ]]; then OS='ubuntu' elif [[ -f /etc/debian_version ]] || [[ "$(cat /etc/issue)" == Debian* ]]; then - if [[ $ARCH == *\ armv7* ]]; then # Raspberry pi 3 uses armv7 cpu + if [[ $ARCH =~ armv.* ]] || [[ $ARCH == aarch64 ]]; then OS='raspbian' else OS='debian' @@ -108,12 +112,72 @@ case "$SHELL_PLATFORM" in ;; esac +if ! hash is_windows 2>/dev/null; then + function is_windows() { + if [[ $SHELL_PLATFORM =~ (msys|cygwin|windows) ]]; then + return 0 + fi + return 1 + } +fi + +if ! hash is_wls 2>/dev/null; then + function is_wls() { + if [[ "$(uname -r)" =~ Microsoft ]]; then + return 0 + fi + return 1 + } +fi + +if ! hash is_osx 2>/dev/null; then function is_osx() { if [[ $SHELL_PLATFORM == 'osx' ]]; then return 0 fi return 1 } +fi + +if ! hash is_root 2>/dev/null; then + function is_root() { + if ! is_windows && [[ $EUID -eq 0 ]]; then + return 0 + fi + return 1 + } +fi + +if ! hash has_sudo 2>/dev/null; then + function has_sudo() { + if ! is_windows && hash sudo 2>/dev/null && [[ "$(groups)" =~ sudo ]]; then + return 0 + fi + return 1 + } +fi + +if ! hash is_arm 2>/dev/null ; then + function is_arm() { + local arch + arch="$(uname -m)" + if [[ $arch =~ ^arm ]] || [[ $arch =~ ^aarch ]]; then + return 0 + fi + return 1 + } +fi + +if ! hash is_64bits 2>/dev/null; then + function is_64bits() { + local arch + arch="$(uname -m)" + if [[ $arch == 'x86_64' ]] || [[ $arch == 'arm64' ]]; then + return 0 + fi + return 1 + } +fi # colors # shellcheck disable=SC2034 @@ -150,17 +214,20 @@ Usage: --nolog Disable log writing --nocolor Disable color output -v, --verbose Enable debug messages + -q, --quiet Suppress all output but the errors -h, --help Display this help message EOF } function warn_msg() { local msg="$1" + if [[ $QUIET -eq 0 ]]; then if [[ $NOCOLOR -eq 0 ]]; then printf "${yellow}[!] Warning:${reset_color}\t %s\n" "$msg" else printf "[!] Warning:\t %s\n" "$msg" fi + fi WARN_COUNT=$((WARN_COUNT + 1)) if [[ $NOLOG -eq 0 ]]; then printf "[!] Warning:\t %s\n" "$msg" >>"${LOG}" @@ -184,11 +251,13 @@ function error_msg() { function status_msg() { local msg="$1" + if [[ $QUIET -eq 0 ]]; then if [[ $NOCOLOR -eq 0 ]]; then printf "${green}[*] Info:${reset_color}\t %s\n" "$msg" else printf "[*] Info:\t %s\n" "$msg" fi + fi if [[ $NOLOG -eq 0 ]]; then printf "[*] Info:\t\t %s\n" "$msg" >>"${LOG}" fi @@ -268,6 +337,38 @@ function exit_append() { return 0 } +function raw_output() { + local msg="echo \"$1\"" + if [[ $NOLOG -eq 0 ]]; then + msg="$msg | tee ${LOG}" + fi + if ! sh -c "$msg"; then + return 1 + fi + return 0 +} + +function shell_exec() { + local cmd="$1" + if [[ $VERBOSE -eq 1 ]]; then + if [[ $NOLOG -eq 0 ]]; then + cmd="$cmd | tee ${LOG}" + fi + if ! sh -c "$cmd"; then + return 1 + fi + elif [[ $NOLOG -eq 0 ]]; then + if ! sh -c "$cmd >> ${LOG}"; then + return 1 + fi + else + if ! sh -c "$cmd &>/dev/null"; then + return 1 + fi + fi + return 0 +} + while [[ $# -gt 0 ]]; do key="$1" case "$key" in @@ -280,6 +381,9 @@ while [[ $# -gt 0 ]]; do -v | --verbose) VERBOSE=1 ;; + -q | --quiet) + QUIET=1 + ;; -h | --help) help_user exit 0 @@ -292,6 +396,12 @@ while [[ $# -gt 0 ]]; do DRIVE_NAME="$2" shift ;; + # -) + # while read -r from_stdin; do + # FROM_STDIN=("$from_stdin") + # done + # break + # ;; *) initlog error_msg "Unknown argument $key" @@ -309,9 +419,6 @@ verbose_msg "Platform : ${SHELL_PLATFORM}" verbose_msg "Architecture : ${ARCH}" verbose_msg "OS : ${OS}" -####################################################################### -# CODE Goes Here # -####################################################################### if [[ ! -f $NTFS_EXE ]]; then error_msg "Missing ntfs-3g, cannot re-mount the drives" @@ -341,9 +448,9 @@ if [[ -z $DRIVE_DEV ]] || [[ -z $DRIVE_NAME ]] || [[ -z $DISK_INFO ]]; then fi # status_msg "Drive name: $DRIVE_NAME, located in $DRIVE_DEV" -if ! eval '$(df -h | grep -qi "/Volumes/$DRIVE_NAME")'; then +if [[ ! $(shell_exec "df -h | grep -i \"/Volumes/$DRIVE_NAME\"") == "" ]] ; then status_msg "Unmounting ${DRIVE_DEV}" - if ! sudo diskutil unmount "$DRIVE_DEV"; then + if ! shell_exec "sudo diskutil unmount \"$DRIVE_DEV\""; then error_msg "Failed to unmount $DRIVE_NAME" exit 1 fi @@ -351,16 +458,14 @@ fi sleep 1 status_msg "Creating mouting point" -sudo mkdir -p "/Volumes/$DRIVE_NAME" +shell_exec "sudo mkdir -p \"/Volumes/$DRIVE_NAME\"" status_msg "Remounting disk as ${DRIVE_NAME}" if ! sudo "$NTFS_EXE" "$DRIVE_DEV" "/Volumes/$DRIVE_NAME" -o local -o allow_other -o auto_xattr -o auto_cache -o uid="$(id -u)"; then error_msg "Failed to mount $DRIVE_NAME with write permissions" exit 1 fi -####################################################################### -# CODE Goes Here # -####################################################################### + if [[ $ERR_COUNT -gt 0 ]]; then exit 1 fi diff --git a/bin/get_nvim.sh b/bin/get_nvim.sh index eee4c7a..4c04ad5 100755 --- a/bin/get_nvim.sh +++ b/bin/get_nvim.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash +# shellcheck disable=SC2317 # # -` @@ -21,8 +22,28 @@ # `++:. `-/+/ # .` github.com/mike325/dotfiles `/ +VERBOSE=0 +QUIET=0 +NOCOLOR=0 +NOLOG=0 +WARN_COUNT=0 +ERR_COUNT=0 +# FROM_STDIN=() + NAME="$0" NAME="${NAME##*/}" +LOG="${NAME%%.*}.log" + +SCRIPT_PATH="$0" + +SCRIPT_PATH="${SCRIPT_PATH%/*}" + +OS='unknown' +ARCH="$(uname -m)" + +trap '{ exit_append; }' EXIT + +TMP='/tmp/' URL="https://github.com/neovim/neovim" PYTHON_LIBS=0 RUBY_LIBS=0 @@ -30,49 +51,29 @@ RUBY_LIBS=0 CLONE=0 FORCE_INSTALL=0 PORTABLE=0 -VERBOSE=0 +INSTALL_DIR="$HOME/.local" DEV=0 STABLE=0 -NOLOG=1 -NOCOLOR=0 -INSTALL_DIR="$HOME/.local" - -NAME="$0" -NAME="${NAME##*/}" -LOG="${NAME%%.*}.log" - -WARN_COUNT=0 -ERR_COUNT=0 - NVIM_VERSION="latest" -OS='unknown' - -TMP='/tmp/' +if hash realpath 2>/dev/null; then + SCRIPT_PATH=$(realpath "$SCRIPT_PATH") +else + pushd "$SCRIPT_PATH" 1>/dev/null || exit 1 + SCRIPT_PATH="$(pwd -P)" + popd 1>/dev/null || exit 1 +fi -# colors -# shellcheck disable=SC2034 -black="\033[0;30m" -# shellcheck disable=SC2034 -red="\033[0;31m" -# shellcheck disable=SC2034 -green="\033[0;32m" -# shellcheck disable=SC2034 -yellow="\033[0;33m" -# shellcheck disable=SC2034 -blue="\033[0;34m" -# shellcheck disable=SC2034 -purple="\033[0;35m" -# shellcheck disable=SC2034 -cyan="\033[0;36m" -# shellcheck disable=SC2034 -white="\033[0;37;1m" -# shellcheck disable=SC2034 -orange="\033[0;91m" -# shellcheck disable=SC2034 -normal="\033[0m" -# shellcheck disable=SC2034 -reset_color="\033[39m" +if [[ -n $ZSH_NAME ]]; then + CURRENT_SHELL="zsh" +elif [[ -n $BASH ]]; then + CURRENT_SHELL="bash" +else + # shellcheck disable=SC2009,SC2046 + if [[ -z $CURRENT_SHELL ]]; then + CURRENT_SHELL="${SHELL##*/}" + fi +fi if [ -z "$SHELL_PLATFORM" ]; then if [[ -n $TRAVIS_OS_NAME ]]; then @@ -95,10 +96,12 @@ case "$SHELL_PLATFORM" in linux) if [[ -f /etc/arch-release ]]; then OS='arch' - elif [[ "$(cat /etc/issue)" == Ubuntu* ]]; then + elif [[ -f /etc/redhat-release ]] && [[ "$(cat /etc/redhat-release)" == Red\ Hat* ]]; then + OS='redhat' + elif [[ -f /etc/issue ]] && [[ "$(cat /etc/issue)" == Ubuntu* ]]; then OS='ubuntu' - elif [[ -f /etc/debian_version ]]; then - if [[ "$(uname -a)" == *\ armv7* ]]; then # Raspberry pi 3 uses armv7 cpu + elif [[ -f /etc/debian_version ]] || [[ "$(cat /etc/issue)" == Debian* ]]; then + if [[ $ARCH =~ armv.* ]] || [[ $ARCH == aarch64 ]]; then OS='raspbian' else OS='debian' @@ -116,8 +119,6 @@ case "$SHELL_PLATFORM" in ;; esac -# _ARCH="$(uname -m)" - if ! hash is_windows 2>/dev/null; then function is_windows() { if [[ $SHELL_PLATFORM =~ (msys|cygwin|windows) ]]; then @@ -127,6 +128,15 @@ if ! hash is_windows 2>/dev/null; then } fi +if ! hash is_wls 2>/dev/null; then + function is_wls() { + if [[ "$(uname -r)" =~ Microsoft ]]; then + return 0 + fi + return 1 + } +fi + if ! hash is_osx 2>/dev/null; then function is_osx() { if [[ $SHELL_PLATFORM == 'osx' ]]; then @@ -136,17 +146,83 @@ if ! hash is_osx 2>/dev/null; then } fi -function show_help() { +if ! hash is_root 2>/dev/null; then + function is_root() { + if ! is_windows && [[ $EUID -eq 0 ]]; then + return 0 + fi + return 1 + } +fi + +if ! hash has_sudo 2>/dev/null; then + function has_sudo() { + if ! is_windows && hash sudo 2>/dev/null && [[ "$(groups)" =~ sudo ]]; then + return 0 + fi + return 1 + } +fi + +if ! hash is_arm 2>/dev/null; then + function is_arm() { + local arch + arch="$(uname -m)" + if [[ $arch =~ ^arm ]] || [[ $arch =~ ^aarch ]]; then + return 0 + fi + return 1 + } +fi + +if ! hash is_64bits 2>/dev/null; then + function is_64bits() { + local arch + arch="$(uname -m)" + if [[ $arch == 'x86_64' ]] || [[ $arch == 'arm64' ]]; then + return 0 + fi + return 1 + } +fi + +# colors +# shellcheck disable=SC2034 +black="\033[0;30m" +# shellcheck disable=SC2034 +red="\033[0;31m" +# shellcheck disable=SC2034 +green="\033[0;32m" +# shellcheck disable=SC2034 +yellow="\033[0;33m" +# shellcheck disable=SC2034 +blue="\033[0;34m" +# shellcheck disable=SC2034 +purple="\033[0;35m" +# shellcheck disable=SC2034 +cyan="\033[0;36m" +# shellcheck disable=SC2034 +white="\033[0;37;1m" +# shellcheck disable=SC2034 +orange="\033[0;91m" +# shellcheck disable=SC2034 +normal="\033[0m" +# shellcheck disable=SC2034 +reset_color="\033[39m" + +function help_user() { cat < Choose the base root of the repo and move to it before compile the source code, if this options is used with -c/--clone flag it will clone the repo in the desire + -i, --install Path where neovim will be install, default: $INSTALL_DIR -p, --python Install Neovim's python package for python2 and python3 -r, --ruby Install Neovim's ruby package @@ -163,17 +240,20 @@ Usage: --stable Use stable builds/portables instead of stable -C Set the compiler, gcc/clang -v, --verbose Enable debug messages - -h, --help Displays help + -q, --quiet Suppress all output but the errors + -h, --help Display this help message EOF # _show_nvim_help } function warn_msg() { local msg="$1" - if [[ $NOCOLOR -eq 0 ]]; then - printf "${yellow}[!] Warning:${reset_color}\t %s\n" "$msg" - else - printf "[!] Warning:\t %s\n" "$msg" + if [[ $QUIET -eq 0 ]]; then + if [[ $NOCOLOR -eq 0 ]]; then + printf "${yellow}[!] Warning:${reset_color}\t %s\n" "$msg" + else + printf "[!] Warning:\t %s\n" "$msg" + fi fi WARN_COUNT=$((WARN_COUNT + 1)) if [[ $NOLOG -eq 0 ]]; then @@ -198,10 +278,12 @@ function error_msg() { function status_msg() { local msg="$1" - if [[ $NOCOLOR -eq 0 ]]; then - printf "${green}[*] Info:${reset_color}\t %s\n" "$msg" - else - printf "[*] Info:\t %s\n" "$msg" + if [[ $QUIET -eq 0 ]]; then + if [[ $NOCOLOR -eq 0 ]]; then + printf "${green}[*] Info:${reset_color}\t %s\n" "$msg" + else + printf "[*] Info:\t %s\n" "$msg" + fi fi if [[ $NOLOG -eq 0 ]]; then printf "[*] Info:\t\t %s\n" "$msg" >>"${LOG}" @@ -224,176 +306,102 @@ function verbose_msg() { return 0 } -function get_portable() { - local dir - local exe_path - local cmd='' - local name - local pkg - local version - local build - local has_backup=false - - if [[ $OS == 'raspbian' ]]; then - error_msg "There's no neovim portable version for ARM devices" - return 1 +function __parse_args() { + if [[ $# -lt 2 ]]; then + error_msg "Internal error in __parse_args function trying to parse $1" + exit 1 fi - if ! hash curl 2>/dev/null && ! hash wget 2>/dev/null; then - error_msg 'Must have curl or wget to download the latest portable' - return 1 - fi + local flag="$2" + local value="$1" - if hash nvim 2>/dev/null && [[ $FORCE_INSTALL -ne 1 ]]; then - warn_msg "Neovim is already install, use --force to bypass this check" - return 1 - fi + local pattern="^--${flag}=[a-zA-Z0-9.:@_/~-]+$" - if hash curl 2>/dev/null; then - local cmd='curl -L' - [[ $VERBOSE -eq 0 ]] && local cmd="${cmd} -s" - else - local cmd='wget -q0-' + if [[ -n $3 ]]; then + local pattern="^--${flag}=$3$" fi - verbose_msg "Using ${cmd} as command" - - if [[ $DEV -eq 0 ]] && [[ $NVIM_VERSION == 'latest' ]]; then - verbose_msg "Fetching latest stable from ${URL}/tags/" - version=$( eval "${cmd} ${URL}/tags/ | grep -oE 'v0\.[0-9]\.[0-9]' | sort -u | tail -n 1") - status_msg "Downloading version: ${version}" - elif [[ $DEV -eq 1 ]]; then - status_msg "Downloading Nightly" - else - status_msg "Downloading ${NVIM_VERSION}" - fi - - if is_windows; then - dir="$HOME/AppData/Roaming/" - exe_path="$dir/Neovim/nvim" - name="nvim.zip" - pkg='nvim-win64.zip' - elif is_osx; then - dir="$HOME/.local/bin" - exe_path="$dir/nvim" - name="nvim.tar.gz" - pkg='nvim-macos.tar.gz' + if [[ $value =~ $pattern ]]; then + local left_side="${value#*=}" + echo "${left_side/#\~/$HOME}" else - dir="$HOME/.local/bin" - exe_path="$dir/nvim" - name="nvim" - pkg='nvim.appimage' + echo "$value" fi +} - [[ ! -d $dir ]] && mkdir -p "$dir" - - if [[ $DEV -eq 1 ]]; then - build='nightly' - elif [[ $NVIM_VERSION == 'latest' ]]; then - build="${version}" - else - build="v${NVIM_VERSION}" - fi - - verbose_msg "Downloading ${pkg} from $URL/releases/download/${build}/${pkg} to $TMP/$name" - - if ! eval '${cmd} "$URL/releases/download/${build}/${pkg}" -o "$TMP/$name"'; then - error_msg "Fail to download neovim" - return 1 - fi - - [[ ! -d "$HOME/.cache" ]] && mkdir -p "$HOME/.cache" - - if is_windows; then - verbose_msg "Unpacking ${name}" - if [[ -d "$dir/Neovim" ]]; then - verbose_msg "Backing up neovim $dir/Neovim" - cp -rf "$dir/Neovim" "$HOME/.cache/" - has_backup=true - fi - if ! unzip -qo "$TMP/$name" -d "$dir"; then - return 1 - fi - mv "$dir/nvim-win64" "$dir/Neovim" - rm -rf "${TMP:?}/${name}" - elif is_osx; then - pushd "$TMP" >/dev/null || { error_msg "Could not get to $TMP" && exit 1; } - verbose_msg "Unpacking ${name}" - if [[ -f "$dir/nvim" ]]; then - verbose_msg "Backing up neovim $dir/nvim" - cp "$dir/nvim" "$HOME/.cache/nvim_backup" - has_backup=true - fi - if ! tar xzvf "$TMP/$name" && mv "${TMP}/nvim-osx64/*" "$dir/"; then +function initlog() { + if [[ $NOLOG -eq 0 ]]; then + [[ -n $LOG ]] && rm -f "${LOG}" 2>/dev/null + if ! touch "${LOG}" &>/dev/null; then + error_msg "Fail to init log file" + NOLOG=1 return 1 fi - popd >/dev/null || { error_msg "Could not get out of $TMP" && exit 1; } - rm -rf "${TMP:?}/${name}" - rm -rf "${TMP:?}/nvim-osx64" - else - verbose_msg "Installing into $dir/$name" - if [[ -f "$dir/nvim" ]]; then - verbose_msg "Backing up neovim $dir/nvim" - cp "$dir/nvim" "$HOME/.cache/nvim_backup" - has_backup=true + if [[ -f "${SCRIPT_PATH}/shell/banner" ]]; then + cat "${SCRIPT_PATH}/shell/banner" >"${LOG}" fi - chmod u+x "$TMP/$name" && mv "$TMP/$name" "$dir/$name" - fi - - if [[ $(file -b "$exe_path" | awk '{print $1}') == 'ASCII' ]]; then - error_msg "Failed to execute neovim" - if [[ $has_backup == true ]]; then - if is_windows; then - rm -rf "$HOME/AppData/Roaming/Neovim" - status_msg "Restoring neovim $HOME/.cache/Neovim -> $HOME/AppData/Roaming/Neovim" - mv "$HOME/.cache/Neovim" "$HOME/AppData/Roaming/Neovim" - else - status_msg "Restoring neovim $HOME/.cache/nvim -> $dir/nvim" - mv "$HOME/.cache/nvim_backup" "$dir/nvim" - fi + if ! is_osx; then + LOG=$(readlink -e "${LOG}") fi + verbose_msg "Using log at ${LOG}" fi - return 0 } -function get_libs() { - if [[ $PYTHON_LIBS -eq 1 ]]; then - # hash pip2 2> /dev/null && { status_msg "Installing python2 libs" && pip2 install --upgrade --user pynvim; } - hash pip3 2>/dev/null && { status_msg "Installing python3 libs" && pip3 install --upgrade --user pynvim; } - fi +function exit_append() { + if [[ $NOLOG -eq 0 ]]; then + if [[ $WARN_COUNT -gt 0 ]] || [[ $ERR_COUNT -gt 0 ]]; then + printf "\n\n" >>"${LOG}" + fi - if [[ $RUBY_LIBS -eq 1 ]]; then - hash gem 2>/dev/null && { status_msg "Installing ruby libs" && gem install --user-install neovim; } + if [[ $WARN_COUNT -gt 0 ]]; then + printf "[*] Warnings:\t%s\n" "$WARN_COUNT" >>"${LOG}" + fi + if [[ $ERR_COUNT -gt 0 ]]; then + printf "[*] Errors:\t%s\n" "$ERR_COUNT" >>"${LOG}" + fi fi + return 0 } -function __parse_args() { - if [[ $# -lt 2 ]]; then - error_msg "Internal error in __parse_args function trying to parse $1" - exit 1 +function raw_output() { + local msg="echo \"$1\"" + if [[ $NOLOG -eq 0 ]]; then + msg="$msg | tee ${LOG}" fi - - local arg="$1" - local name="$2" - - local pattern="^--${name}=[a-zA-Z0-9.:@_/~-]+$" - - if [[ -n $3 ]]; then - local pattern="^--${name}=$3$" + if ! sh -c "$msg"; then + return 1 fi + return 0 +} - if [[ $arg =~ $pattern ]]; then - local left_side="${arg#*=}" - echo "${left_side/#\~/$HOME}" +function shell_exec() { + local cmd="$1" + if [[ $VERBOSE -eq 1 ]]; then + if [[ $NOLOG -eq 0 ]]; then + cmd="$cmd | tee ${LOG}" + fi + if ! sh -c "$cmd"; then + return 1 + fi + elif [[ $NOLOG -eq 0 ]]; then + if ! sh -c "$cmd >> ${LOG}"; then + return 1 + fi else - echo "$arg" + if ! sh -c "$cmd &>/dev/null"; then + return 1 + fi fi + return 0 } while [[ $# -gt 0 ]]; do key="$1" case "$key" in + --nolog) + NOLOG=1 + ;; --nocolor) NOCOLOR=1 ;; @@ -491,6 +499,9 @@ while [[ $# -gt 0 ]]; do -v | --verbose) VERBOSE=1 ;; + -q | --quiet) + QUIET=1 + ;; --stable) DEV=0 STABLE=1 @@ -500,18 +511,171 @@ while [[ $# -gt 0 ]]; do STABLE=0 ;; -h | --help) - show_help + help_user exit 0 ;; *) + initlog error_msg "Unknown argument $key" - show_help + help_user exit 1 ;; esac shift done +initlog +verbose_msg "Log Disable : ${NOLOG}" +verbose_msg "Current Shell : ${CURRENT_SHELL}" +verbose_msg "Platform : ${SHELL_PLATFORM}" +verbose_msg "Architecture : ${ARCH}" +verbose_msg "OS : ${OS}" + + +function get_portable() { + local dir + local exe_path + local cmd='' + local name + local pkg + local version + local build + local has_backup=false + + if [[ $OS == 'raspbian' ]]; then + error_msg "There's no neovim portable version for ARM devices" + return 1 + fi + + if ! hash curl 2>/dev/null && ! hash wget 2>/dev/null; then + error_msg 'Must have curl or wget to download the latest portable' + return 1 + fi + + if hash nvim 2>/dev/null && [[ $FORCE_INSTALL -ne 1 ]]; then + warn_msg "Neovim is already install, use --force to bypass this check" + return 1 + fi + + if hash curl 2>/dev/null; then + local cmd='curl -L' + [[ $VERBOSE -eq 0 ]] && local cmd="${cmd} -s" + else + local cmd='wget -q0-' + fi + + verbose_msg "Using ${cmd} as command" + + if [[ $DEV -eq 0 ]] && [[ $NVIM_VERSION == 'latest' ]]; then + verbose_msg "Fetching latest stable from ${URL}/tags/" + version=$( eval "${cmd} ${URL}/tags/ | grep -oE 'v0\.[0-9]\.[0-9]' | sort -u | tail -n 1") + status_msg "Downloading version: ${version}" + elif [[ $DEV -eq 1 ]]; then + status_msg "Downloading Nightly" + else + status_msg "Downloading ${NVIM_VERSION}" + fi + + if is_windows; then + dir="$HOME/AppData/Roaming/" + exe_path="$dir/Neovim/nvim" + name="nvim.zip" + pkg='nvim-win64.zip' + elif is_osx; then + dir="$HOME/.local/bin" + exe_path="$dir/nvim" + name="nvim.tar.gz" + pkg='nvim-macos.tar.gz' + else + dir="$HOME/.local/bin" + exe_path="$dir/nvim" + name="nvim" + pkg='nvim.appimage' + fi + + [[ ! -d $dir ]] && mkdir -p "$dir" + + if [[ $DEV -eq 1 ]]; then + build='nightly' + elif [[ $NVIM_VERSION == 'latest' ]]; then + build="${version}" + else + build="v${NVIM_VERSION}" + fi + + verbose_msg "Downloading ${pkg} from $URL/releases/download/${build}/${pkg} to $TMP/$name" + + if ! eval '${cmd} "$URL/releases/download/${build}/${pkg}" -o "$TMP/$name"'; then + error_msg "Fail to download neovim" + return 1 + fi + + [[ ! -d "$HOME/.cache" ]] && mkdir -p "$HOME/.cache" + + if is_windows; then + verbose_msg "Unpacking ${name}" + if [[ -d "$dir/Neovim" ]]; then + verbose_msg "Backing up neovim $dir/Neovim" + cp -rf "$dir/Neovim" "$HOME/.cache/" + has_backup=true + fi + if ! unzip -qo "$TMP/$name" -d "$dir"; then + return 1 + fi + mv "$dir/nvim-win64" "$dir/Neovim" + rm -rf "${TMP:?}/${name}" + elif is_osx; then + pushd "$TMP" >/dev/null || { error_msg "Could not get to $TMP" && exit 1; } + verbose_msg "Unpacking ${name}" + if [[ -f "$dir/nvim" ]]; then + verbose_msg "Backing up neovim $dir/nvim" + cp "$dir/nvim" "$HOME/.cache/nvim_backup" + has_backup=true + fi + if ! tar xzvf "$TMP/$name" && mv "${TMP}/nvim-osx64/*" "$dir/"; then + return 1 + fi + popd >/dev/null || { error_msg "Could not get out of $TMP" && exit 1; } + rm -rf "${TMP:?}/${name}" + rm -rf "${TMP:?}/nvim-osx64" + else + verbose_msg "Installing into $dir/$name" + if [[ -f "$dir/nvim" ]]; then + verbose_msg "Backing up neovim $dir/nvim" + cp "$dir/nvim" "$HOME/.cache/nvim_backup" + has_backup=true + fi + chmod u+x "$TMP/$name" && mv "$TMP/$name" "$dir/$name" + fi + + if [[ $(file -b "$exe_path" | awk '{print $1}') == 'ASCII' ]]; then + error_msg "Failed to execute neovim" + if [[ $has_backup == true ]]; then + if is_windows; then + rm -rf "$HOME/AppData/Roaming/Neovim" + status_msg "Restoring neovim $HOME/.cache/Neovim -> $HOME/AppData/Roaming/Neovim" + mv "$HOME/.cache/Neovim" "$HOME/AppData/Roaming/Neovim" + else + status_msg "Restoring neovim $HOME/.cache/nvim -> $dir/nvim" + mv "$HOME/.cache/nvim_backup" "$dir/nvim" + fi + fi + fi + + return 0 +} + +function get_libs() { + if [[ $PYTHON_LIBS -eq 1 ]]; then + # hash pip2 2> /dev/null && { status_msg "Installing python2 libs" && pip2 install --upgrade --user pynvim; } + hash pip3 2>/dev/null && { status_msg "Installing python3 libs" && pip3 install --upgrade --user pynvim; } + fi + + if [[ $RUBY_LIBS -eq 1 ]]; then + hash gem 2>/dev/null && { status_msg "Installing ruby libs" && gem install --user-install neovim; } + fi +} + if [[ $PORTABLE -eq 1 ]]; then if get_portable; then if get_libs; then @@ -535,7 +699,7 @@ fi if [[ $CLONE -eq 1 ]]; then status_msg "Cloning neovim repo" - if ! git clone --recursive https://github.com/neovim/neovim "${CLONE_LOC:-.}"; then + if ! shell_exec "git clone --recursive https://github.com/neovim/neovim \"${CLONE_LOC:-.}\""; then error_msg "Failed to clone neovim" exit 2 fi @@ -543,7 +707,7 @@ if [[ $CLONE -eq 1 ]]; then fi status_msg "Cleaning current build" -make clean +shell_exec "make clean" BUILD_TYPE="Release" @@ -564,17 +728,17 @@ fi if [[ $BRANCH != "$current_branch" ]]; then status_msg "Checking out to $BRANCH" if [[ $BRANCH =~ ^origin/ ]]; then - git branch --track "${BRANCH#origin/}" "remotes/$BRANCH" + shell_exec "git branch --track \"${BRANCH#origin/}\" \"remotes/$BRANCH\"" fi - git switch "${BRANCH#origin/}" + shell_exec "git switch \"${BRANCH#origin/}\"" fi status_msg "Building neovim" verbose_msg "Building $BUILD_TYPE on $BRANCH" -if make CMAKE_BUILD_TYPE="$BUILD_TYPE" -j; then +if shell_exec "make CMAKE_BUILD_TYPE=$BUILD_TYPE -j"; then INSTALL_DIR="${INSTALL_DIR:-$HOME/.local}" status_msg "Installing neovim into $INSTALL_DIR" - if ! make CMAKE_BUILD_TYPE="$BUILD_TYPE" CMAKE_INSTALL_PREFIX="$INSTALL_DIR" install; then + if ! shell_exec "make CMAKE_BUILD_TYPE=$BUILD_TYPE CMAKE_INSTALL_PREFIX=\"$INSTALL_DIR\" install"; then error_msg "Failed to install neovim" fi else @@ -585,6 +749,7 @@ if [[ $CLONE -eq 1 ]]; then popd >/dev/null || exit 1 fi + if [[ $ERR_COUNT -gt 0 ]]; then exit 1 fi diff --git a/git/bin/git-co b/git/bin/git-co index ef553b4..9b04a08 100755 --- a/git/bin/git-co +++ b/git/bin/git-co @@ -1,4 +1,5 @@ #!/usr/bin/env bash +# shellcheck disable=SC2317 # # -` @@ -22,6 +23,7 @@ # .` `/ VERBOSE=0 +QUIET=0 NOCOLOR=0 NOLOG=1 WARN_COUNT=0 @@ -30,6 +32,7 @@ ERR_COUNT=0 NAME="$0" NAME="${NAME##*/}" +NAME="${NAME##*-}" LOG="${NAME%%.*}.log" SCRIPT_PATH="$0" @@ -81,10 +84,12 @@ case "$SHELL_PLATFORM" in linux) if [[ -f /etc/arch-release ]]; then OS='arch' - elif [[ "$(cat /etc/issue)" == Ubuntu* ]]; then + elif [[ -f /etc/redhat-release ]] && [[ "$(cat /etc/redhat-release)" == Red\ Hat* ]]; then + OS='redhat' + elif [[ -f /etc/issue ]] && [[ "$(cat /etc/issue)" == Ubuntu* ]]; then OS='ubuntu' elif [[ -f /etc/debian_version ]] || [[ "$(cat /etc/issue)" == Debian* ]]; then - if [[ $ARCH == *\ armv7* ]]; then # Raspberry pi 3 uses armv7 cpu + if [[ $ARCH =~ armv.* ]] || [[ $ARCH == aarch64 ]]; then OS='raspbian' else OS='debian' @@ -129,7 +134,7 @@ if ! hash is_osx 2>/dev/null; then } fi -if hash is_root 2>/dev/null; then +if ! hash is_root 2>/dev/null; then function is_root() { if ! is_windows && [[ $EUID -eq 0 ]]; then return 0 @@ -138,7 +143,7 @@ if hash is_root 2>/dev/null; then } fi -if hash has_sudo 2>/dev/null; then +if ! hash has_sudo 2>/dev/null; then function has_sudo() { if ! is_windows && hash sudo 2>/dev/null && [[ "$(groups)" =~ sudo ]]; then return 0 @@ -147,6 +152,17 @@ if hash has_sudo 2>/dev/null; then } fi +if ! hash is_arm 2>/dev/null; then + function is_arm() { + local arch + arch="$(uname -m)" + if [[ $arch =~ ^arm ]] || [[ $arch =~ ^aarch ]]; then + return 0 + fi + return 1 + } +fi + if ! hash is_64bits 2>/dev/null; then function is_64bits() { local arch @@ -190,20 +206,22 @@ Usage: $NAME [OPTIONAL] Optional Flags - --nolog Disable log writing --nocolor Disable color output -v, --verbose Enable debug messages - -h, --help Display help, if you are seeing this, that means that you already know it (nice) + -q, --quiet Suppress all output but the errors + -h, --help Display this help message EOF } function warn_msg() { local msg="$1" - if [[ $NOCOLOR -eq 0 ]]; then - printf "${yellow}[!] Warning:${reset_color}\t %s\n" "$msg" - else - printf "[!] Warning:\t %s\n" "$msg" + if [[ $QUIET -eq 0 ]]; then + if [[ $NOCOLOR -eq 0 ]]; then + printf "${yellow}[!] Warning:${reset_color}\t %s\n" "$msg" + else + printf "[!] Warning:\t %s\n" "$msg" + fi fi WARN_COUNT=$((WARN_COUNT + 1)) if [[ $NOLOG -eq 0 ]]; then @@ -228,10 +246,12 @@ function error_msg() { function status_msg() { local msg="$1" - if [[ $NOCOLOR -eq 0 ]]; then - printf "${green}[*] Info:${reset_color}\t %s\n" "$msg" - else - printf "[*] Info:\t %s\n" "$msg" + if [[ $QUIET -eq 0 ]]; then + if [[ $NOCOLOR -eq 0 ]]; then + printf "${green}[*] Info:${reset_color}\t %s\n" "$msg" + else + printf "[*] Info:\t %s\n" "$msg" + fi fi if [[ $NOLOG -eq 0 ]]; then printf "[*] Info:\t\t %s\n" "$msg" >>"${LOG}" @@ -312,6 +332,38 @@ function exit_append() { return 0 } +function raw_output() { + local msg="echo \"$1\"" + if [[ $NOLOG -eq 0 ]]; then + msg="$msg | tee ${LOG}" + fi + if ! sh -c "$msg"; then + return 1 + fi + return 0 +} + +function shell_exec() { + local cmd="$1" + if [[ $VERBOSE -eq 1 ]]; then + if [[ $NOLOG -eq 0 ]]; then + cmd="$cmd | tee ${LOG}" + fi + if ! sh -c "$cmd"; then + return 1 + fi + elif [[ $NOLOG -eq 0 ]]; then + if ! sh -c "$cmd >> ${LOG}"; then + return 1 + fi + else + if ! sh -c "$cmd &>/dev/null"; then + return 1 + fi + fi + return 0 +} + while [[ $# -gt 0 ]]; do key="$1" case "$key" in @@ -324,6 +376,9 @@ while [[ $# -gt 0 ]]; do -v | --verbose) VERBOSE=1 ;; + -q | --quiet) + QUIET=1 + ;; -h | --help) help_user exit 0 @@ -351,9 +406,6 @@ verbose_msg "Platform : ${SHELL_PLATFORM}" verbose_msg "Architecture : ${ARCH}" verbose_msg "OS : ${OS}" -####################################################################### -# CODE Goes Here # -####################################################################### # TODO: Add commit log as preview checkout() { @@ -371,14 +423,16 @@ checkout() { fi local branch - branch=$( git branch --all | tr -d '*' | tr -d '+' | awk '{gsub("remotes/", "", $1) ; print $1}' | bash -c "$filter") + branch=$(git branch --all | tr -d '*' | tr -d '+' | awk '{gsub("remotes/", "", $1) ; print $1}' | bash -c "$filter") # NOTE: Assuming we just have 1 remote # remote="$(git remote show)" if [[ -n $branch ]]; then - if [[ $branch =~ ^origin/ ]]; then - git branch --track "${branch#origin/}" "remotes/$branch" + if [[ $branch =~ ^[a-zA-Z0-9_]/ ]]; then + verbose_msg "creating branch ${branch#*/}" + shell_exec "git branch --track \"${branch#*/}\" \"remotes/$branch\"" fi - git switch "${branch#origin/}" + verbose_msg "Switching to ${branch#*/}" + shell_exec "git switch \"${branch#*/}\"" fi } @@ -395,9 +449,7 @@ else exit 1 fi -####################################################################### -# CODE Goes Here # -####################################################################### + if [[ $ERR_COUNT -gt 0 ]]; then exit 1 fi diff --git a/git/bin/git-dev b/git/bin/git-dev index 3b3a6f2..9177601 100755 --- a/git/bin/git-dev +++ b/git/bin/git-dev @@ -22,6 +22,7 @@ # .` `/ VERBOSE=0 +QUIET=0 NOCOLOR=0 NOLOG=1 WARN_COUNT=0 @@ -32,6 +33,7 @@ BRANCH="" NAME="$0" NAME="${NAME##*/}" +NAME="${NAME##*-}" LOG="${NAME%%.*}.log" SCRIPT_PATH="$0" @@ -83,10 +85,12 @@ case "$SHELL_PLATFORM" in linux) if [[ -f /etc/arch-release ]]; then OS='arch' - elif [[ "$(cat /etc/issue)" == Ubuntu* ]]; then + elif [[ -f /etc/redhat-release ]] && [[ "$(cat /etc/redhat-release)" == Red\ Hat* ]]; then + OS='redhat' + elif [[ -f /etc/issue ]] && [[ "$(cat /etc/issue)" == Ubuntu* ]]; then OS='ubuntu' elif [[ -f /etc/debian_version ]] || [[ "$(cat /etc/issue)" == Debian* ]]; then - if [[ $ARCH == *\ armv7* ]]; then # Raspberry pi 3 uses armv7 cpu + if [[ $ARCH =~ armv.* ]] || [[ $ARCH == aarch64 ]]; then OS='raspbian' else OS='debian' @@ -104,7 +108,25 @@ case "$SHELL_PLATFORM" in ;; esac -if ! hash is_osx >/dev/null; then +if ! hash is_windows 2>/dev/null; then + function is_windows() { + if [[ $SHELL_PLATFORM =~ (msys|cygwin|windows) ]]; then + return 0 + fi + return 1 + } +fi + +if ! hash is_wls 2>/dev/null; then + function is_wls() { + if [[ "$(uname -r)" =~ Microsoft ]]; then + return 0 + fi + return 1 + } +fi + +if ! hash is_osx 2>/dev/null; then function is_osx() { if [[ $SHELL_PLATFORM == 'osx' ]]; then return 0 @@ -113,6 +135,46 @@ if ! hash is_osx >/dev/null; then } fi +if ! hash is_root 2>/dev/null; then + function is_root() { + if ! is_windows && [[ $EUID -eq 0 ]]; then + return 0 + fi + return 1 + } +fi + +if ! hash has_sudo 2>/dev/null; then + function has_sudo() { + if ! is_windows && hash sudo 2>/dev/null && [[ "$(groups)" =~ sudo ]]; then + return 0 + fi + return 1 + } +fi + +if ! hash is_arm 2>/dev/null; then + function is_arm() { + local arch + arch="$(uname -m)" + if [[ $arch =~ ^arm ]] || [[ $arch =~ ^aarch ]]; then + return 0 + fi + return 1 + } +fi + +if ! hash is_64bits 2>/dev/null; then + function is_64bits() { + local arch + arch="$(uname -m)" + if [[ $arch == 'x86_64' ]] || [[ $arch == 'arm64' ]]; then + return 0 + fi + return 1 + } +fi + # colors # shellcheck disable=SC2034 black="\033[0;30m" @@ -145,66 +207,70 @@ Usage: $NAME [OPTIONAL] Optional Flags - --nolog Disable log writing --nocolor Disable color output -v, --verbose Enable debug messages - -h, --help Display help, if you are seeing this, that means that you already know it (nice) + -q, --quiet Suppress all output but the errors + -h, --help Display this help message EOF } function warn_msg() { - local warn_message="$1" + local msg="$1" + if [[ $QUIET -eq 0 ]]; then if [[ $NOCOLOR -eq 0 ]]; then - printf "${yellow}[!] Warning:${reset_color}\t %s\n" "$warn_message" + printf "${yellow}[!] Warning:${reset_color}\t %s\n" "$msg" else - printf "[!] Warning:\t %s\n" "$warn_message" + printf "[!] Warning:\t %s\n" "$msg" + fi fi WARN_COUNT=$((WARN_COUNT + 1)) if [[ $NOLOG -eq 0 ]]; then - printf "[!] Warning:\t %s\n" "$warn_message" >>"${LOG}" + printf "[!] Warning:\t %s\n" "$msg" >>"${LOG}" fi return 0 } function error_msg() { - local error_message="$1" + local msg="$1" if [[ $NOCOLOR -eq 0 ]]; then - printf "${red}[X] Error:${reset_color}\t %s\n" "$error_message" 1>&2 + printf "${red}[X] Error:${reset_color}\t %s\n" "$msg" 1>&2 else - printf "[X] Error:\t %s\n" "$error_message" 1>&2 + printf "[X] Error:\t %s\n" "$msg" 1>&2 fi ERR_COUNT=$((ERR_COUNT + 1)) if [[ $NOLOG -eq 0 ]]; then - printf "[X] Error:\t %s\n" "$error_message" >>"${LOG}" + printf "[X] Error:\t %s\n" "$msg" >>"${LOG}" fi return 0 } function status_msg() { - local status_message="$1" + local msg="$1" + if [[ $QUIET -eq 0 ]]; then if [[ $NOCOLOR -eq 0 ]]; then - printf "${green}[*] Info:${reset_color}\t %s\n" "$status_message" + printf "${green}[*] Info:${reset_color}\t %s\n" "$msg" else - printf "[*] Info:\t %s\n" "$status_message" + printf "[*] Info:\t %s\n" "$msg" + fi fi if [[ $NOLOG -eq 0 ]]; then - printf "[*] Info:\t\t %s\n" "$status_message" >>"${LOG}" + printf "[*] Info:\t\t %s\n" "$msg" >>"${LOG}" fi return 0 } function verbose_msg() { - local debug_message="$1" + local msg="$1" if [[ $VERBOSE -eq 1 ]]; then if [[ $NOCOLOR -eq 0 ]]; then - printf "${purple}[+] Debug:${reset_color}\t %s\n" "$debug_message" + printf "${purple}[+] Debug:${reset_color}\t %s\n" "$msg" else - printf "[+] Debug:\t %s\n" "$debug_message" + printf "[+] Debug:\t %s\n" "$msg" fi fi if [[ $NOLOG -eq 0 ]]; then - printf "[+] Debug:\t\t %s\n" "$debug_message" >>"${LOG}" + printf "[+] Debug:\t\t %s\n" "$msg" >>"${LOG}" fi return 0 } @@ -267,15 +333,53 @@ function exit_append() { return 0 } +function raw_output() { + local msg="echo \"$1\"" + if [[ $NOLOG -eq 0 ]]; then + msg="$msg | tee ${LOG}" + fi + if ! sh -c "$msg"; then + return 1 + fi + return 0 +} + +function shell_exec() { + local cmd="$1" + if [[ $VERBOSE -eq 1 ]]; then + if [[ $NOLOG -eq 0 ]]; then + cmd="$cmd | tee ${LOG}" + fi + if ! sh -c "$cmd"; then + return 1 + fi + elif [[ $NOLOG -eq 0 ]]; then + if ! sh -c "$cmd >> ${LOG}"; then + return 1 + fi + else + if ! sh -c "$cmd &>/dev/null"; then + return 1 + fi + fi + return 0 +} + while [[ $# -gt 0 ]]; do key="$1" case "$key" in + --nolog) + NOLOG=1 + ;; --nocolor) NOCOLOR=1 ;; -v | --verbose) VERBOSE=1 ;; + -q | --quiet) + QUIET=1 + ;; -h | --help) help_user exit 0 @@ -300,16 +404,13 @@ verbose_msg "Platform : ${SHELL_PLATFORM}" verbose_msg "Architecture : ${ARCH}" verbose_msg "OS : ${OS}" -####################################################################### -# CODE Goes Here # -####################################################################### if [[ -z $BRANCH ]]; then error_msg 'missing git branch' exit 1 fi -git checkout -b "dev/$USER/$BRANCH" +shell_exec "git checkout -b \"dev/$USER/$BRANCH\"" if [[ $ERR_COUNT -gt 0 ]]; then exit 1 diff --git a/git/bin/git-edit b/git/bin/git-edit index 39afe38..a84d3d2 100755 --- a/git/bin/git-edit +++ b/git/bin/git-edit @@ -22,10 +22,17 @@ # .` `/ VERBOSE=0 +QUIET=0 +NOCOLOR=0 +NOLOG=1 +WARN_COUNT=0 +ERR_COUNT=0 +# FROM_STDIN=() NAME="$0" NAME="${NAME##*/}" NAME="${NAME##*-}" +LOG="${NAME%%.*}.log" EDITOR="${EDITOR:-vim}" @@ -34,6 +41,11 @@ WORKTREE="" SCRIPT_PATH="${SCRIPT_PATH%/*}" +OS='unknown' +ARCH="$(uname -m)" + +trap '{ exit_append; }' EXIT + if hash realpath 2>/dev/null; then SCRIPT_PATH=$(realpath "$SCRIPT_PATH") else @@ -53,10 +65,144 @@ else fi fi +if [ -z "$SHELL_PLATFORM" ]; then + if [[ -n $TRAVIS_OS_NAME ]]; then + export SHELL_PLATFORM="$TRAVIS_OS_NAME" + else + case "$OSTYPE" in + *'linux'*) export SHELL_PLATFORM='linux' ;; + *'darwin'*) export SHELL_PLATFORM='osx' ;; + *'freebsd'*) export SHELL_PLATFORM='bsd' ;; + *'cygwin'*) export SHELL_PLATFORM='cygwin' ;; + *'msys'*) export SHELL_PLATFORM='msys' ;; + *'windows'*) export SHELL_PLATFORM='windows' ;; + *) export SHELL_PLATFORM='unknown' ;; + esac + fi +fi + +case "$SHELL_PLATFORM" in + # TODO: support more linux distros + linux) + if [[ -f /etc/arch-release ]]; then + OS='arch' + elif [[ -f /etc/redhat-release ]] && [[ "$(cat /etc/redhat-release)" == Red\ Hat* ]]; then + OS='redhat' + elif [[ -f /etc/issue ]] && [[ "$(cat /etc/issue)" == Ubuntu* ]]; then + OS='ubuntu' + elif [[ -f /etc/debian_version ]] || [[ "$(cat /etc/issue)" == Debian* ]]; then + if [[ $ARCH =~ armv.* ]] || [[ $ARCH == aarch64 ]]; then + OS='raspbian' + else + OS='debian' + fi + fi + ;; + cygwin | msys | windows) + OS='windows' + ;; + osx) + OS='macos' + ;; + bsd) + OS='bsd' + ;; +esac + +if ! hash is_windows 2>/dev/null; then + function is_windows() { + if [[ $SHELL_PLATFORM =~ (msys|cygwin|windows) ]]; then + return 0 + fi + return 1 + } +fi + +if ! hash is_wls 2>/dev/null; then + function is_wls() { + if [[ "$(uname -r)" =~ Microsoft ]]; then + return 0 + fi + return 1 + } +fi + +if ! hash is_osx 2>/dev/null; then + function is_osx() { + if [[ $SHELL_PLATFORM == 'osx' ]]; then + return 0 + fi + return 1 + } +fi + +if ! hash is_root 2>/dev/null; then + function is_root() { + if ! is_windows && [[ $EUID -eq 0 ]]; then + return 0 + fi + return 1 + } +fi + +if ! hash has_sudo 2>/dev/null; then + function has_sudo() { + if ! is_windows && hash sudo 2>/dev/null && [[ "$(groups)" =~ sudo ]]; then + return 0 + fi + return 1 + } +fi + +if ! hash is_arm 2>/dev/null; then + function is_arm() { + local arch + arch="$(uname -m)" + if [[ $arch =~ ^arm ]] || [[ $arch =~ ^aarch ]]; then + return 0 + fi + return 1 + } +fi + +if ! hash is_64bits 2>/dev/null; then + function is_64bits() { + local arch + arch="$(uname -m)" + if [[ $arch == 'x86_64' ]] || [[ $arch == 'arm64' ]]; then + return 0 + fi + return 1 + } +fi + +# colors +# shellcheck disable=SC2034 +black="\033[0;30m" +# shellcheck disable=SC2034 +red="\033[0;31m" +# shellcheck disable=SC2034 +green="\033[0;32m" +# shellcheck disable=SC2034 +yellow="\033[0;33m" +# shellcheck disable=SC2034 +blue="\033[0;34m" +# shellcheck disable=SC2034 +purple="\033[0;35m" +# shellcheck disable=SC2034 +cyan="\033[0;36m" +# shellcheck disable=SC2034 +white="\033[0;37;1m" +# shellcheck disable=SC2034 +orange="\033[0;91m" +# shellcheck disable=SC2034 +normal="\033[0m" +# shellcheck disable=SC2034 +reset_color="\033[39m" + function help_user() { cat < Change the default editor + By default this uses \$EDITOR var and fallback vi in it's unset or empty + -h, --help Display this help message EOF } +function warn_msg() { + local msg="$1" + if [[ $QUIET -eq 0 ]]; then + if [[ $NOCOLOR -eq 0 ]]; then + printf "${yellow}[!] Warning:${reset_color}\t %s\n" "$msg" + else + printf "[!] Warning:\t %s\n" "$msg" + fi + fi + WARN_COUNT=$((WARN_COUNT + 1)) + if [[ $NOLOG -eq 0 ]]; then + printf "[!] Warning:\t %s\n" "$msg" >>"${LOG}" + fi + return 0 +} + +function error_msg() { + local msg="$1" + if [[ $NOCOLOR -eq 0 ]]; then + printf "${red}[X] Error:${reset_color}\t %s\n" "$msg" 1>&2 + else + printf "[X] Error:\t %s\n" "$msg" 1>&2 + fi + ERR_COUNT=$((ERR_COUNT + 1)) + if [[ $NOLOG -eq 0 ]]; then + printf "[X] Error:\t %s\n" "$msg" >>"${LOG}" + fi + return 0 +} + +function status_msg() { + local msg="$1" + if [[ $QUIET -eq 0 ]]; then + if [[ $NOCOLOR -eq 0 ]]; then + printf "${green}[*] Info:${reset_color}\t %s\n" "$msg" + else + printf "[*] Info:\t %s\n" "$msg" + fi + fi + if [[ $NOLOG -eq 0 ]]; then + printf "[*] Info:\t\t %s\n" "$msg" >>"${LOG}" + fi + return 0 +} + +function verbose_msg() { + local msg="$1" + if [[ $VERBOSE -eq 1 ]]; then + if [[ $NOCOLOR -eq 0 ]]; then + printf "${purple}[+] Debug:${reset_color}\t %s\n" "$msg" + else + printf "[+] Debug:\t %s\n" "$msg" + fi + fi + if [[ $NOLOG -eq 0 ]]; then + printf "[+] Debug:\t\t %s\n" "$msg" >>"${LOG}" + fi + return 0 +} + function __parse_args() { if [[ $# -lt 2 ]]; then - echo "" + error_msg "Internal error in __parse_args function trying to parse $1" + exit 1 fi - local arg="$1" - local name="$2" + local flag="$2" + local value="$1" - local pattern="^--${name}[=][a-zA-Z0-9._-/~]+$" + local pattern="^--${flag}=[a-zA-Z0-9.:@_/~-]+$" - if [[ -n $3 ]]; then - local pattern="^--${name}[=]$3$" + if [[ -n $3 ]]; then + local pattern="^--${flag}=$3$" fi - if [[ $arg =~ $pattern ]]; then - local left_side="${arg#*=}" + if [[ $value =~ $pattern ]]; then + local left_side="${value#*=}" echo "${left_side/#\~/$HOME}" else - echo "$arg" + echo "$value" fi } -function warn_msg() { - local warn_message="$1" - printf "[!] ---- Warning!!! %s \n" "$warn_message" +function initlog() { + if [[ $NOLOG -eq 0 ]]; then + [[ -n $LOG ]] && rm -f "${LOG}" 2>/dev/null + if ! touch "${LOG}" &>/dev/null; then + error_msg "Fail to init log file" + NOLOG=1 + return 1 + fi + if [[ -f "${SCRIPT_PATH}/shell/banner" ]]; then + cat "${SCRIPT_PATH}/shell/banner" >"${LOG}" + fi + if ! is_osx; then + LOG=$(readlink -e "${LOG}") + fi + verbose_msg "Using log at ${LOG}" + fi + return 0 } -function error_msg() { - local error_message="$1" - printf "[X] ---- Error!!! %s \n" "$error_message" 1>&2 +function exit_append() { + if [[ $NOLOG -eq 0 ]]; then + if [[ $WARN_COUNT -gt 0 ]] || [[ $ERR_COUNT -gt 0 ]]; then + printf "\n\n" >>"${LOG}" + fi + + if [[ $WARN_COUNT -gt 0 ]]; then + printf "[*] Warnings:\t%s\n" "$WARN_COUNT" >>"${LOG}" + fi + if [[ $ERR_COUNT -gt 0 ]]; then + printf "[*] Errors:\t%s\n" "$ERR_COUNT" >>"${LOG}" + fi + fi + return 0 } -function status_msg() { - local status_message="$1" - printf "[*] ---- %s \n" "$status_message" +function raw_output() { + local msg="echo \"$1\"" + if [[ $NOLOG -eq 0 ]]; then + msg="$msg | tee ${LOG}" + fi + if ! sh -c "$msg"; then + return 1 + fi + return 0 } -function verbose_msg() { +function shell_exec() { + local cmd="$1" if [[ $VERBOSE -eq 1 ]]; then - local debug_message="$1" - printf "[+] ---- Debug!!! %s \n" "$debug_message" + if [[ $NOLOG -eq 0 ]]; then + cmd="$cmd | tee ${LOG}" + fi + if ! sh -c "$cmd"; then + return 1 + fi + elif [[ $NOLOG -eq 0 ]]; then + if ! sh -c "$cmd >> ${LOG}"; then + return 1 + fi + else + if ! sh -c "$cmd &>/dev/null"; then + return 1 + fi fi + return 0 } while [[ $# -gt 0 ]]; do key="$1" case "$key" in - --verbose) + --nolog) + NOLOG=1 + ;; + --nocolor) + NOCOLOR=1 + ;; + -v | --verbose) VERBOSE=1 ;; + -q | --quiet) + QUIET=1 + ;; -h | --help) help_user exit 0 @@ -143,6 +405,7 @@ while [[ $# -gt 0 ]]; do shift ;; *) + initlog error_msg "Unknown argument $key" help_user exit 1 @@ -151,6 +414,14 @@ while [[ $# -gt 0 ]]; do shift done +initlog +verbose_msg "Log Disable : ${NOLOG}" +verbose_msg "Current Shell : ${CURRENT_SHELL}" +verbose_msg "Platform : ${SHELL_PLATFORM}" +verbose_msg "Architecture : ${ARCH}" +verbose_msg "OS : ${OS}" + + # _GITDIR="$(git rev-parse --git-dir)" IFS=$'\n' read -r -d '' -a worklist < <(git worktree list 2>/dev/null | awk '{print $1}') for i in "${worklist[@]}"; do @@ -165,9 +436,15 @@ fi # shellcheck disable=SC2009,SC2046 if [[ -n ${FILES[0]} ]]; then + verbose_msg "Files: ${FILES[*]}" "$EDITOR" "${FILES[@]}" else warn_msg "No files to edit" fi + +if [[ $ERR_COUNT -gt 0 ]]; then + exit 1 +fi + exit 0 diff --git a/git/bin/git-pla b/git/bin/git-pla index 383e621..5b63608 100755 --- a/git/bin/git-pla +++ b/git/bin/git-pla @@ -1,4 +1,5 @@ #!/usr/bin/env bash +# shellcheck disable=SC2317 # # -` @@ -22,6 +23,7 @@ # .` `/ VERBOSE=0 +QUIET=0 NOCOLOR=0 NOLOG=1 WARN_COUNT=0 @@ -30,6 +32,7 @@ ERR_COUNT=0 NAME="$0" NAME="${NAME##*/}" +NAME="${NAME##*-}" LOG="${NAME%%.*}.log" SCRIPT_PATH="$0" @@ -81,10 +84,12 @@ case "$SHELL_PLATFORM" in linux) if [[ -f /etc/arch-release ]]; then OS='arch' - elif [[ "$(cat /etc/issue)" == Ubuntu* ]]; then + elif [[ -f /etc/redhat-release ]] && [[ "$(cat /etc/redhat-release)" == Red\ Hat* ]]; then + OS='redhat' + elif [[ -f /etc/issue ]] && [[ "$(cat /etc/issue)" == Ubuntu* ]]; then OS='ubuntu' elif [[ -f /etc/debian_version ]] || [[ "$(cat /etc/issue)" == Debian* ]]; then - if [[ $ARCH == *\ armv7* ]]; then # Raspberry pi 3 uses armv7 cpu + if [[ $ARCH =~ armv.* ]] || [[ $ARCH == aarch64 ]]; then OS='raspbian' else OS='debian' @@ -102,7 +107,25 @@ case "$SHELL_PLATFORM" in ;; esac -if ! hash is_osx >/dev/null; then +if ! hash is_windows 2>/dev/null; then + function is_windows() { + if [[ $SHELL_PLATFORM =~ (msys|cygwin|windows) ]]; then + return 0 + fi + return 1 + } +fi + +if ! hash is_wls 2>/dev/null; then + function is_wls() { + if [[ "$(uname -r)" =~ Microsoft ]]; then + return 0 + fi + return 1 + } +fi + +if ! hash is_osx 2>/dev/null; then function is_osx() { if [[ $SHELL_PLATFORM == 'osx' ]]; then return 0 @@ -111,6 +134,46 @@ if ! hash is_osx >/dev/null; then } fi +if ! hash is_root 2>/dev/null; then + function is_root() { + if ! is_windows && [[ $EUID -eq 0 ]]; then + return 0 + fi + return 1 + } +fi + +if ! hash has_sudo 2>/dev/null; then + function has_sudo() { + if ! is_windows && hash sudo 2>/dev/null && [[ "$(groups)" =~ sudo ]]; then + return 0 + fi + return 1 + } +fi + +if ! hash is_arm 2>/dev/null; then + function is_arm() { + local arch + arch="$(uname -m)" + if [[ $arch =~ ^arm ]] || [[ $arch =~ ^aarch ]]; then + return 0 + fi + return 1 + } +fi + +if ! hash is_64bits 2>/dev/null; then + function is_64bits() { + local arch + arch="$(uname -m)" + if [[ $arch == 'x86_64' ]] || [[ $arch == 'arm64' ]]; then + return 0 + fi + return 1 + } +fi + # colors # shellcheck disable=SC2034 black="\033[0;30m" @@ -144,66 +207,70 @@ Usage: $NAME [OPTIONAL] Optional Flags - --nolog Disable log writing --nocolor Disable color output -v, --verbose Enable debug messages - -h, --help Display help, if you are seeing this, that means that you already know it (nice) + -q, --quiet Suppress all output but the errors + -h, --help Display this help message EOF } function warn_msg() { - local warn_message="$1" + local msg="$1" + if [[ $QUIET -eq 0 ]]; then if [[ $NOCOLOR -eq 0 ]]; then - printf "${yellow}[!] Warning:${reset_color}\t %s\n" "$warn_message" + printf "${yellow}[!] Warning:${reset_color}\t %s\n" "$msg" else - printf "[!] Warning:\t %s\n" "$warn_message" + printf "[!] Warning:\t %s\n" "$msg" + fi fi WARN_COUNT=$((WARN_COUNT + 1)) if [[ $NOLOG -eq 0 ]]; then - printf "[!] Warning:\t %s\n" "$warn_message" >>"${LOG}" + printf "[!] Warning:\t %s\n" "$msg" >>"${LOG}" fi return 0 } function error_msg() { - local error_message="$1" + local msg="$1" if [[ $NOCOLOR -eq 0 ]]; then - printf "${red}[X] Error:${reset_color}\t %s\n" "$error_message" 1>&2 + printf "${red}[X] Error:${reset_color}\t %s\n" "$msg" 1>&2 else - printf "[X] Error:\t %s\n" "$error_message" 1>&2 + printf "[X] Error:\t %s\n" "$msg" 1>&2 fi ERR_COUNT=$((ERR_COUNT + 1)) if [[ $NOLOG -eq 0 ]]; then - printf "[X] Error:\t %s\n" "$error_message" >>"${LOG}" + printf "[X] Error:\t %s\n" "$msg" >>"${LOG}" fi return 0 } function status_msg() { - local status_message="$1" + local msg="$1" + if [[ $QUIET -eq 0 ]]; then if [[ $NOCOLOR -eq 0 ]]; then - printf "${green}[*] Info:${reset_color}\t %s\n" "$status_message" + printf "${green}[*] Info:${reset_color}\t %s\n" "$msg" else - printf "[*] Info:\t %s\n" "$status_message" + printf "[*] Info:\t %s\n" "$msg" + fi fi if [[ $NOLOG -eq 0 ]]; then - printf "[*] Info:\t\t %s\n" "$status_message" >>"${LOG}" + printf "[*] Info:\t\t %s\n" "$msg" >>"${LOG}" fi return 0 } function verbose_msg() { - local debug_message="$1" + local msg="$1" if [[ $VERBOSE -eq 1 ]]; then if [[ $NOCOLOR -eq 0 ]]; then - printf "${purple}[+] Debug:${reset_color}\t %s\n" "$debug_message" + printf "${purple}[+] Debug:${reset_color}\t %s\n" "$msg" else - printf "[+] Debug:\t %s\n" "$debug_message" + printf "[+] Debug:\t %s\n" "$msg" fi fi if [[ $NOLOG -eq 0 ]]; then - printf "[+] Debug:\t\t %s\n" "$debug_message" >>"${LOG}" + printf "[+] Debug:\t\t %s\n" "$msg" >>"${LOG}" fi return 0 } @@ -266,6 +333,38 @@ function exit_append() { return 0 } +function raw_output() { + local msg="echo \"$1\"" + if [[ $NOLOG -eq 0 ]]; then + msg="$msg | tee ${LOG}" + fi + if ! sh -c "$msg"; then + return 1 + fi + return 0 +} + +function shell_exec() { + local cmd="$1" + if [[ $VERBOSE -eq 1 ]]; then + if [[ $NOLOG -eq 0 ]]; then + cmd="$cmd | tee ${LOG}" + fi + if ! sh -c "$cmd"; then + return 1 + fi + elif [[ $NOLOG -eq 0 ]]; then + if ! sh -c "$cmd >> ${LOG}"; then + return 1 + fi + else + if ! sh -c "$cmd &>/dev/null"; then + return 1 + fi + fi + return 0 +} + while [[ $# -gt 0 ]]; do key="$1" case "$key" in @@ -278,6 +377,9 @@ while [[ $# -gt 0 ]]; do -v | --verbose) VERBOSE=1 ;; + -q | --quiet) + QUIET=1 + ;; -h | --help) help_user exit 0 @@ -305,16 +407,13 @@ verbose_msg "Platform : ${SHELL_PLATFORM}" verbose_msg "Architecture : ${ARCH}" verbose_msg "OS : ${OS}" -####################################################################### -# CODE Goes Here # -####################################################################### for i in *; do if [[ -d $i ]]; then if pushd "$i" &>/dev/null; then if git rev-parse --show-toplevel &>/dev/null; then status_msg "Updating $i" - if ! git pull &>/dev/null; then + if ! shell_exec "git pull"; then error_msg "Failed to update $i" fi else @@ -325,6 +424,7 @@ for i in *; do fi done + if [[ $ERR_COUNT -gt 0 ]]; then exit 1 fi diff --git a/git/bin/git-user b/git/bin/git-user index 0e7556e..c77e29d 100755 --- a/git/bin/git-user +++ b/git/bin/git-user @@ -1,4 +1,5 @@ #!/usr/bin/env bash +# shellcheck disable=SC2317 # # -` @@ -22,6 +23,7 @@ # .` `/ VERBOSE=0 +QUIET=0 NOCOLOR=0 NOLOG=1 WARN_COUNT=0 @@ -31,6 +33,7 @@ GLOBAL=0 NAME="$0" NAME="${NAME##*/}" +NAME="${NAME##*-}" LOG="${NAME%%.*}.log" SCRIPT_PATH="$0" @@ -85,10 +88,12 @@ case "$SHELL_PLATFORM" in linux) if [[ -f /etc/arch-release ]]; then OS='arch' - elif [[ "$(cat /etc/issue)" == Ubuntu* ]]; then + elif [[ -f /etc/redhat-release ]] && [[ "$(cat /etc/redhat-release)" == Red\ Hat* ]]; then + OS='redhat' + elif [[ -f /etc/issue ]] && [[ "$(cat /etc/issue)" == Ubuntu* ]]; then OS='ubuntu' elif [[ -f /etc/debian_version ]] || [[ "$(cat /etc/issue)" == Debian* ]]; then - if [[ $ARCH == *\ armv7* ]]; then # Raspberry pi 3 uses armv7 cpu + if [[ $ARCH =~ armv.* ]] || [[ $ARCH == aarch64 ]]; then OS='raspbian' else OS='debian' @@ -106,7 +111,25 @@ case "$SHELL_PLATFORM" in ;; esac -if ! hash is_osx >/dev/null; then +if ! hash is_windows 2>/dev/null; then + function is_windows() { + if [[ $SHELL_PLATFORM =~ (msys|cygwin|windows) ]]; then + return 0 + fi + return 1 + } +fi + +if ! hash is_wls 2>/dev/null; then + function is_wls() { + if [[ "$(uname -r)" =~ Microsoft ]]; then + return 0 + fi + return 1 + } +fi + +if ! hash is_osx 2>/dev/null; then function is_osx() { if [[ $SHELL_PLATFORM == 'osx' ]]; then return 0 @@ -115,6 +138,46 @@ if ! hash is_osx >/dev/null; then } fi +if ! hash is_root 2>/dev/null; then + function is_root() { + if ! is_windows && [[ $EUID -eq 0 ]]; then + return 0 + fi + return 1 + } +fi + +if ! hash has_sudo 2>/dev/null; then + function has_sudo() { + if ! is_windows && hash sudo 2>/dev/null && [[ "$(groups)" =~ sudo ]]; then + return 0 + fi + return 1 + } +fi + +if ! hash is_arm 2>/dev/null; then + function is_arm() { + local arch + arch="$(uname -m)" + if [[ $arch =~ ^arm ]] || [[ $arch =~ ^aarch ]]; then + return 0 + fi + return 1 + } +fi + +if ! hash is_64bits 2>/dev/null; then + function is_64bits() { + local arch + arch="$(uname -m)" + if [[ $arch == 'x86_64' ]] || [[ $arch == 'arm64' ]]; then + return 0 + fi + return 1 + } +fi + # colors # shellcheck disable=SC2034 black="\033[0;30m" @@ -147,66 +210,70 @@ Usage: $NAME [OPTIONAL] Optional Flags - --nolog Disable log writing --nocolor Disable color output -v, --verbose Enable debug messages - -h, --help Display help, if you are seeing this, that means that you already know it (nice) + -q, --quiet Suppress all output but the errors + -h, --help Display this help message EOF } function warn_msg() { - local warn_message="$1" + local msg="$1" + if [[ $QUIET -eq 0 ]]; then if [[ $NOCOLOR -eq 0 ]]; then - printf "${yellow}[!] Warning:${reset_color}\t %s\n" "$warn_message" + printf "${yellow}[!] Warning:${reset_color}\t %s\n" "$msg" else - printf "[!] Warning:\t %s\n" "$warn_message" + printf "[!] Warning:\t %s\n" "$msg" + fi fi WARN_COUNT=$((WARN_COUNT + 1)) if [[ $NOLOG -eq 0 ]]; then - printf "[!] Warning:\t %s\n" "$warn_message" >>"${LOG}" + printf "[!] Warning:\t %s\n" "$msg" >>"${LOG}" fi return 0 } function error_msg() { - local error_message="$1" + local msg="$1" if [[ $NOCOLOR -eq 0 ]]; then - printf "${red}[X] Error:${reset_color}\t %s\n" "$error_message" 1>&2 + printf "${red}[X] Error:${reset_color}\t %s\n" "$msg" 1>&2 else - printf "[X] Error:\t %s\n" "$error_message" 1>&2 + printf "[X] Error:\t %s\n" "$msg" 1>&2 fi ERR_COUNT=$((ERR_COUNT + 1)) if [[ $NOLOG -eq 0 ]]; then - printf "[X] Error:\t %s\n" "$error_message" >>"${LOG}" + printf "[X] Error:\t %s\n" "$msg" >>"${LOG}" fi return 0 } function status_msg() { - local status_message="$1" + local msg="$1" + if [[ $QUIET -eq 0 ]]; then if [[ $NOCOLOR -eq 0 ]]; then - printf "${green}[*] Info:${reset_color}\t %s\n" "$status_message" + printf "${green}[*] Info:${reset_color}\t %s\n" "$msg" else - printf "[*] Info:\t %s\n" "$status_message" + printf "[*] Info:\t %s\n" "$msg" + fi fi if [[ $NOLOG -eq 0 ]]; then - printf "[*] Info:\t\t %s\n" "$status_message" >>"${LOG}" + printf "[*] Info:\t\t %s\n" "$msg" >>"${LOG}" fi return 0 } function verbose_msg() { - local debug_message="$1" + local msg="$1" if [[ $VERBOSE -eq 1 ]]; then if [[ $NOCOLOR -eq 0 ]]; then - printf "${purple}[+] Debug:${reset_color}\t %s\n" "$debug_message" + printf "${purple}[+] Debug:${reset_color}\t %s\n" "$msg" else - printf "[+] Debug:\t %s\n" "$debug_message" + printf "[+] Debug:\t %s\n" "$msg" fi fi if [[ $NOLOG -eq 0 ]]; then - printf "[+] Debug:\t\t %s\n" "$debug_message" >>"${LOG}" + printf "[+] Debug:\t\t %s\n" "$msg" >>"${LOG}" fi return 0 } @@ -269,15 +336,53 @@ function exit_append() { return 0 } +function raw_output() { + local msg="echo \"$1\"" + if [[ $NOLOG -eq 0 ]]; then + msg="$msg | tee ${LOG}" + fi + if ! sh -c "$msg"; then + return 1 + fi + return 0 +} + +function shell_exec() { + local cmd="$1" + if [[ $VERBOSE -eq 1 ]]; then + if [[ $NOLOG -eq 0 ]]; then + cmd="$cmd | tee ${LOG}" + fi + if ! sh -c "$cmd"; then + return 1 + fi + elif [[ $NOLOG -eq 0 ]]; then + if ! sh -c "$cmd >> ${LOG}"; then + return 1 + fi + else + if ! sh -c "$cmd &>/dev/null"; then + return 1 + fi + fi + return 0 +} + while [[ $# -gt 0 ]]; do key="$1" case "$key" in + --nolog) + NOLOG=1 + ;; --nocolor) NOCOLOR=1 ;; -v | --verbose) VERBOSE=1 ;; + -q | --quiet) + QUIET=1 + ;; -h | --help) help_user exit 0 @@ -319,9 +424,6 @@ verbose_msg "Platform : ${SHELL_PLATFORM}" verbose_msg "Architecture : ${ARCH}" verbose_msg "OS : ${OS}" -####################################################################### -# CODE Goes Here # -####################################################################### if ! hash git 2>/dev/null; then error_msg "Missig git" @@ -335,13 +437,14 @@ verbose_msg "Using User: $USER" verbose_msg "Using Mail: $EMAIL" if [[ $GLOBAL -eq 0 ]]; then - git config user.name "$USER" - git config user.email "$EMAIL" + shell_exec "git config user.name \"$USER\"" + shell_exec "git config user.email \"$EMAIL\"" else - git config --global user.name "$USER" - git config --global user.email "$EMAIL" + shell_exec "git config --global user.name \"$USER\"" + shell_exec "git config --global user.email \"$EMAIL\"" fi + if [[ $ERR_COUNT -gt 0 ]]; then exit 1 fi diff --git a/git/bin/git-wt b/git/bin/git-wt index 4667d2a..59a7e6c 100755 --- a/git/bin/git-wt +++ b/git/bin/git-wt @@ -1,4 +1,5 @@ #!/usr/bin/env bash +# shellcheck disable=SC2317 # # -` @@ -22,6 +23,7 @@ # .` `/ VERBOSE=0 +QUIET=0 NOCOLOR=0 NOLOG=1 WARN_COUNT=0 @@ -32,6 +34,7 @@ LIST=0 NAME="$0" NAME="${NAME##*/}" +NAME="${NAME##*-}" LOG="${NAME%%.*}.log" SCRIPT_PATH="$0" @@ -83,10 +86,12 @@ case "$SHELL_PLATFORM" in linux) if [[ -f /etc/arch-release ]]; then OS='arch' - elif [[ "$(cat /etc/issue)" == Ubuntu* ]]; then + elif [[ -f /etc/redhat-release ]] && [[ "$(cat /etc/redhat-release)" == Red\ Hat* ]]; then + OS='redhat' + elif [[ -f /etc/issue ]] && [[ "$(cat /etc/issue)" == Ubuntu* ]]; then OS='ubuntu' elif [[ -f /etc/debian_version ]] || [[ "$(cat /etc/issue)" == Debian* ]]; then - if [[ $ARCH == *\ armv7* ]]; then # Raspberry pi 3 uses armv7 cpu + if [[ $ARCH =~ armv.* ]] || [[ $ARCH == aarch64 ]]; then OS='raspbian' else OS='debian' @@ -131,7 +136,7 @@ if ! hash is_osx 2>/dev/null; then } fi -if hash is_root 2>/dev/null; then +if ! hash is_root 2>/dev/null; then function is_root() { if ! is_windows && [[ $EUID -eq 0 ]]; then return 0 @@ -140,7 +145,7 @@ if hash is_root 2>/dev/null; then } fi -if hash has_sudo 2>/dev/null; then +if ! hash has_sudo 2>/dev/null; then function has_sudo() { if ! is_windows && hash sudo 2>/dev/null && [[ "$(groups)" =~ sudo ]]; then return 0 @@ -149,6 +154,17 @@ if hash has_sudo 2>/dev/null; then } fi +if ! hash is_arm 2>/dev/null; then + function is_arm() { + local arch + arch="$(uname -m)" + if [[ $arch =~ ^arm ]] || [[ $arch =~ ^aarch ]]; then + return 0 + fi + return 1 + } +fi + if ! hash is_64bits 2>/dev/null; then function is_64bits() { local arch @@ -192,22 +208,24 @@ Usage: $NAME [OPTIONAL] Optional Flags - --nolog Disable log writing --nocolor Disable color output -r, --remove Remove the selected worktree -l, --list List all available worktrees -v, --verbose Enable debug messages - -h, --help Display help, if you are seeing this, that means that you already know it (nice) + -q, --quiet Suppress all output but the errors + -h, --help Display this help message EOF } function warn_msg() { local msg="$1" - if [[ $NOCOLOR -eq 0 ]]; then - printf "${yellow}[!] Warning:${reset_color}\t %s\n" "$msg" - else - printf "[!] Warning:\t %s\n" "$msg" + if [[ $QUIET -eq 0 ]]; then + if [[ $NOCOLOR -eq 0 ]]; then + printf "${yellow}[!] Warning:${reset_color}\t %s\n" "$msg" + else + printf "[!] Warning:\t %s\n" "$msg" + fi fi WARN_COUNT=$((WARN_COUNT + 1)) if [[ $NOLOG -eq 0 ]]; then @@ -232,10 +250,12 @@ function error_msg() { function status_msg() { local msg="$1" - if [[ $NOCOLOR -eq 0 ]]; then - printf "${green}[*] Info:${reset_color}\t %s\n" "$msg" - else - printf "[*] Info:\t %s\n" "$msg" + if [[ $QUIET -eq 0 ]]; then + if [[ $NOCOLOR -eq 0 ]]; then + printf "${green}[*] Info:${reset_color}\t %s\n" "$msg" + else + printf "[*] Info:\t %s\n" "$msg" + fi fi if [[ $NOLOG -eq 0 ]]; then printf "[*] Info:\t\t %s\n" "$msg" >>"${LOG}" @@ -316,6 +336,38 @@ function exit_append() { return 0 } +function raw_output() { + local msg="echo \"$1\"" + if [[ $NOLOG -eq 0 ]]; then + msg="$msg | tee ${LOG}" + fi + if ! sh -c "$msg"; then + return 1 + fi + return 0 +} + +function shell_exec() { + local cmd="$1" + if [[ $VERBOSE -eq 1 ]]; then + if [[ $NOLOG -eq 0 ]]; then + cmd="$cmd | tee ${LOG}" + fi + if ! sh -c "$cmd"; then + return 1 + fi + elif [[ $NOLOG -eq 0 ]]; then + if ! sh -c "$cmd >> ${LOG}"; then + return 1 + fi + else + if ! sh -c "$cmd &>/dev/null"; then + return 1 + fi + fi + return 0 +} + while [[ $# -gt 0 ]]; do key="$1" case "$key" in @@ -328,6 +380,9 @@ while [[ $# -gt 0 ]]; do -v | --verbose) VERBOSE=1 ;; + -q | --quiet) + QUIET=1 + ;; -h | --help) help_user exit 0 @@ -364,9 +419,6 @@ verbose_msg "Platform : ${SHELL_PLATFORM}" verbose_msg "Architecture : ${ARCH}" verbose_msg "OS : ${OS}" -####################################################################### -# CODE Goes Here # -####################################################################### gum_filter='gum filter --fuzzy --height=60 --width=0 --limit=1 --header "Select worktree"' fzf_filter='fzf --select-1 --exit-0 --height=60% ' @@ -382,13 +434,13 @@ else fi if [[ $LIST -eq 1 ]]; then - git worktree list + raw_output "$(git worktree list)" elif [[ $REMOVE -eq 1 ]]; then wt="$(git worktree list | bash -c "$filter" | awk '{print $1}')" if [[ -n $wt ]]; then status_msg "Removing worktree ${wt##*/}" if gum confirm "Remove worktree: ${wt##*/}"; then - if ! git worktree remove "$wt"; then + if ! shell_exec "git worktree remove \"$wt\""; then error_msg "Failed to remove worktree" exit 1 fi @@ -403,7 +455,7 @@ else if [[ $branch =~ ^origin/* ]]; then if ! git branch --all | tr -d '*' | awk '{gsub("remotes/", "", $1) ; print $1}' | command grep -q -E "^${branch#origin/}"; then status_msg "Creating local branch from remote $branch" - if ! git branch --track "${branch#origin/}" "remotes/$branch"; then + if ! shell_exec "git branch --track \"${branch#origin/}\" \"remotes/$branch\""; then error_msg "Failed to create local branch" exit 1 fi @@ -416,7 +468,7 @@ else branch_dir="${branch_dir#"$i"/}" done verbose_msg "Dir location: ${branch_dir} set to branch $branch" - if ! git worktree add "./../${branch_dir}" "$branch"; then + if ! shell_exec "git worktree add \"./../${branch_dir}\" \"$branch\""; then error_msg "failed to create worktree" exit 1 fi @@ -445,18 +497,16 @@ else for i in "${project_files[@]}"; do if [[ -f "$git_dir/../../$i" ]]; then verbose_msg "linking $i" - ln "$git_dir/../../$i" ./ + shell_exec "ln \"$git_dir/../../$i\" ./" fi done # TODO: Make this dynamic and create it with just C/C++ repos - [ ! -d ./.cache/clangd/pchs ] && mkdir -p './.cache/clangd/pchs/' + [ ! -d ./.cache/clangd/pchs ] && shell_exec "mkdir -p './.cache/clangd/pchs/'" fi fi -####################################################################### -# CODE Goes Here # -####################################################################### + if [[ $ERR_COUNT -gt 0 ]]; then exit 1 fi