From cf713a16626447abf468d06e19997ca43ab02350 Mon Sep 17 00:00:00 2001 From: kobymeir Date: Thu, 5 Sep 2024 14:51:40 +0300 Subject: [PATCH] build content docs - take 6 --- .gitlab/helper_functions.sh | 81 +++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 .gitlab/helper_functions.sh diff --git a/.gitlab/helper_functions.sh b/.gitlab/helper_functions.sh new file mode 100644 index 000000000..2ccc08395 --- /dev/null +++ b/.gitlab/helper_functions.sh @@ -0,0 +1,81 @@ +#!/bin/bash + +CYAN="\e[0;36m" +CLEAR="\e[0m" +SECTION_START="\e[0Ksection_start:the_time:section_id\r\e[0K${CYAN}section_header${CLEAR}" +SECTION_END="\e[0Ksection_end:the_time:section_id\r\e[0K" +RED='\033[0;31m' +GREEN='\033[0;32m' +BLUE='\033[0;34m' +NC='\033[0m' + +section_start() { + local section_header section_id start + start="$SECTION_START" + if [[ "$#" -eq 1 ]]; then + section_header="$1" + section_id="$(echo "$1" | tr -c '[:alnum:]\n\r' '_')" + elif [[ "$#" -eq 2 ]]; then + if [[ "$2" =~ -{0,2}collapsed ]]; then + start="${start/section_id/section_id[collapsed=true]}" + section_header="$1" + section_id="$(echo "$1" | tr -c '[:alnum:]\n\r' '_')" + else + section_header="$2" + section_id="$1" + fi + elif [[ "$#" -eq 3 && "$3" =~ /^-{0,2}collapsed$/ ]]; then + start="${start/section_id/section_id[collapsed=true]}" + section_header="$2" + section_id="$1" + else + echo "section_start should be called with 1-3 args but it was called with $#" + echo "acceptable usages:" + echo " 1. section_start \"\" \"\"" + echo " 2. section_start \"\"" + echo " 3. section_start \"\" \"\" --collapse" + echo " 4. section_start \"\" --collapse" + echo "where is only alphanumeric characters and underscore and" + echo "--collapse indicates that you would like those log steps to be collapsed in the job log output by default" + exit 9 + fi + start_time=$(date +%s) + start="$(echo "$start" | sed -e "s/the_time/$start_time/" -e "s/section_id/$section_id/" -e "s/section_header/$section_header/")" + echo -e "$start" + date +"[%Y-%m-%dT%H:%M:%S.%3N] section start" +} + +section_end() { + local section_id end + date +"[%Y-%m-%dT%H:%M:%S.%3N] section end" + end="$SECTION_END" + if [[ "$#" -eq 1 ]]; then + section_id="$(echo "$1" | tr -c '[:alnum:]\n\r' '_')" + else + echo "section_end should be called with 1 arg but it was called with $#" + echo "acceptable usage:" + echo " 1. section_end \"\"" + echo " 2. section_start \"\"" + echo "where or is that of the section this marks the end of" + exit 9 + fi + end_time=$(date +%s) + end="$(echo "$end" | sed -e "s/the_time/$end_time/" -e "s/section_id/$section_id/")" + echo -e "$end" +} + +job-done() { + mkdir -p "${PIPELINE_JOBS_FOLDER}" + echo "creating file ${PIPELINE_JOBS_FOLDER}/${CI_JOB_NAME}.txt" + echo "done" > "${PIPELINE_JOBS_FOLDER}/${CI_JOB_NAME}.txt" + echo "finished writing to file ${PIPELINE_JOBS_FOLDER}/${CI_JOB_NAME}.txt" +} + +sleep-with-progress() { + local sleep_time=${1:-10} + local sleep_interval=${2:-1} + local sleep_message=${3:-"Sleeping... "} + local columns=${4:-$(tput cols)} + local sleep_step=$((sleep_time / sleep_interval)) + for ((i=0; i< sleep_step;i++)); do echo "${sleep_interval}";sleep "${sleep_interval}"; done | poetry run tqdm --total ${sleep_time} --unit seconds --leave --update --colour green -ncols ${columns} --desc "${sleep_message}" 1> /dev/null +}