From 82d6ebf459eb1fcf918b5555465b2371a1ef6a8a Mon Sep 17 00:00:00 2001 From: th33xitus Date: Wed, 27 Apr 2022 11:01:58 +0200 Subject: [PATCH] refactor: klipper switch repo mechanics Signed-off-by: Dominik Willner th33xitus@gmail.com --- klipper_repos.txt | 11 ----- klipper_repos.txt.example | 18 +++++++++ scripts/switch_klipper_repo.sh | 74 ++++++++++++++++++---------------- scripts/utilities.sh | 4 +- 4 files changed, 59 insertions(+), 48 deletions(-) delete mode 100644 klipper_repos.txt create mode 100644 klipper_repos.txt.example diff --git a/klipper_repos.txt b/klipper_repos.txt deleted file mode 100644 index 3a6ac38..0000000 --- a/klipper_repos.txt +++ /dev/null @@ -1,11 +0,0 @@ -############################################################## -## DO NOT REMOVE OR EDIT THIS COMMENT BLOCK! ## -## Always add the URL and branch in a pattern like this: ## -## , ## -## 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 diff --git a/klipper_repos.txt.example b/klipper_repos.txt.example new file mode 100644 index 0000000..6cc3393 --- /dev/null +++ b/klipper_repos.txt.example @@ -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 ','. +# , -> 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 diff --git a/scripts/switch_klipper_repo.sh b/scripts/switch_klipper_repo.sh index 0ecac90..d8cdd08 100644 --- a/scripts/switch_klipper_repo.sh +++ b/scripts/switch_klipper_repo.sh @@ -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 diff --git a/scripts/utilities.sh b/scripts/utilities.sh index 14041fe..2ac7e6d 100644 --- a/scripts/utilities.sh +++ b/scripts/utilities.sh @@ -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}" }