From 3a4e708b029562b5e33944331886c9f00f16eedc Mon Sep 17 00:00:00 2001 From: Muhammad Atif Ali Date: Sat, 16 Dec 2023 17:26:44 +0300 Subject: [PATCH] Discard changes to slackme/slackme.sh --- slackme/slackme.sh | 87 +++++++++++++++++++++++----------------------- 1 file changed, 43 insertions(+), 44 deletions(-) diff --git a/slackme/slackme.sh b/slackme/slackme.sh index 7874d9f..17f74ec 100644 --- a/slackme/slackme.sh +++ b/slackme/slackme.sh @@ -1,15 +1,14 @@ #!/usr/bin/env sh PROVIDER_ID=${PROVIDER_ID} -SLACK_MESSAGE=$( - cat << "EOF" +SLACK_MESSAGE=$(cat << "EOF" ${SLACK_MESSAGE} EOF ) SLACK_URL=$${SLACK_URL:-https://slack.com} usage() { - cat << EOF + cat < @@ -18,45 +17,45 @@ EOF } pretty_duration() { - local duration_ms=$1 - - # If the duration is less than 1 second, display in milliseconds - if [ $duration_ms -lt 1000 ]; then - echo "$${duration_ms}ms" - return - fi - - # Convert the duration to seconds - local duration_sec=$((duration_ms / 1000)) - local remaining_ms=$((duration_ms % 1000)) - - # If the duration is less than 1 minute, display in seconds (with ms) - if [ $duration_sec -lt 60 ]; then - echo "$${duration_sec}.$${remaining_ms}s" - return - fi - - # Convert the duration to minutes - local duration_min=$((duration_sec / 60)) - local remaining_sec=$((duration_sec % 60)) - - # If the duration is less than 1 hour, display in minutes and seconds - if [ $duration_min -lt 60 ]; then - echo "$${duration_min}m $${remaining_sec}.$${remaining_ms}s" - return - fi - - # Convert the duration to hours - local duration_hr=$((duration_min / 60)) - local remaining_min=$((duration_min % 60)) - - # Display in hours, minutes, and seconds - echo "$${duration_hr}hr $${remaining_min}m $${remaining_sec}.$${remaining_ms}s" + local duration_ms=$1 + + # If the duration is less than 1 second, display in milliseconds + if [ $duration_ms -lt 1000 ]; then + echo "$${duration_ms}ms" + return + fi + + # Convert the duration to seconds + local duration_sec=$((duration_ms / 1000)) + local remaining_ms=$((duration_ms % 1000)) + + # If the duration is less than 1 minute, display in seconds (with ms) + if [ $duration_sec -lt 60 ]; then + echo "$${duration_sec}.$${remaining_ms}s" + return + fi + + # Convert the duration to minutes + local duration_min=$((duration_sec / 60)) + local remaining_sec=$((duration_sec % 60)) + + # If the duration is less than 1 hour, display in minutes and seconds + if [ $duration_min -lt 60 ]; then + echo "$${duration_min}m $${remaining_sec}.$${remaining_ms}s" + return + fi + + # Convert the duration to hours + local duration_hr=$((duration_min / 60)) + local remaining_min=$((duration_min % 60)) + + # Display in hours, minutes, and seconds + echo "$${duration_hr}hr $${remaining_min}m $${remaining_sec}.$${remaining_ms}s" } if [ $# -eq 0 ]; then - usage - exit 1 + usage + exit 1 fi BOT_TOKEN=$(coder external-auth access-token $PROVIDER_ID) @@ -73,16 +72,16 @@ fi START=$(date +%s%N) # Run all arguments as a command -"$@" +$@ END=$(date +%s%N) -DURATION_MS=$${DURATION_MS:-$(((END - START) / 1000000))} +DURATION_MS=$${DURATION_MS:-$(( (END - START) / 1000000 ))} PRETTY_DURATION=$(pretty_duration $DURATION_MS) set -e -COMMAND=$("$@") +COMMAND=$(echo $@) SLACK_MESSAGE=$(echo "$SLACK_MESSAGE" | sed "s|\\$COMMAND|$COMMAND|g") SLACK_MESSAGE=$(echo "$SLACK_MESSAGE" | sed "s|\\$DURATION|$PRETTY_DURATION|g") curl --silent -o /dev/null --header "Authorization: Bearer $BOT_TOKEN" \ - -G --data-urlencode "text=$${SLACK_MESSAGE}" \ - "$SLACK_URL/api/chat.postMessage?channel=$USER_ID&pretty=1" + -G --data-urlencode "text=$${SLACK_MESSAGE}" \ + "$SLACK_URL/api/chat.postMessage?channel=$USER_ID&pretty=1"