Handle existing qhtcp project dir gracefully
This commit is contained in:
@@ -267,14 +267,16 @@ random_words() {
|
|||||||
printf "%s_" "${arr[@]}" | sed 's/_$//'
|
printf "%s_" "${arr[@]}" | sed 's/_$//'
|
||||||
}
|
}
|
||||||
# @description Backup one or more files to an incremented .bk file
|
# @description Backup one or more files to an incremented .bk file
|
||||||
|
# @exitcode backup iterator max 255
|
||||||
backup() {
|
backup() {
|
||||||
debug "Running: ${FUNCNAME[0]}" "$@"
|
debug "Running: ${FUNCNAME[0]}" "$@"
|
||||||
for f in "$@"; do
|
for f in "$@"; do
|
||||||
[[ -e $f ]] || continue
|
[[ -e $f ]] || continue
|
||||||
count=1
|
count=1
|
||||||
while [[ -f $f.bk.$count ]]; do
|
while [[ -f $f.bk.$count ]]; do
|
||||||
count=$((count+1))
|
count=$((count++))
|
||||||
done
|
done
|
||||||
|
echo "Backing up $f to $f.bk.$count"
|
||||||
debug "rsync -a $f $f.bk.$count"
|
debug "rsync -a $f $f.bk.$count"
|
||||||
rsync -a "$f" "$f.bk.$count"
|
rsync -a "$f" "$f.bk.$count"
|
||||||
done
|
done
|
||||||
@@ -811,17 +813,34 @@ qhtcp() {
|
|||||||
debug "Running: ${FUNCNAME[0]}"
|
debug "Running: ${FUNCNAME[0]}"
|
||||||
|
|
||||||
if [[ -d $QHTCP_PROJECT_DIR ]]; then
|
if [[ -d $QHTCP_PROJECT_DIR ]]; then
|
||||||
|
# Handle existing output directory
|
||||||
echo "A project already exists at $QHTCP_PROJECT_DIR"
|
echo "A project already exists at $QHTCP_PROJECT_DIR"
|
||||||
ask "Safely update $QHTCP_PROJECT_DIR from the $QHTCP_TEMPLATE_DIR template?"
|
echo "Would you like to (c)ontinue, (u)pdate it from the template and continue, or (b)ack it up and continue from scratch?"
|
||||||
if ! ((YES)) && ask "Back up $QHTCP_PROJECT_DIR to $QHTCP_PROJECT_DIR.bk first and start fresh?"; then
|
for i in {1..3}; do # give the user three chances to get it right
|
||||||
mv "$QHTCP_PROJECT_DIR" "$QHTCP_PROJECT_DIR.bk" || (echo "Backup unsuccessful, exiting"; exit 1)
|
((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
|
fi
|
||||||
fi
|
;;
|
||||||
|
b)
|
||||||
# Copy template to QHTCP project directory
|
backup "$QHTCP_PROJECT_DIR" && rm -rf "$QHTCP_PROJECT_DIR"
|
||||||
if rsync --archive --update "$QHTCP_TEMPLATE_DIR"/ "$QHTCP_PROJECT_DIR"; then
|
if rsync --archive --update "$QHTCP_TEMPLATE_DIR"/ "$QHTCP_PROJECT_DIR"; then
|
||||||
echo "New project created at $QHTCP_PROJECT_DIR"
|
echo "New project created at $QHTCP_PROJECT_DIR"
|
||||||
fi
|
fi
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
err "Invalid response, please try again"
|
||||||
|
continue
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
break
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
|
||||||
# Sets STUDIES_NUMS and NUM_STUDIES (yes this makes sense)
|
# Sets STUDIES_NUMS and NUM_STUDIES (yes this makes sense)
|
||||||
get_studies "$STUDY_INFO_FILE"
|
get_studies "$STUDY_INFO_FILE"
|
||||||
@@ -832,7 +851,7 @@ qhtcp() {
|
|||||||
|
|
||||||
# If the next Exp dir already exists don't use it
|
# If the next Exp dir already exists don't use it
|
||||||
while [[ -d $QHTCP_PROJECT_DIR/Exp$next_study_num ]]; do
|
while [[ -d $QHTCP_PROJECT_DIR/Exp$next_study_num ]]; do
|
||||||
(( next_study_num=next_study_num+1 ))
|
(( next_study_num++ ))
|
||||||
done
|
done
|
||||||
|
|
||||||
# Use initials from project not whoami
|
# Use initials from project not whoami
|
||||||
|
|||||||
Reference in New Issue
Block a user