From 6fbeffd21ec07940fd153b2e215da9e33e342c9b Mon Sep 17 00:00:00 2001 From: Llewellyn van der Merwe Date: Sun, 2 May 2021 23:45:27 +0200 Subject: [PATCH] Adds test option to repo. --- .github/workflows/test.yml | 27 ++++++++++++ .github/workflows/today.yml | 3 ++ src/today.sh | 84 ++++++++++++++++++++++++++++--------- 3 files changed, 94 insertions(+), 20 deletions(-) create mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 00000000..0b0f04b7 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,27 @@ +name: "Test Today's Scripture!" + +on: + # so we can manually run a test also + workflow_dispatch: + +jobs: + build: + runs-on: [ubuntu-20.04] + steps: + - name: Setup gitHub User Details + env: + GIT_USER: ${{ secrets.GIT_USER }} + GIT_EMAIL: ${{ secrets.GIT_EMAIL }} + GPG_USER: ${{ secrets.GPG_USER }} + GPG_KEY: ${{ secrets.GPG_KEY }} + SSH_KEY: ${{ secrets.SSH_KEY }} + SSH_PUB: ${{ secrets.SSH_PUB }} + run: | + /bin/bash <(/bin/curl -s https://raw.githubusercontent.com/vdm-io/github-user/master/src/setup.sh) --gpg-key "$GPG_KEY" --gpg-user "$GPG_USER" --ssh-key "$SSH_KEY" --ssh-pub "$SSH_PUB" --git-user "$GIT_USER" --git-email "$GIT_EMAIL" + - name: Clone Master Repositry + run: | + /bin/git clone git@github.com:trueChristian/daily-scripture.git daily_scripture + - name: Build the Daily Scripture + run: | + cd daily_scripture + /bin/bash ./src/today.sh --dry diff --git a/.github/workflows/today.yml b/.github/workflows/today.yml index 5d885025..1c7a5fb3 100644 --- a/.github/workflows/today.yml +++ b/.github/workflows/today.yml @@ -5,6 +5,9 @@ on: schedule: - cron: '7 22 * * *' + # so we can manually run a test also + workflow_dispatch: + jobs: build: runs-on: [ubuntu-20.04] diff --git a/src/today.sh b/src/today.sh index bed9ef33..1497a0f8 100755 --- a/src/today.sh +++ b/src/today.sh @@ -1,7 +1,33 @@ #!/bin/bash +# Do some prep work +command -v git >/dev/null 2>&1 || { + echo >&2 "We require git for this script to run, but it's not installed. Aborting." + exit 1 +} +command -v curl >/dev/null 2>&1 || { + echo >&2 "We require curl for this script to run, but it's not installed. Aborting." + exit 1 +} + +# global config options +DRYRUN=0 + +# check if we have options +while :; do + case $1 in + --dry) + DRYRUN=1 + ;; + *) # Default case: No more options, so break out of the loop. + break ;; + esac + shift +done + #██████████████████████████████████████████████████████████████ DATE TODAY ███ -TODAY=$(date '+%A %d-%B, %Y') +# must set the time to Namibian :) +TODAY=$(TZ="Africa/Windhoek" date '+%A %d-%B, %Y') #█████████████████████████████████████████████████████████████ SCRIPT PATH ███ DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" @@ -16,26 +42,37 @@ TMP="$DIR/.TMP" sort -R -k1 -b "${SORTED}" >"${TMP}" # get the first line VERSE=$(head -n 1 "${TMP}") -# remove the verse -sed -i -e '1,1d' "${TMP}" -# add to used verses -[ -f "${USED}" ] && echo "$VERSE" >>"${USED}" || echo "$VERSE" >"${USED}" +# test behaviour +if (("$DRYRUN" == 1)); then + echo "selected: $VERSE" +else + # remove the verse + sed -i -e '1,1d' "${TMP}" + # add to used verses + [ -f "${USED}" ] && echo "$VERSE" >>"${USED}" || echo "$VERSE" >"${USED}" +fi #█████████████████████████████████████████████████████ SIX MONTH RETENTION ███ -# count the number of verse in used file -LINES_NR=$(wc -l <"${USED}") -if [ "$LINES_NR" -gt 182 ]; then - # get the first line - VERSE_BACK=$(head -n 1 "${USED}") - # remove the verse - sed -i -e '1,1d' "${USED}" - # add add back to the pile - echo "$VERSE_BACK" >>"${TMP}" +# check test behaviour +if (("$DRYRUN" == 0)); then + # count the number of verse in used file + LINES_NR=$(wc -l <"${USED}") + if [ "$LINES_NR" -gt 182 ]; then + # get the first line + VERSE_BACK=$(head -n 1 "${USED}") + # remove the verse + sed -i -e '1,1d' "${USED}" + # add add back to the pile + echo "$VERSE_BACK" >>"${TMP}" + fi fi #███████████████████████████████████████████████████████████████ SORT BACK ███ -# store back for next time -sort -h -b -k1 "${TMP}" >"${SORTED}" +# check test behaviour +if (("$DRYRUN" == 0)); then + # store back for next time + sort -h -b -k1 "${TMP}" >"${SORTED}" +fi # remove the temp file rm -f "${TMP}" @@ -61,10 +98,17 @@ ${VERSES//$'\n'/ } #███████████████████████████████████████████████████████████████ SET FILES ███ -echo "${HTML}" >README.html -echo "${MARKDOWN}" >README.md +# check test behaviour +if (("$DRYRUN" == 1)); then + echo "${HTML}" + echo "----------------------------------------------------" + echo "${MARKDOWN}" +else + echo "${HTML}" >README.html + echo "${MARKDOWN}" >README.md -git commit -am"${TODAY}" -git push + git commit -am"${TODAY}" + git push +fi exit 0