ask_for_yn now use stdout to return

This commit is contained in:
xiongdian.me 2023-01-06 14:21:16 +08:00
parent 27bfcacc07
commit 90895fe4a9
3 changed files with 34 additions and 22 deletions

View File

@ -1,6 +1,5 @@
#!/bin/bash #!/bin/bash
# ask_for_Yn is not compatible with `set -e` set -e
THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd ) THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
source "$THIS_DIR/tools/common.sh" source "$THIS_DIR/tools/common.sh"
@ -29,22 +28,32 @@ HOME_SYMLINKS_DST[0]=".ssh/authorized_keys2"
install_dependencies() install_dependencies()
{ {
local ret=0
fmt_note "installing dependencies ..." fmt_note "installing dependencies ..."
set +e
case $(get_os_name) in case $(get_os_name) in
"ubuntu"|"debian" ) "ubuntu"|"debian" )
$SUDOE "$DOTFILES/tools/ubuntu.sh" apt-install $SUDOE "$DOTFILES/tools/ubuntu.sh" apt-install
ret=$?
;; ;;
"alpine" ) "alpine" )
$SUDOE "$DOTFILES/tools/alpine.sh" apk-add $SUDOE "$DOTFILES/tools/alpine.sh" apk-add
ret=$?
;; ;;
"macos" ) "macos" )
"$DOTFILES/tools/macos.sh" brew-install "$DOTFILES/tools/macos.sh" brew-install
ret=$?
;; ;;
"msys" ) "msys" )
"$DOTFILES/tools/msys2.sh" pacman-S "$DOTFILES/tools/msys2.sh" pacman-S
ret=$?
;; ;;
* ) fmt_error "dfs auto-install is not implemented on OS: $(get_os_name). skipping ..." * ) fmt_error "dfs auto-install is not implemented on OS: $(get_os_name). skipping ..."
esac esac
set -e
if [[ "$ret" != "0" ]]; then
fmt_error "failed to install dependencies."
fi
} }
preinstall_check() preinstall_check()
@ -62,8 +71,8 @@ preinstall_check()
for i in "${optional_commands[@]}"; do for i in "${optional_commands[@]}"; do
if ! command -v $i 1>/dev/null; then if ! command -v $i 1>/dev/null; then
fmt_warning "\"$i\" not found" fmt_warning "\"$i\" not found"
ask_for_Yn "continue anyway?" yn=$(ask_for_Yn "continue anyway?")
if [[ "$?" == "0" ]]; then if [[ "$yn" == "0" ]]; then
fmt_info "all this utils are suggested: ${optional_commands[@]}" fmt_info "all this utils are suggested: ${optional_commands[@]}"
fmt_info "install them manually or check scripts in tools/" fmt_info "install them manually or check scripts in tools/"
fmt_fatal "aborting ..." fmt_fatal "aborting ..."
@ -120,8 +129,8 @@ install_symlink()
echo ---------- echo ----------
stat $dst stat $dst
echo ---------- echo ----------
ask_for_yN "would you like to replace ${dst}?" yn=$(ask_for_yN "would you like to replace ${dst}?")
if [ $? -eq 1 ]; then if [[ "$yn" == "1" ]]; then
rm $dst rm $dst
else else
fmt_error "aborting this job ..." fmt_error "aborting this job ..."
@ -242,10 +251,9 @@ install()
uninstall() uninstall()
{ {
ask_for_yN "do you really want to uninstall?" yn=$(ask_for_yN "do you really want to uninstall?")
if [[ $? != 1 ]]; then if [[ "$yn" != "1" ]]; then
fmt_error "aborting this job ..." fmt_fatal "aborting this job ..."
return
fi fi
uninstall_update uninstall_update
uninstall_crontab uninstall_crontab
@ -257,6 +265,7 @@ uninstall()
FUNC=install FUNC=install
INSTALL_DEP=0 INSTALL_DEP=0
store_config=0
for i in ${GOT_OPTS[@]}; do for i in ${GOT_OPTS[@]}; do
case $i in case $i in
-i ) FUNC=install ;; -i ) FUNC=install ;;
@ -264,6 +273,7 @@ for i in ${GOT_OPTS[@]}; do
-d|--dev ) export DFS_DEV=1; set -x ;; -d|--dev ) export DFS_DEV=1; set -x ;;
-a|--auto ) INSTALL_DEP=1 ;; -a|--auto ) INSTALL_DEP=1 ;;
-s|--secure ) export DFS_DEV=0 ;; -s|--secure ) export DFS_DEV=0 ;;
-x ) store_config=1 ;; # TODO: store and write to config
* ) fmt_fatal "unknown option \"$i\"" ;; * ) fmt_fatal "unknown option \"$i\"" ;;
esac esac
done done

View File

@ -134,26 +134,28 @@ fi
ask_for_yN() ask_for_yN()
{ {
while [[ -z "$DFS_QUIET" || "$DFS_QUIET" == "0" ]]; do if [[ "$DFS_QUIET" == "1" ]]; then
echo 0
else
read -p "${FMT_YELLOW}$1${FMT_RESET} [yN]: " yn read -p "${FMT_YELLOW}$1${FMT_RESET} [yN]: " yn
case $yn in case $yn in
[Yy]* ) return 1;; [Yy]* ) echo 1;;
* ) return 0;; * ) echo 0;;
esac esac
done fi
return 0
} }
ask_for_Yn() ask_for_Yn()
{ {
while [[ -z "$DFS_QUIET" || "$DFS_QUIET" == "0" ]]; do if [[ "$DFS_QUIET" == "1" ]]; then
echo 1
else
read -p "${FMT_YELLOW}$1${FMT_RESET} [Yn]: " yn read -p "${FMT_YELLOW}$1${FMT_RESET} [Yn]: " yn
case $yn in case $yn in
[Nn]* ) return 0;; [Nn]* ) echo 0;;
* ) return 1;; * ) echo 1;;
esac esac
done fi
return 1
} }
post_log() post_log()

View File

@ -31,8 +31,8 @@ while [[ $# > 0 || -n "$ARG" ]]; do
fi fi
done done
# install # install TODO: multiple users; remove -a after installed for the first user
if command -v su 1>/dev/null && test -n "$DFS_USER"; then if command -v su 1>/dev/null && test -n "$DFS_USER" && test "$DFS_USER" != "$(whoami)"; then
SUCMD="su $DFS_USER" SUCMD="su $DFS_USER"
else else
SUCMD="bash" SUCMD="bash"