refactor(kiauh.sh): clean up kiauh.sh

Signed-off-by: Dominik Willner th33xitus@gmail.com
This commit is contained in:
th33xitus
2022-04-02 23:25:07 +02:00
parent 52cb40ac89
commit d1cb24d725
3 changed files with 149 additions and 125 deletions

113
kiauh.sh
View File

@@ -12,32 +12,11 @@
set -e set -e
clear clear
### set color variables
green=$(echo -en "\e[92m")
yellow=$(echo -en "\e[93m")
red=$(echo -en "\e[91m")
cyan=$(echo -en "\e[96m")
default=$(echo -en "\e[39m")
white=$(echo -en "\e[39m")
### sourcing all additional scripts ### sourcing all additional scripts
SRCDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )"/.. && pwd )" SRCDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )"/.. && pwd )"
for script in "${SRCDIR}/kiauh/scripts/"*.sh; do . "${script}"; done for script in "${SRCDIR}/kiauh/scripts/"*.sh; do . "${script}"; done
for script in "${SRCDIR}/kiauh/scripts/ui/"*.sh; do . "${script}"; done for script in "${SRCDIR}/kiauh/scripts/ui/"*.sh; do . "${script}"; done
#nginx
NGINX_SA=/etc/nginx/sites-available
NGINX_SE=/etc/nginx/sites-enabled
NGINX_CONFD=/etc/nginx/conf.d
#misc
INI_FILE=${HOME}/.kiauh.ini
### set github repos
DMBUTYUGIN_REPO=https://github.com/dmbutyugin/klipper.git
#branches
BRANCH_SCURVE_SMOOTHING=dmbutyugin/scurve-smoothing
BRANCH_SCURVE_SHAPING=dmbutyugin/scurve-shaping
#===================================================# #===================================================#
#=================== UPDATE KIAUH ==================# #=================== UPDATE KIAUH ==================#
#===================================================# #===================================================#
@@ -70,77 +49,39 @@ function kiauh_update_avail(){
} }
### format some default message types ### format some default message types
select_msg() {
echo -e "${white}>>>>>> $1"
}
warn_msg(){
echo -e "${red}>>>>>> $1${white}"
}
status_msg(){
echo; echo -e "${yellow}###### $1${white}"
}
ok_msg(){
echo -e "${green}>>>>>> $1${white}"
}
error_msg(){
echo -e "${red}>>>>>> $1${white}"
}
abort_msg(){
echo -e "${red}<<<<<< $1${white}"
}
title_msg(){
echo -e "${cyan}$1${white}"
}
get_date(){ get_date(){
current_date=$(date +"%y%m%d-%H%M") current_date=$(date +"%y%m%d-%H%M")
export current_date export current_date
} }
print_unkown_cmd(){
ERROR_MSG="Invalid command!"
}
invalid_option(){
ERROR_MSG="Invalid command!"
}
print_msg(){ #print_unkown_cmd(){
if [ -n "${ERROR_MSG}" ]; then # ERROR_MSG="Invalid command!"
echo -e "${red}" #}
echo -e "#########################################################" #invalid_option(){
echo -e " ${ERROR_MSG} " # ERROR_MSG="Invalid command!"
echo -e "#########################################################" #}
echo -e "${white}"
fi
if [ -n "${CONFIRM_MSG}" ]; then
echo -e "${green}"
echo -e "#########################################################"
echo -e " ${CONFIRM_MSG} "
echo -e "#########################################################"
echo -e "${white}"
fi
}
print_error(){ #print_msg(){
[ -z "${1}" ] && return # if [ -n "${ERROR_MSG}" ]; then
echo -e "${red}" # echo -e "${red}"
echo -e "#########################################################" # echo -e "#########################################################"
echo -e " ${1} " # echo -e " ${ERROR_MSG} "
echo -e "#########################################################" # echo -e "#########################################################"
echo -e "${white}" # echo -e "${white}"
} # fi
# if [ -n "${CONFIRM_MSG}" ]; then
print_confirm(){ # echo -e "${green}"
[ -z "${1}" ] && return # echo -e "#########################################################"
echo -e "${green}" # echo -e " ${CONFIRM_MSG} "
echo -e "#########################################################" # echo -e "#########################################################"
echo -e " ${1} " # echo -e "${white}"
echo -e "#########################################################" # fi
echo -e "${white}" #}
} #
#clear_msg(){
clear_msg(){ # unset CONFIRM_MSG
unset CONFIRM_MSG # unset ERROR_MSG
unset ERROR_MSG #}
}
check_euid check_euid
init_ini init_ini

