From f18ca5f555c67f2035edde5eb82672f16b4cf9ad Mon Sep 17 00:00:00 2001 From: f-trycua Date: Sat, 10 May 2025 14:58:40 -0700 Subject: [PATCH] Clean up lume_stop --- libs/lumier/src/lib/utils.sh | 34 ++++++++++++++++++++++++++++------ libs/lumier/src/lib/vm.sh | 2 +- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/libs/lumier/src/lib/utils.sh b/libs/lumier/src/lib/utils.sh index 8a993754..079ca047 100755 --- a/libs/lumier/src/lib/utils.sh +++ b/libs/lumier/src/lib/utils.sh @@ -134,13 +134,35 @@ EOF extract_json_field() { local field_name=$1 local input=$2 - local result - result=$(echo "$input" | grep -oP '"'"$field_name"'"\s*:\s*"\K[^"]+') - if [[ $? -ne 0 ]]; then - echo "" - else - echo "$result" + local result="" + + # First attempt with jq if available (most reliable JSON parsing) + if command -v jq &> /dev/null; then + # Use jq for reliable JSON parsing + result=$(echo "$input" | jq -r ".$field_name // empty" 2>/dev/null) + if [[ -n "$result" ]]; then + echo "$result" + return 0 + fi fi + + # Fallback to grep-based approach with improvements + # First try for quoted string values + result=$(echo "$input" | tr -d '\n' | grep -o "\"$field_name\"\s*:\s*\"[^\"]*\"" | sed -E 's/.*":\s*"(.*)"$/\1/') + if [[ -n "$result" ]]; then + echo "$result" + return 0 + fi + + # Try for non-quoted values (numbers, true, false, null) + result=$(echo "$input" | tr -d '\n' | grep -o "\"$field_name\"\s*:\s*[^,}]*" | sed -E 's/.*":\s*(.*)$/\1/') + if [[ -n "$result" ]]; then + echo "$result" + return 0 + fi + + # Return empty string if field not found + echo "" } extract_json_field_from_file() { diff --git a/libs/lumier/src/lib/vm.sh b/libs/lumier/src/lib/vm.sh index 54379767..b2bd10d7 100755 --- a/libs/lumier/src/lib/vm.sh +++ b/libs/lumier/src/lib/vm.sh @@ -233,7 +233,7 @@ stop_vm() { # still attempt a stop just in case. echo "VM status is unknown ('$vm_status') or VM not found during cleanup. Attempting stop anyway." lume_stop "$VM_NAME" "$STORAGE_PATH" - sleep 5000 + sleep 5 echo "VM '$VM_NAME' stop command issued as a precaution." else echo "VM status is unknown ('$vm_status') or VM not found. Not attempting stop."