From 600bdd75381125e828295da14fa4e48c39552f97 Mon Sep 17 00:00:00 2001 From: Christopher Beckmann Date: Thu, 25 May 2023 14:53:07 +0200 Subject: [PATCH] Cleanup :) --- kfg | 140 +++++++++++++++++++++++++++++++----------------------------- 1 file changed, 72 insertions(+), 68 deletions(-) diff --git a/kfg b/kfg index ca6af46..aba6df1 100644 --- a/kfg +++ b/kfg @@ -1,7 +1,7 @@ #!/bin/bash NAME="kfg" -VERSION="0.5" +VERSION="0.6" HOME_PATH="$HOME" TMP_PATH="$HOME_PATH/.tmp/kfg_install" @@ -44,7 +44,7 @@ function show_help_opt { echo -en "opt := { ${INFO}-h${ENDCOLOR} | ${INFO}--help${ENDCOLOR} \t\t Show this help, ${INFO}-y${ENDCOLOR} | ${INFO}--yes${ENDCOLOR} \t\t Skip confirmation questions, - ${INFO}-a${ENDCOLOR} | ${INFO}--arch STRING${ENDCOLOR} \t Set the architecture string to be used when downloading files from repositories. Most common are "amd64", "arm64", "armhf", etc, + ${INFO}-a${ENDCOLOR} | ${INFO}--arch STRING${ENDCOLOR} \t Set the architecture string to be used when downloading files from repositories. Most common are \"amd64\", \"arm64\", \"armhf\", etc, ${INFO}-d${ENDCOLOR} | ${INFO}--debug${ENDCOLOR}\t\t Print debug output, }\n" } @@ -62,20 +62,20 @@ function version { } function log { - color $1 "[$2]" + color "$1" "[$2]" echo "$3" } function log_add { - log $ADD "+" "$1" + log "$ADD" "+" "$1" } function log_info { - log $INFO "*" "$1" + log "$INFO" "*" "$1" } function log_error { - echo "$(log $ERROR "!" "$1")" >&2 + log "$ERROR" "!" "$1" >&2 } function err { @@ -85,11 +85,11 @@ function err { function log_debug { if [ -z "$P_Debug" ]; then return 0; fi - log $INFO "&" "$1" + log "$INFO" "&" "$1" } function ask { - color $ASK "[?]" + color "$ASK" "[?]" read -d '' -p "$1 [Y/n] " -N 1 -r } @@ -98,9 +98,9 @@ function color { } function user_confirm { - if [ ! -z "$P_Yes" ]; then return 0; fi + if [ -n "$P_Yes" ]; then return 0; fi ask "$1" - while [ true ]; do + while true; do if [[ ! $REPLY =~ ^[$'\n']$ ]]; then echo -e -n "\n"; fi case $REPLY in [Yy$'\n']* ) return 0;; @@ -120,7 +120,7 @@ function download_config { mkdir -p "$CONFIG_PATH/$1/" contains_path="${2%*/*}" - if [ -z $contains_path ]; then + if [ -z "$contains_path" ]; then mkdir -p "$CONFIG_PATH/$1/$contains_path/" fi @@ -191,7 +191,7 @@ function download { need_cmd 'curl or wget' fi - if [ ! -z $_path ]; then + if [ -n "$_path" ]; then mkdir -p "$(dirname "$_path")" log_info "Downloading $_url to $_path" @@ -242,16 +242,17 @@ function github_install { #log_debug "Packages: ${_package_urls}" # Check for Rust package - local _rust_package_url="$(echo "$_package_urls" | grep $_arch)" - local _other_package_url="$(echo "$_package_urls" | grep $ARCH | grep -vE "$APP_FILTER")" + local _rust_package_url _other_package_url + _rust_package_url="$(echo "$_package_urls" | grep "$_arch")" + _other_package_url="$(echo "$_package_urls" | grep "$ARCH" | grep -vE "$APP_FILTER")" log_debug "Rust package: $_rust_package_url" log_debug "Filter: $APP_FILTER" log_debug "Other package: $_other_package_url" - if [ ! -z "$_other_package_url" ]; then + if [ -n "$_other_package_url" ]; then _package_url="${_other_package_url}" - elif [ ! -z "$_rust_package_url" ]; then + elif [ -n "$_rust_package_url" ]; then _package_url="${_rust_package_url}" else log_error "Can not find installable package for '$_program' for the detected architecture (${_arch})." @@ -279,7 +280,7 @@ function github_install { download "$_package_url" "$_package" # unpack - cd "${TMP_PATH}" + cd "${TMP_PATH}" || (log_error "Could not unpack ${_package}. The directory ${TMP_PATH} does not exist" && return 1) case "${_package}" in *.tar.gz) need_cmd tar @@ -299,7 +300,7 @@ function github_install { case "${_ext}" in tar.gz) _binary="$(tar --list -f "${_package}" | grep -E "^(.*/)?$_program$")" - log_debug $_binary + log_debug "$_binary" install_binary "$_program" "$TMP_PATH/$_binary" return 0 ;; @@ -309,13 +310,13 @@ function github_install { ;; zip) _binary="$(unzip -l "${_package}" | sed -E 's/ {1,}/ /g' | cut -d ' ' -f 5 | grep -E "^(.*/)?$_program$")" - log_debug $_binary + log_debug "$_binary" install_binary "$_program" "$TMP_PATH/$_binary" return 0 ;; *) _binary="${_program}" - log_debug $_binary + log_debug "$_binary" log_debug "Installing binary!" install_binary "$_program" "$TMP_PATH/$_binary" ;; @@ -325,10 +326,10 @@ function github_install { function install_deb_check { CHECK_CURRENT=$(dpkg -s "$1" | grep -E "Status|Version") - INSTALLED_CURRENT=$(echo $CHECK_CURRENT | grep -oE "Status: .*" | cut -d ' ' -f 2) + #INSTALLED_CURRENT=$(echo $CHECK_CURRENT | grep -oE "Status: .*" | cut -d ' ' -f 2) VERSION_CURRENT=$(echo $CHECK_CURRENT | grep -oE "Version: .*" | cut -d ' ' -f 2) CHECK_NEW=$(sudo dpkg -I $2 | grep -E "Status|Version") - INSTALLED_NEW=$(echo $CHECK_NEW | grep -oE "Status: .*" | cut -d ' ' -f 2) + #INSTALLED_NEW=$(echo $CHECK_NEW | grep -oE "Status: .*" | cut -d ' ' -f 2) VERSION_NEW=$(echo $CHECK_NEW | grep -oE "Version: .*" | cut -d ' ' -f 2) if [ ! -z "$CHECK_CURRENT" ]; then @@ -356,14 +357,14 @@ function install_deb { return 1 fi - INSTALL=$(sudo dpkg -i $_file) + INSTALL=$(sudo dpkg -i "$_file") UNPACK=$(echo -n "$INSTALL" | grep -oE "Unpacking .*") - PROG=$(echo -n $UNPACK | cut -d '(' -f 1 | cut -d ' ' -f 2) - CURR_VERSION=$(echo -n $UNPACK | cut -d '(' -f 2 | cut -d ')' -f 1) - PREV_VERSION=$(echo -n $UNPACK | cut -d '(' -f 3 | cut -d ')' -f 1) - color $ADD "[+]" + PROG=$(echo -n "$UNPACK" | cut -d '(' -f 1 | cut -d ' ' -f 2) + CURR_VERSION=$(echo -n "$UNPACK" | cut -d '(' -f 2 | cut -d ')' -f 1) + PREV_VERSION=$(echo -n "$UNPACK" | cut -d '(' -f 3 | cut -d ')' -f 1) + color "$ADD" "[+]" echo -n "Installed $PROG ($CURR_VERSION)" - if [ ! -z ${PREV_VERSION+x} ]; then + if [ -n "${PREV_VERSION+x}" ]; then echo " over ($PREV_VERSION)" else echo "" @@ -380,16 +381,16 @@ function init { # exit 0 # fi #fi - if [ ! -z "$P_APT" ]; then + if [ -n "$P_APT" ]; then log_info "Running apt update ..." sudo apt update > /dev/null 2>&1 fi - mkdir -p $TMP_PATH/ - mkdir -p $CONFIG_PATH/ + mkdir -p "$TMP_PATH/" + mkdir -p "$CONFIG_PATH/" } function cleanup { - rm -R $TMP_PATH + rm -R "$TMP_PATH" } function apt_install { @@ -404,7 +405,7 @@ function apt_install { function github_latest { VERSION=$(curl -I "https://github.com/$1/releases/latest/" 2> /dev/null | grep -iE "^Location:" | tr -s ' ' | cut -d '/' -f 8 2> /dev/null) - echo -n ${VERSION//[$'\t\r\n']} 2> /dev/null + echo -n "${VERSION//[$'\t\r\n']}" 2> /dev/null } function git_clone { @@ -412,7 +413,7 @@ function git_clone { log_debug "${CLONE}" if [[ $CLONE =~ "already exists" ]] || [[ $CLONE =~ "existiert bereits" ]]; then log_debug "$1 already exists. Running 'git pull'." - PULL=$(git -C "$2" pull) + git -C "$2" pull > /dev/null 2>&1 log_info "Updated $1 in $2" else log_add "Cloned $1 into $2" @@ -425,10 +426,10 @@ function install_binary { if [ -z "$P_BINARY" ]; then return 0; fi #log_info "Downloading $2 to $BINARY_PATH ..." #sudo wget -O $BINARY_PATH/$1 $2 > /dev/null 2>&1 - log_debug $_path - log_debug $BINARY_PATH/$_app - sudo cp $_path $BINARY_PATH/$_app - sudo chmod +x $BINARY_PATH/$_app + log_debug "$_path" + log_debug "$BINARY_PATH/$_app" + sudo cp "$_path" "$BINARY_PATH/$_app" + sudo chmod +x "$BINARY_PATH/$_app" log_add "Installed $_app into $BINARY_PATH/$_app" } @@ -436,10 +437,10 @@ function set_shell { if [ -z "$P_ZSH" ]; then return 0; fi CURR_U=$(grep "^$USER" /etc/passwd | cut -d ':' -f 7 | cut -d '/' -f 4) CURR_R=$(grep "^root" /etc/passwd | cut -d ':' -f 7 | cut -d '/' -f 4) - if [[ $1 != $CURR_U ]]; then + if [[ $1 != "$CURR_U" ]]; then log_add "Setting default shell to $1" - sudo -u $USER chsh -s $(which $1) - sudo chsh -s $(which $1) + sudo -u "$USER" chsh -s "$(which "$1")" + sudo chsh -s "$(which "$1")" else log_info "Shell already set to $1" fi @@ -448,7 +449,7 @@ function set_shell { # -------------------------------------------------------------------------------------------------- function update { - cd "$(dirname "$0")" + cd "$(dirname "$0")" || (log_error "Could not change directory to $(dirname "$0")." && exit 1) download "https://git.cbeck.tech/kamu/dotfiles/raw/branch/main/kfg" "kfg" exit 0 } @@ -458,8 +459,8 @@ function show_help_install { echo -e "Usage: $NAME install [ opt ]\n" echo -e "program := {" local _length _space - for key in ${!TOOLS[@]}; do - _length="$(echo -n $key | wc -m)" + for key in "${!TOOLS[@]}"; do + _length="$(echo -n "$key" | wc -m)" if [ "$_length" -gt 21 ]; then _space="" elif [ "$_length" -gt 13 ]; then @@ -510,7 +511,7 @@ function p_tmux { download_config tmux tmux.conf https://git.cbeck.tech/kamu/dotfiles/raw/branch/main/.tmux.conf download_config tmux tmux.theme https://git.cbeck.tech/kamu/dotfiles/raw/branch/main/.tmux.theme download_config tmux myip.sh https://git.cbeck.tech/kamu/dotfiles/raw/branch/main/myip.sh - sudo chmod +x ${CONFIG_PATH}/tmux/myip.sh + sudo chmod +x "${CONFIG_PATH}/tmux/myip.sh" install_rc_download tmux https://git.cbeck.tech/kamu/dotfiles/raw/branch/main/.tmux.rc git_clone "https://github.com/tmux-plugins/tpm" "${CONFIG_PATH}/tmux/plugins/tpm" } @@ -566,13 +567,17 @@ function p_fzf { github_install "fzf" "${_repo}" # Install latest release version of fzf-tmux - local _latest_version="$( github_latest ${_repo})" - local _path_fzf_tmux="$TMP_PATH/fzf-tmux" + local _latest_version _path_fzf_tmux + _latest_version="$( github_latest ${_repo})" + _path_fzf_tmux="$TMP_PATH/fzf-tmux" download "https://raw.githubusercontent.com/${_repo}/${_latest_version}/bin/fzf-tmux" "${_path_fzf_tmux}" install_binary "fzf-tmux" "${_path_fzf_tmux}" # download autocomplete - if [ ! -z $ALL ] || user_confirm "Do you want to install fzf-tab as well?"; then + + + # install fzf tab + if [ -n "$ALL" ] || user_confirm "Do you want to install fzf-tab as well?"; then p_fzf_tab fi @@ -592,9 +597,8 @@ function p_fzf_tab { } function install { - if [ -z $1 ]; then + if [ -z "$1" ]; then show_help_install - exit 0 fi init @@ -611,7 +615,7 @@ function install { while : do case "$1" in - help) show_help_install; exit 0 ;; + help) show_help_install ;; "") break ;; all) p_all; shift ;; zsh) p_zsh; shift ;; @@ -634,7 +638,7 @@ function install { } function main { - if [ -z $1 ]; then + if [ -z "$1" ]; then show_help fi @@ -669,9 +673,9 @@ function main { case "$1" in update) update ;; version) version ;; - install) shift; install $@;; + install) shift; install "$@";; # -- means the end of the arguments; drop this, and break out of the while loop - --) shift; break ;; + --) shift; return ;; # If invalid options were passed, then getopt should have reported an error, # which we checked as VALID_ARGUMENTS when getopt was called... *) log_error "Unexpected command: $1"; show_help ;; @@ -693,9 +697,9 @@ function get_architecture { if ldd --version 2>&1 | grep -q 'musl'; then _clibtype="musl" - APP_FILTER=$(echo $APP_FILTER | sed -e "s/musl|//gi") + APP_FILTER="${APP_FILTER//musl|/}" else - APP_FILTER=$(echo $APP_FILTER | sed -e "s/gnu|//gi") + APP_FILTER="${APP_FILTER//gnu|/}" fi fi @@ -727,21 +731,21 @@ function get_architecture { case "${_ostype}" in Android) _ostype=linux-android - APP_FILTER=$(echo $APP_FILTER | sed -e "s/android|//gi") + APP_FILTER="${APP_FILTER//android|/}" ;; Linux) check_proc _ostype=unknown-linux-${_clibtype} _bitness=$(get_bitness) - APP_FILTER=$(echo $APP_FILTER | sed -e "s/linux|//gi") + APP_FILTER="${APP_FILTER//linux|/}" ;; FreeBSD) _ostype=unknown-freebsd - APP_FILTER=$(echo $APP_FILTER | sed -e "s/freebsd|//gi") + APP_FILTER="${APP_FILTER//freebsd|/}" ;; OpenBSD) _ostype=unknown-freebsd - APP_FILTER=$(echo $APP_FILTER | sed -e "s/openbsd|//gi") + APP_FILTER="${APP_FILTER//openbsd|/}" ;; NetBSD) _ostype=unknown-netbsd @@ -751,7 +755,7 @@ function get_architecture { ;; Darwin) _ostype=apple-darwin - APP_FILTER=$(echo $APP_FILTER | sed -e "s/darwin|//gi") + APP_FILTER="${APP_FILTER//darwin|/}" ;; illumos) _ostype=unknown-illumos @@ -767,7 +771,7 @@ function get_architecture { case "${_cputype}" in i386 | i486 | i686 | i786 | x86) _cputype=i686 - APP_FILTER=$(echo $APP_FILTER | sed -e "s/i686|//gi") + APP_FILTER="${APP_FILTER//i686|/}" ;; xscale | arm) _cputype=arm @@ -793,15 +797,15 @@ function get_architecture { ;; aarch64 | arm64) _cputype=aarch64 - APP_FILTER=$(echo $APP_FILTER | sed -e "s/aarch64|//gi") - APP_FILTER=$(echo $APP_FILTER | sed -e "s/arm64|//gi") + APP_FILTER="${APP_FILTER//aarch64|/}" + APP_FILTER="${APP_FILTER//arm64|/}" ;; x86_64 | x86-64 | x64 | amd64) _cputype=x86_64 - APP_FILTER=$(echo $APP_FILTER | sed -e "s/amd64|//gi") - APP_FILTER=$(echo $APP_FILTER | sed -e "s/x86_64|//gi") - APP_FILTER=$(echo $APP_FILTER | sed -e "s/x86-64|//gi") - APP_FILTER=$(echo $APP_FILTER | sed -e "s/x64|//gi") + APP_FILTER="${APP_FILTER//amd64|/}" + APP_FILTER="${APP_FILTER//x86_64|/}" + APP_FILTER="${APP_FILTER//x86-64|/}" + APP_FILTER="${APP_FILTER//x64|/}" ;; mips) _cputype=$(get_endianness mips '' el)