From 8114744235761f709df8f649ab32285a7f9634af Mon Sep 17 00:00:00 2001
From: Kostis Anagnostopoulos <ankostis@gmail.com>
Date: Tue, 17 May 2022 22:06:24 +0300
Subject: [PATCH] FEAT(setup) -k: KEEP_GOING, -f: FORCE >

fix: script nargs($#) NOT updated with `set -- new args`!
---
 setup-account.sh | 59 ++++++++++++++++++++++++++++++++++++------------
 1 file changed, 45 insertions(+), 14 deletions(-)

diff --git a/setup-account.sh b/setup-account.sh
index e55042c..db809a2 100755
--- a/setup-account.sh
+++ b/setup-account.sh
@@ -3,24 +3,24 @@
 # Call this script with the name of the action-function as arg[1].
 function _HELP { echo \
 "$mycmd - setup a BDAP user account with (pending) actions for keyboard, bash, git, conda
-SYNTAX:
+logout & re-login:
     $mycmd [-h]--help] ...
-    $mycmd [-n] [ACTION]...
+    $mycmd [-n] [-k] [-f] [ACTION]...
 OPTIONS:
-    -n:  dry_run - don't actually update stuff
+    -n:  dry_run    - don't actually update stuff (but evaluate checks)
+    -k:  keep_going - don't stop on errors
+    -f:  force      - update stuff regardless of checks
 ACTION:
 $(for i in $(_list_all_actions); do echo "    $i"; done)
 NOTES:
     * Without any args, it  launches 'all' actions above, in that order.
     * The 'upd30_git_user_XXX' actions awaits interactively input from the user.
-    * You may have to logout & re-login after 'upd40_bashrc_conda_init',
-      for 'upd50_create_conda_env' to succeed.
+    * It stops after 'upd40_bashrc_conda_init', to logout & re-login,
+      for 'upd50_create_conda_env' to succeed (unless -k).
     * You can repeatedly run this script, until all actions succeed.
 "
 }
 
-set -e
-
 mycmd="${0##*/}"
 mydir=$(realpath "${0%/*}")
 cd "$mydir"
@@ -37,6 +37,10 @@ function _short {
     fi
 }
 
+function _join {
+    echo $*
+}
+
 conda_env_prefix=$(_short "$mydir/env")
 
 function _mute {
@@ -50,13 +54,16 @@ function _log {
 
 function _cmd_before_update {
     local lognstack=$(( ${lognstack:-1} + 1))
+    if [ -n "$force" ]; then
+        set -- false  # Force 2nd (running) branch in the check below.
+    fi
     if "${@}"; then
         _log -e "${notrun_msg:-already ok}"
         return 1
     else
         _log -en "${running_msg:-updating...}"
         if [ -n "${dry_run}" ]; then
-            echo "(DRY_RUN)"
+            echo "${force:+(FORCE)}(DRY_RUN)"
             return 1 # (already run) when $dry_run
         fi
         return 0  # true, to run if-body updating stuff.
@@ -276,7 +283,12 @@ function upd40_bashrc_conda_init {
     if _grep_before_update "$check_file" ">>> conda initialize >>>"; then
         ## Will print a message to logout & re-login.
         conda init bash
-        exit 0
+
+        if [ -n "$keep_going" ]; then
+            _log "Would STOP to logout + re-login if not KEEP_GOING."
+        else
+            exit 0
+        fi
     fi
 }
 
@@ -342,13 +354,32 @@ function all {
 if [[ "${@#-h}" != "$@" || "${@#--help}" != "$@" ]]; then
     _HELP
 else
-    [[ "${@#-n}" != "$@" ]] && dry_run=1
-    set -- "${@#-n}"
-    if [[ $# -eq 0 ]]; then
-        echo "Running all actions${dry_run:+(DRY_RUN)}..."
+    let nargs="$#"
+    if [[ "${@#-k}" != "$@" ]]; then
+        keep_going=1
+        set -- "${@#-k}"
+        ((nargs-=1))
+    fi
+    if [[ "${@#-n}" != "$@" ]]; then
+        dry_run=1
+        set -- "${@#-n}"
+        ((nargs-=1))
+    fi
+    if [[ "${@#-f}" != "$@" ]]; then
+        force=1
+        set -- "${@#-f}"
+        ((nargs-=1))
+    fi
+
+    if [ -z "$keep_going" ]; then
+        set -e
+    fi
+
+    if [ "$nargs" -eq 0 ]; then
+        echo "Running all actions${dry_run:+(DRY_RUN)}${keep_going:+(KEEP_GOING)}${force:+(FORCE)}..."
         all
     else
-        echo "Running x$# actions: '${@}'"
+        echo "Running x$nargs actions: '${@}'"
         ## execute the asked action.
         for cmd in "${@}"; do
             $cmd
-- 
GitLab