From eb06256aeb02335f69b886026debea87d65b1159 Mon Sep 17 00:00:00 2001 From: Cezar Craciunoiu Date: Thu, 25 Apr 2024 17:20:23 +0300 Subject: [PATCH 1/2] feat(webinstall): Do not reask for sudo after asking once Signed-off-by: Cezar Craciunoiu --- tools/webinstall/install.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tools/webinstall/install.sh b/tools/webinstall/install.sh index b8691d938..c1c19af64 100755 --- a/tools/webinstall/install.sh +++ b/tools/webinstall/install.sh @@ -28,6 +28,7 @@ INSTALL_SERVER=${INSTALL_SERVER:-https://get.kraftkit.sh} INSTALL_TLS=${INSTALL_TLS:-y} DEBUG=${DEBUG:-n} NEED_TTY=${NEED_TTY:-y} +ASK_SUDO=${ASK_SUDO:-y} # Commands as variables to make them easier to override APK=${APK:-apk} @@ -240,13 +241,15 @@ do_cmd() { # Retry with root if the user wants to, otherwise exit # with the return value of the command - if prompt_user_yes_no "do you want to retry with root? [y/N]: " "n"; then + if [ "$ASK_SUDO" = "n" ] || \ + prompt_user_yes_no "do you want to retry with root from now on? [y/N]: " "n"; then # Check if we have a tty and run with it if we do, # otherwise exit with an error # This is because sudo requires a tty to input the password if [ "$NEED_TTY" = "y" ]; then # shellcheck disable=SC2002 $SUDO sh -c "$@" < /dev/tty + ASK_SUDO="n" else err "fatal: cannot retry with root without a tty." fi From 4bd702c62bd842b85dbe1f8cfdd3441fcadd50da Mon Sep 17 00:00:00 2001 From: Cezar Craciunoiu Date: Thu, 25 Apr 2024 17:34:08 +0300 Subject: [PATCH 2/2] refactor(webinstall): Make lines shorter than ~80 characters Signed-off-by: Cezar Craciunoiu --- tools/webinstall/install.sh | 48 ++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/tools/webinstall/install.sh b/tools/webinstall/install.sh index c1c19af64..c8d24ed18 100755 --- a/tools/webinstall/install.sh +++ b/tools/webinstall/install.sh @@ -200,12 +200,13 @@ get_user_response() { _RETVAL="$_gur_read_answer" } -# prompt_user_yes_no asks the user a question which must be answered with yes or no. Returns a boolean value. +# prompt_yes_no asks the user a question which must be answered with yes or no. +# Returns a boolean value. # $1: question # $2: default answer # Returns: # _RETVAL: true for "yes", false for "no" -prompt_user_yes_no() { +prompt_yes_no() { while true; do get_user_response "$1" "$2" @@ -241,8 +242,8 @@ do_cmd() { # Retry with root if the user wants to, otherwise exit # with the return value of the command - if [ "$ASK_SUDO" = "n" ] || \ - prompt_user_yes_no "do you want to retry with root from now on? [y/N]: " "n"; then + if [ "$ASK_SUDO" = "n" ] || prompt_yes_no \ + "do you want to retry with root from now on? [y/N]: " "n"; then # Check if we have a tty and run with it if we do, # otherwise exit with an error # This is because sudo requires a tty to input the password @@ -1070,7 +1071,7 @@ install_linux_gnu() { _idd_recommended="" - if prompt_user_yes_no "install recommended dependencies? [y/N]: " "n"; then + if prompt_yes_no "install recommended dependencies? [y/N]: " "n"; then _idd_recommended="--no-install-recommends" else _idd_recommended="--install-recommends" @@ -1089,9 +1090,9 @@ install_linux_gnu() { cd - 1> /dev/null || exit $RM -rf /tmp/kraftkit-bin else - _ilg_msg=$(printf "error: %s%s%s" \ - "Unsupported Linux distribution. " \ - "Try downloading the tar.gz file from " \ + _ilg_msg=$(printf "error: %s%s%s" \ + "Unsupported Linux distribution. " \ + "Try downloading the tar.gz file from " \ "https://github.com/unikraft/kraftkit or switch to manual mode" \ ) err "$_ilg_msg" @@ -1113,9 +1114,9 @@ install_linux_musl() { ) do_cmd "$_ilm_cmd" else - _ilm_msg=$(printf "error: %s%s%s" \ - "Unsupported Linux distribution. " \ - "Try downloading the tar.gz file from " \ + _ilm_msg=$(printf "error: %s%s%s" \ + "Unsupported Linux distribution. " \ + "Try downloading the tar.gz file from " \ "https://github.com/unikraft/kraftkit or switch to manual mode" \ ) err "$_ilm_msg" @@ -1187,20 +1188,20 @@ install_linux_manual() { "[y/N]: " \ ) - if prompt_user_yes_no "$_ill_prompt_msg" "n"; then + if prompt_yes_no "$_ill_prompt_msg" "n"; then PREFIX="$($DIRNAME "$_ill_kraft_path")" else err "fatal: kraft binary already installed." fi else - if prompt_user_yes_no "change the install prefix? [$PREFIX] [y/N]: " "n"; then + if prompt_yes_no "change the install prefix? [$PREFIX] [y/N]: " "n"; then get_user_response "what should the prefix be? [$PREFIX]: " "$PREFIX" PREFIX="$_RETVAL" fi if [ ! -d "$PREFIX" ]; then - if prompt_user_yes_no "prefix does not exist, create? [y/N]: " "n"; then + if prompt_yes_no "prefix does not exist, create? [y/N]: " "n"; then do_cmd "$MKDIR -p $PREFIX" else err "fatal: prefix does not exist." @@ -1274,19 +1275,19 @@ install_darwin_manual() { "[y/N]: " \ ) - if prompt_user_yes_no "$_idr_prompt_msg" "n"; then + if prompt_yes_no "$_idr_prompt_msg" "n"; then PREFIX="$($DIRNAME "$_idr_kraft_path")" else err "fatal: kraft binary already installed." fi else - if prompt_user_yes_no "change the install prefix? [$PREFIX] [y/N]: " "n"; then + if prompt_yes_no "change the install prefix? [$PREFIX] [y/N]: " "n"; then get_user_response "what should the prefix be? [$PREFIX]: " "$PREFIX" PREFIX="$_RETVAL" fi if [ ! -d "$PREFIX" ]; then - if prompt_user_yes_no "prefix does not exist, create? [y/N]: " "n"; then + if prompt_yes_no "prefix does not exist, create? [y/N]: " "n"; then do_cmd "$MKDIR -p $PREFIX" else err "fatal: prefix does not exist." @@ -1429,7 +1430,7 @@ install_dependencies_gnu() { say_debug "Installing dependencies: $_idd_list" _idd_recommended="" - if prompt_user_yes_no "install recommended dependencies? [y/N]: " "n"; then + if prompt_yes_no "install recommended dependencies? [y/N]: " "n"; then _idd_recommended="--no-install-recommends" else _idd_recommended="--install-recommends" @@ -1517,7 +1518,8 @@ install_completions() { _inc_arch="$1" - if ! prompt_user_yes_no "Do you want to install command completions? [Y/n]: " "y"; then + if ! prompt_yes_no \ + "Do you want to install command completions? [Y/n]: " "y"; then return 0 fi @@ -1553,12 +1555,14 @@ install_completions() { elif [ -z "$_inc_answer" ]; then printf "No shell provided. Please try again or exit with Ctrl+C\n" else - printf "Shell %s not supported. Please try again or exit with Ctrl+C\n" "$_inc_answer" + printf "Shell %s not supported. " \ + "Please try again or exit with Ctrl+C\n" "$_inc_answer" fi done if [ -f "$_inc_kraft_config_file" ]; then - if ! prompt_user_yes_no "kraft completions already exist, overwrite? [Y/n]: " "y"; then + if ! prompt_yes_no \ + "kraft completions already exist, overwrite? [Y/n]: " "y"; then return 0 fi fi @@ -1632,7 +1636,7 @@ arg_parse() { check_autoinstall() { _cai_auto_install="" - if prompt_user_yes_no "install kraftkit using package manager? [Y/n]: " "y"; then + if prompt_yes_no "install kraftkit using package manager? [Y/n]: " "y"; then say "installing kraftkit via package manager..." else _cai_auto_install="n"