refactor: klipper switch repo mechanics

Signed-off-by: Dominik Willner th33xitus@gmail.com
This commit is contained in:
th33xitus
2022-04-27 11:01:58 +02:00
parent d000ba1373
commit 82d6ebf459
4 changed files with 59 additions and 48 deletions

View File

@@ -1,11 +0,0 @@
##############################################################
## DO NOT REMOVE OR EDIT THIS COMMENT BLOCK! ##
## Always add the URL and branch in a pattern like this: ##
## <URL>,<branch> ##
## E.g.: https://github.com/Klipper3d/klipper,master ##
## ##
## A wrong format will cause problems! ##
## Do NOT insert blank lines. Only add one entry per line! ##
##############################################################
##~~~~~ Add the custom repositories below THIS line ~~~~~~~~##
https://github.com/Klipper3d/klipper,master

18
klipper_repos.txt.example Normal file
View File

@@ -0,0 +1,18 @@
# This file acts as an example file.
#
# 1) Make a copy of this file and rename it to 'klipper_repos.txt'
# 2) Add your custom Klipper repository to the bottom of that copy
# 3) Save the file
#
# Back in KIAUH you can now go into -> [Settings] and use action '2' to set a different Klipper repository
#
# Make sure to always separate the repository and the branch with a ','.
# <repository>,<branch> -> https://github.com/Klipper3d/klipper,master
# If you omit a branch, it will always default to 'master'
#
# You are allowed to omit the 'https://github.com/' part of the repository URL
# Down below are now a few examples of what is considered as valid:
https://github.com/Klipper3d/klipper,master
https://github.com/Klipper3d/klipper
Klipper3d/klipper,master
Klipper3d/klipper

View File

