From 71a6e9b707524ab7566987cc0d6be16657a95349 Mon Sep 17 00:00:00 2001 From: Bryan Roessler Date: Thu, 1 Aug 2024 19:59:51 -0400 Subject: [PATCH] Handle existing qhtcp project dir gracefully --- workflow/qhtcp-workflow | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/workflow/qhtcp-workflow b/workflow/qhtcp-workflow index 224919d3..07a3ecd0 100755 --- a/workflow/qhtcp-workflow +++ b/workflow/qhtcp-workflow @@ -267,14 +267,16 @@ random_words() { printf "%s_" "${arr[@]}" | sed 's/_$//' } # @description Backup one or more files to an incremented .bk file +# @exitcode backup iterator max 255 backup() { debug "Running: ${FUNCNAME[0]}" "$@" for f in "$@"; do [[ -e $f ]] || continue count=1 while [[ -f $f.bk.$count ]]; do - count=$((count+1)) + count=$((count++)) done + echo "Backing up $f to $f.bk.$count" debug "rsync -a $f $f.bk.$count" rsync -a "$f" "$f.bk.$count" done @@ -811,16 +813,33 @@ qhtcp() { debug "Running: ${FUNCNAME[0]}" if [[ -d $QHTCP_PROJECT_DIR ]]; then + # Handle existing output directory echo "A project already exists at $QHTCP_PROJECT_DIR" - ask "Safely update $QHTCP_PROJECT_DIR from the $QHTCP_TEMPLATE_DIR template?" - if ! ((YES)) && ask "Back up $QHTCP_PROJECT_DIR to $QHTCP_PROJECT_DIR.bk first and start fresh?"; then - mv "$QHTCP_PROJECT_DIR" "$QHTCP_PROJECT_DIR.bk" || (echo "Backup unsuccessful, exiting"; exit 1) - fi - fi - - # Copy template to QHTCP project directory - if rsync --archive --update "$QHTCP_TEMPLATE_DIR"/ "$QHTCP_PROJECT_DIR"; then - echo "New project created at $QHTCP_PROJECT_DIR" + echo "Would you like to (c)ontinue, (u)pdate it from the template and continue, or (b)ack it up and continue from scratch?" + for i in {1..3}; do # give the user three chances to get it right + ((YES)) || read -r -p "Hit [Enter] or c to continue: " response + [[ -z $response ]] && break + case $response in + u) + echo "Updating project from template" + echo "Only files that are newer in the template will be overwritten" + if rsync --archive --update "$QHTCP_TEMPLATE_DIR"/ "$QHTCP_PROJECT_DIR"; then + echo "Project updated with template" + fi + ;; + b) + backup "$QHTCP_PROJECT_DIR" && rm -rf "$QHTCP_PROJECT_DIR" + if rsync --archive --update "$QHTCP_TEMPLATE_DIR"/ "$QHTCP_PROJECT_DIR"; then + echo "New project created at $QHTCP_PROJECT_DIR" + fi + ;; + *) + err "Invalid response, please try again" + continue + ;; + esac + break + done fi # Sets STUDIES_NUMS and NUM_STUDIES (yes this makes sense) @@ -832,7 +851,7 @@ qhtcp() { # If the next Exp dir already exists don't use it while [[ -d $QHTCP_PROJECT_DIR/Exp$next_study_num ]]; do - (( next_study_num=next_study_num+1 )) + (( next_study_num++ )) done # Use initials from project not whoami