View File

@@ -11,6 +11,11 @@
set -e set -e
### global variables
NGINX_SA="/etc/nginx/sites-available"
NGINX_SE="/etc/nginx/sites-enabled"
NGINX_CONFD="/etc/nginx/conf.d"
#===================================================# #===================================================#
#=================== REMOVE NGINX ==================# #=================== REMOVE NGINX ==================#
#===================================================# #===================================================#

View File

@@ -11,12 +11,16 @@
set -e set -e
### base variables ### global variables
SYSTEMD="/etc/systemd/system" SYSTEMD="/etc/systemd/system"
INI_FILE="${HOME}/.kiauh.ini"
LOGFILE="/tmp/kiauh.log" LOGFILE="/tmp/kiauh.log"
# setting up some frequently used functions #================================================#
check_euid(){ #=================== STARTUP ====================#
#================================================#
function check_euid(){
if [ "${EUID}" -eq 0 ] if [ "${EUID}" -eq 0 ]
then then
echo -e "${red}" echo -e "${red}"
@@ -28,13 +32,121 @@ check_euid(){
fi fi
} }
#================================================#
#============= MESSAGE FORMATTING ===============#
#================================================#
green=$(echo -en "\e[92m")
yellow=$(echo -en "\e[93m")
red=$(echo -en "\e[91m")
cyan=$(echo -en "\e[96m")
white=$(echo -en "\e[39m")
function select_msg() {
echo -e "${white}>>>>>> $1"
}
function warn_msg(){
echo -e "${red}>>>>>> $1${white}"
}
function status_msg(){
echo; echo -e "${yellow}###### $1${white}"
}
function ok_msg(){
echo -e "${green}>>>>>> $1${white}"
}
function error_msg(){
echo -e "${red}>>>>>> $1${white}"
}
function abort_msg(){
echo -e "${red}<<<<<< $1${white}"
}
function title_msg(){
echo -e "${cyan}$1${white}"
}
function print_error(){
[ -z "${1}" ] && return
echo -e "${red}"
echo -e "#########################################################"
echo -e " ${1} "
echo -e "#########################################################"
echo -e "${white}"
}
function print_confirm(){
[ -z "${1}" ] && return
echo -e "${green}"
echo -e "#########################################################"
echo -e " ${1} "
echo -e "#########################################################"
echo -e "${white}"
}
#================================================#
#=================== LOGGING ====================#
#================================================#
function timestamp() { function timestamp() {
date +"[%F %T]" date +"[%F %T]"
} }
function log() { function log_info() {
local message="${1}" local message="${1}"
echo -e "$(timestamp) ${message}" | tr -s " " >> "${LOGFILE}" echo -e "$(timestamp) <INFO> ${message}" | tr -s " " >> "${LOGFILE}"
}
function log_warning() {
local message="${1}"
echo -e "$(timestamp) <WARN> ${message}" | tr -s " " >> "${LOGFILE}"
}
function log_error() {
local message="${1}"
echo -e "$(timestamp) <ERR> ${message}" | tr -s " " >> "${LOGFILE}"
}
#================================================#
#=============== KIAUH SETTINGS =================#
#================================================#
function read_kiauh_ini(){
if [ ! -f "${INI_FILE}" ]; then
log_error "Reading from .kiauh.ini failed! File not found!"
return
fi
log_info "Reading from .kiauh.ini"
source "${INI_FILE}"
}
function init_ini(){
### remove pre-version 4 ini files
if [ -f "${INI_FILE}" ] && ! grep -Eq "^KIAUH v4\.0\.0$" "${INI_FILE}"; then
rm "${INI_FILE}"
fi
### initialize ini file
if [ ! -f "${INI_FILE}" ]; then
{
echo -e "File creation date: $(date)"
echo -e "#=================================================#"
echo -e "# KIAUH - Klipper Installation And Update Helper #"
echo -e "# https://github.com/th33xitus/kiauh #"
echo -e "# DO NOT edit this file! #"
echo -e "#=================================================#"
echo -e "KIAUH v4.0.0"
} >> "${INI_FILE}"
fi
if ! grep -Eq "^backup_before_update=." "${INI_FILE}"; then
echo -e "\nbackup_before_update=false\c" >> "${INI_FILE}"
fi
if ! grep -Eq "^logupload_accepted=." "${INI_FILE}"; then
echo -e "\nlogupload_accepted=false\c" >> "${INI_FILE}"
fi
if ! grep -Eq "^custom_klipper_cfg_loc=" "${INI_FILE}"; then
echo -e "\ncustom_klipper_cfg_loc=\c" >> "${INI_FILE}"
fi
if ! grep -Eq "^mainsail_always_install_latest=" "${INI_FILE}"; then
echo -e "\nmainsail_always_install_latest=false\c" >> "${INI_FILE}"
fi
fetch_webui_ports
} }
check_klipper_cfg_path(){ check_klipper_cfg_path(){
@@ -166,10 +278,6 @@ set_klipper_cfg_path(){
do_action_service "restart" "moonraker" do_action_service "restart" "moonraker"
} }
source_kiauh_ini(){
source $INI_FILE
}
do_action_service(){ do_action_service(){
shopt -s extglob # enable extended globbing shopt -s extglob # enable extended globbing
SERVICES="${SYSTEMD}/$2?(-*([0-9])).service" SERVICES="${SYSTEMD}/$2?(-*([0-9])).service"
@@ -487,33 +595,3 @@ function fetch_webui_ports(){
fi fi
done done
} }
init_ini(){
### copy an existing kiauh.ini to its new location to keep all possible saved values
if [ -f "${SRCDIR}/kiauh/kiauh.ini" ] && [ ! -f "${INI_FILE}" ]; then
cp "${SRCDIR}/kiauh/kiauh.ini" "${INI_FILE}"
fi
if [ ! -f "${INI_FILE}" ]; then
echo -e "#don't edit this file if you don't know what you are doing...\c" > "${INI_FILE}"
fi
if [ ! $(grep -E "^backup_before_update=." "${INI_FILE}") ]; then
echo -e "\nbackup_before_update=false\c" >> "${INI_FILE}"
fi
if [ ! $(grep -E "^previous_origin_state=[[:alnum:]]" "${INI_FILE}") ]; then
echo -e "\nprevious_origin_state=0\c" >> "${INI_FILE}"
fi
if [ ! $(grep -E "^previous_smoothing_state=[[:alnum:]]" "${INI_FILE}") ]; then
echo -e "\nprevious_smoothing_state=0\c" >> "${INI_FILE}"
fi
if [ ! $(grep -E "^previous_shaping_state=[[:alnum:]]" "${INI_FILE}") ]; then
echo -e "\nprevious_shaping_state=0\c" >> "${INI_FILE}"
fi
if [ ! $(grep -E "^logupload_accepted=." "${INI_FILE}") ]; then
echo -e "\nlogupload_accepted=false\c" >> "${INI_FILE}"
fi
###add empty klipper config path if missing
if [ ! $(grep -E "^klipper_cfg_loc=" "${INI_FILE}") ]; then
echo -e "\nklipper_cfg_loc=\c" >> "${INI_FILE}"
fi
fetch_webui_ports
}