@@ -13,26 +13,40 @@ set -e
function change_klipper_repo_menu(){
local repo_file="${SRCDIR}/kiauh/klipper_repos.txt"
local url branch
local repos=() branches=()
if [ ! -f "${repo_file}" ]; then
print_error "File 'klipper_repos.txt' in ${SRCDIR}/kiauh not found!"
return
fi
### generate the repolist from the klipper_repos.txt textfile
while IFS="," read -r repo branch; do
repo=$(echo "${repo}" | sed -r "s/^http(s)?:\/\/github.com\///" | sed "s/\.git$//" )
repos+=("${repo}")
### if branch is not given, default to 'master'
[ -z "${branch}" ] && branch="master"
branches+=("${branch}")
done < <(grep -E "^[^#]" "${repo_file}")
top_border
echo -e "| ~~~~~~~~ [ Set custom Klipper repo ] ~~~~~~~~ | "
hr
blank_line
### dynamically generate the repolist from the klipper_repos.txt textfile
get_klipper_repo_list "${repo_file}"
### print repolist
local i=0
for _ in "${repos[@]}"; do
printf "| %s) %-63s|\n" "${i}" "${yellow}${repos[${i}]}${white}${branches[${i}]}"
i=$((i+1))
done
blank_line
back_help_footer
while IFS="," read -r col1 col2; do
url+=("${col1}")
branch+=("${col2}")
done < <(grep "" "${repo_file}" | tail -n "+11")
while true; do
read -p "${cyan}Perform action:${white} " option
read -p "${cyan}###### Perform action:${white} " option
case "${option}" in
0 | "$((option < ${#url[@]}))")
0 | "$((option < ${#repos[@]}))")
select_msg "Repo: ${repos[option]} Branch: ${branches[option]}"
if [ -d "${KLIPPER_DIR}" ]; then
top_border
echo -e "| ${red}!!! ATTENTION !!!${white} |"
@@ -45,8 +59,8 @@ function change_klipper_repo_menu(){
case "${yn}" in
Y|y|Yes|yes|"")
select_msg "Yes"
switch_klipper_repo "${url[${option}]}" "${branch[${option}]}"
set_custom_klipper_repo "${url[${option}]}" "${branch[${option}]}"
switch_klipper_repo "${repos[${option}]}" "${branches[${option}]}"
set_custom_klipper_repo "${repos[${option}]}" "${branches[${option}]}"
break;;
N|n|No|no)
select_msg "No"
@@ -56,8 +70,8 @@ function change_klipper_repo_menu(){
esac
done
else
status_msg "Set custom Klipper repository to:\n ● Repository URL: ${url[${option}]}\n ● Branch: ${branch[${option}]}"
set_custom_klipper_repo "${url[${option}]}" "${branch[${option}]}"
status_msg "Set custom Klipper repository to:\n ● Repository URL: ${repos[${option}]}\n ● Branch: ${branches[${option}]}"
set_custom_klipper_repo "${repos[${option}]}" "${branches[${option}]}"
ok_msg "This repo will now be used for new Klipper installations!\n"
fi
break;;
@@ -73,30 +87,21 @@ function change_klipper_repo_menu(){
error_msg "Invalid command!";;
esac
done
change_klipper_repo_menu
}
#================================================#
#=================== HELPERS ====================#
#================================================#
function get_klipper_repo_list(){
local repo_file=${1} i=0
while IFS="," read -r col1 col2; do
col1=$(echo "${col1}" | sed "s/https:\/\/github\.com\///" | sed "s/\.git$//" )
col1=${yellow}${col1}${white}
printf "| ${i}) %s → %-31s|\n" "${col1}" "${col2}"
i=$((i+1))
done < <(grep "" "${repo_file}" | tail -n "+11")
}
function switch_klipper_repo(){
local url branch
url=${1} branch=${2}
local repo_url branch
repo_url="https://github.com/${1}" branch=${2}
status_msg "Switching Klipper repository..."
do_action_service "stop" "klipper"
cd "${HOME}"
[ -d "${KLIPPER_DIR}" ] && rm -rf "${KLIPPER_DIR}"
git clone "${url}" "klipper" && cd "${KLIPPER_DIR}"
git clone "${repo_url}" "klipper" && cd "${KLIPPER_DIR}"
git checkout "${branch}" && cd "${HOME}"
do_action_service "start" "klipper"
}
@@ -105,14 +110,13 @@ function show_custom_klipper_repo_help(){
top_border
echo -e "| ~~~~ < ? > Help: Custom Klipper repo < ? > ~~~~ |"
hr
echo -e "| With this setting, it is possible to install Klipper | "
echo -e "| from a custom repository. It will also switch an | "
echo -e "| existing Klipper installation to the newly selected | "
echo -e "| source repository. | "
echo -e "| A list of selectable repositories is automatically | "
echo -e "| generated by a 'klipper_repos.txt' textfile in KIAUHs | "
echo -e "| root folder. You can add as many additional repos as | "
echo -e "| you wish. Make sure to always add URL ${red}and${white} branch! | "
echo -e "| With this setting, it is possible to install Klipper |"
echo -e "| from a custom repository. It will also switch an |"
echo -e "| existing Klipper installation to the newly selected |"
echo -e "| source repository. |"
echo -e "| A list of repositories is automatically generated by |"
echo -e "| a 'klipper_repos.txt' textfile in KIAUHs root folder. |"
echo -e "| An example file is provided at the same location. |"
blank_line
back_footer
while true; do

View File

@@ -334,9 +334,9 @@ function toggle_backup_before_update(){
function set_custom_klipper_repo() {
read_kiauh_ini "${FUNCNAME[0]}"
local repo_url=${1} branch=${2}
local repo=${1} branch=${2}
sed -i "/^custom_klipper_repo=/d" "${INI_FILE}"
sed -i '$a'"custom_klipper_repo=${repo_url}" "${INI_FILE}"
sed -i '$a'"custom_klipper_repo=${repo}" "${INI_FILE}"
sed -i "/^custom_klipper_repo_branch=/d" "${INI_FILE}"
sed -i '$a'"custom_klipper_repo_branch=${branch}" "${INI_FILE}"
}