From 40a4e7d48685e22b4cc1932ec967d9ff850eda59 Mon Sep 17 00:00:00 2001 From: Bryan Roessler Date: Mon, 29 Jul 2024 11:37:51 -0400 Subject: [PATCH] Beginning debug/cleanup --- workflow/qhtcp-workflow | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/workflow/qhtcp-workflow b/workflow/qhtcp-workflow index ed33d652..0f672293 100755 --- a/workflow/qhtcp-workflow +++ b/workflow/qhtcp-workflow @@ -237,11 +237,18 @@ ask_pn() { Make it descriptive and avoid spaces and special characters. Example: ${example_pn} EOF - read -r -p "Enter a new or existing project name ($example_pn): " PROJECT - if [[ -z $PROJECT ]]; then - echo "Setting project name to the default: $example_pn" - PROJECT="$example_pn" - fi + trys=3 + for ((i=1; i<=$trys; i++)); do # give the user up to 3 tries + read -r -p "Enter a new or existing project name ($example_pn): " PROJECT + if [[ -z $PROJECT ]]; then + PROJECT="$example_pn" + else + sanitize_pn "$PROJECT" && break + err "Invalid project name: $PROJECT" + echo "Retrying ($i of $trys)" + fi + done + echo "Using project name: $PROJECT" } debug() { (( DEBUG )) && echo "Debug: $*"; } # Not super portable but nice to have @@ -1479,7 +1486,7 @@ main() { # Set the automatic project directory prefix PROJECT_USER="$(whoami)" PROJECT_PREFIX="${DATE}_${PROJECT_USER}" # reversed these so easier to sort and parse date - san() { [[ $1 =~ [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]_.+_.+ ]]; } # sanitizer regex for prefix + sanitize_pn() { [[ $1 =~ [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]_.+_.+ ]]; } # sanitizer regex for prefix declare -ag PROJECTS=() # this array will hold all of the projects for this run @@ -1487,18 +1494,21 @@ main() { # Prompt user for the PROJECT if we still don't have one if [[ ${#PROJECTS[@]} -eq 0 ]]; then # still allows for environment overrides + ask_pn && PROJECTS+=("$PROJECT") while :; do - ask_pn || break - PROJECTS+=("$PROJECT") + || break + done + + fi for i in "${!PROJECTS[@]}"; do - if ! san "${PROJECTS[i]}"; then + if ! sanitize_pn "${PROJECTS[i]}"; then echo "Project name ${PROJECTS[i]} is invalid" echo "Enter a replacement" ask_pn - san "$PROJECT" || (echo "RTFM"; return 1) + sanitize_pn "$PROJECT" || (echo "RTFM"; return 1) PROJECTS[i]="$PROJECT" fi done