From 49b1274135fb7c3822853fd2fca6d5f1a0f76ec5 Mon Sep 17 00:00:00 2001 From: Sven Jorns Date: Sat, 10 Jun 2023 14:46:36 +0300 Subject: [PATCH] with the help of ms code ussing shellcheck to optimize the code --- git2zola.sh | 159 ++++++++----------------- staticzolaserver-docker.code-workspace | 8 ++ 2 files changed, 57 insertions(+), 110 deletions(-) create mode 100644 staticzolaserver-docker.code-workspace diff --git a/git2zola.sh b/git2zola.sh index cf065d5..02aa10a 100755 --- a/git2zola.sh +++ b/git2zola.sh @@ -10,102 +10,41 @@ # ssh://gogs@gogs.serinty.net:22104/Serinty/serinty-website.git #GITCLONEURL=https://${GITUSER}:${GITPASSWD}@${GITSERVER}:${GITSERVERPORT}/${GITPROJECTUSER}/${GITPROJECTNAME}.git +# shellcheck source=./getOrCreateSshKeys.sh source getOrCreateSshKeys.sh -if [ -z $IDLE_TIME ] - then - # one day - IDLE_TIME=86400 -fi - -if [ -z ${SWSPORT} ] -then - # port over 1024 for normal user - SWSPORT=8970 -fi - -if [ -z ${GITPROJECTNAME} ] -then - # projectname of the git repository with the data and conf for zola - GITPROJECTNAME='serinty-website' -fi - -if [ -z ${GITTHEMENAME} ] -then - # projectname of the git repository with the theme for zola - GITTHEMENAME="serinty-zola-theme" -fi - -if [ -z ${THEME_NAME} ] -then - # the name of the theme, which is configed in the zola project, can be diffrent to the GITTHEMENAME - THEME_NAME=${GITTHEMENAME} -else - echo "THEME_NAME:${THEME_NAME}" -fi - -if [ -z ${OUTPUT_DIR} ] -then - # Zola outpu build and sws path - OUTPUT_DIR="${HOME}/${GITPROJECTNAME}/public" -fi - -if [ -z ${BASE_URL} ] -then - # Domain with root path of the url - if [ ${SWSPORT} eq 80 ] || [ ${SWSPORT} eq 433 ] - then - BASE_URL="http://docker.local}" - else - #the port is a part of the url, so it has to be defined in the BASE_URL - BASE_URL="http://docker.local:${SWSPORT}" - fi -fi - -if [ -z ${GITPROJECTUSER} ] -then - # User or Organisation name of the repository - GITPROJECTUSER='Serinty' -fi - -if [ -z ${GITSERVER} ] -then - # name or ip of the git server - GITSERVER='gogs.serinty.net' -fi - -if [ -z ${GITSERVERPORT} ] -then - # ssh port of the git server - GITSERVERPORT=22104 -fi - -if [ -z ${GITUSER} ] -then - # ssh git user - GITUSER='gogs' -fi - -if [ -z ${GITPASSWD} ] -then - # password of the https login, not used because https isn't used - GITPASSWD='gogspw' -fi - -if [ -z ${GITCLONEURL} ] -then - # url to clone zola data and config - GITCLONEURL="ssh://${GITUSER}@${GITSERVER}:${GITSERVERPORT}/${GITPROJECTUSER}/${GITPROJECTNAME}.git" -fi - -if [ -z ${GITTHEMECLONEURL} ] -then - # url to clone zola data and config - GITTHEMECLONEURL="ssh://${GITUSER}@${GITSERVER}:${GITSERVERPORT}/${GITPROJECTUSER}/${GITTHEMENAME}.git" - -fi +# Set default values for variables +# one day +IDLE_TIME=${IDLE_TIME:-86400} +# port over 1024 for normal user +SWSPORT=${SWSPORT:=8970} +# projectname of the git repository with the data and conf for zola +GITPROJECTNAME=${GITPROJECTNAME:-serinty-website} +# projectname of the git repository with the theme for zola +GITTHEMENAME=${GITTHEMENAME:-serinty-zola-theme} +# the name of the theme, which is configed in the zola project, can be diffrent to the GITTHEMENAME +THEME_NAME=${THEME_NAME:-$GITTHEMENAME} +# User or Organisation name of the repository +GITPROJECTUSER=${GITPROJECTUSER:-Serinty} +# name or ip of the git server +GITSERVER=${GITSERVER:-gogs.serinty.net} +# ssh port of the git server +GITSERVERPORT=${GITSERVERPORT:-22104} +# ssh git user +GITUSER=${GITUSER:-gogs} +# password of the https login, not used because https isn't used +GITPASSWD=${GITPASSWD:-gogspw} +# url to clone zola data and config +GITCLONEURL=${GITCLONEURL:-"ssh://$GITUSER@$GITSERVER:$GITSERVERPORT/$GITPROJECTUSER/$GITPROJECTNAME.git"} +# url to clone zola data and config +GITTHEMECLONEURL=${GITTHEMECLONEURL:-"ssh://$GITUSER@$GITSERVER:$GITSERVERPORT/$GITPROJECTUSER/$GITTHEMENAME.git"} +BASE_URL=${BASE_URL:-"http://docker.local:${SWSPORT}"} +# Zola output build and sws path +OUTPUT_DIR=${OUTPUT_DIR:-$HOME/$GITPROJECTNAME/public} + +# set trap to exit this process # at this time only exit this process if it get a exit code trap "exit" EXIT @@ -117,8 +56,8 @@ startSWSifNotRunning () { pgrep -x "static-web-server" else echo "starting webserver on port ${SWSPORT} with content:" - ls -la ${OUTPUT_DIR} \ - && static-web-server --port ${SWSPORT} --log-level warn --root ${OUTPUT_DIR} & + ls -la "${OUTPUT_DIR}" \ + && static-web-server --port "${SWSPORT}" --log-level warn --root "${OUTPUT_DIR}" & fi } @@ -127,7 +66,7 @@ pullAndBuild () { echo "getting new data" git pull --ff-only echo "build website with newer data to ${OUTPUT_DIR}" - zola build --force --base-url ${BASE_URL} --output-dir ${OUTPUT_DIR} + zola build --force --base-url "${BASE_URL}" --output-dir "${OUTPUT_DIR}" } # clone or pull the theme @@ -138,20 +77,20 @@ getTheme () { mkdir themes fi echo "change into themes directory" - cd ./themes - if [ ! -d ./${THEME_NAME} ] + cd ./themes || exit 3 + if [ ! -d ./"${THEME_NAME}" ] then echo "clone theme with ${GITTHEMECLONEURL} ${THEME_NAME}" - git clone ${GITTHEMECLONEURL} ${THEME_NAME} + git clone "${GITTHEMECLONEURL} ${THEME_NAME}" fi - if [ -d ./${THEME_NAME} ] + if [ -d ./"${THEME_NAME}" ] then echo "change into theme directory ${THEME_NAME}" - cd ./${THEME_NAME} + cd ./"${THEME_NAME}" || exit 4 gitpull=$(git pull --dry-run --ff-only 2>&1) gitwords=0 - gitwords=$(echo $gitpull | wc -w) - if [ ${gitwords} -gt 0 ] + gitwords=$(echo "$gitpull" | wc -w) + if [ "${gitwords}" -gt 0 ] then echo "getting new design" git pull --ff-only @@ -162,12 +101,12 @@ getTheme () { echo "no theme ${THEME_NAME} found" fi echo "change into project directory ${HOME}/${GITPROJECTNAME}" - cd ${HOME}/${GITPROJECTNAME} + cd "${HOME}/${GITPROJECTNAME}" || exit 5 } getOrCreateSshKeys echo "put git server ${GITSERVER} to known hosts" -ssh-keyscan -p ${GITSERVERPORT} -H ${GITSERVER} >> ~/.ssh/known_hosts +ssh-keyscan -p "${GITSERVERPORT}" -H "${GITSERVER}" >> ~/.ssh/known_hosts zola --version while true do @@ -179,9 +118,9 @@ do if [ ${gitstatus} -eq 128 ] then echo "no data, cloning from ${GITCLONEURL}" - git clone ${GITCLONEURL} + git clone "${GITCLONEURL}" echo "change into project path ${GITPROJECTNAME}" - cd ${GITPROJECTNAME} + cd "${GITPROJECTNAME}" || exit 6 getTheme pullAndBuild echo "checking git status" @@ -195,15 +134,15 @@ do # all outputs from git goes to error output gitpull=$(git pull --dry-run --ff-only 2>&1) gitwords=0 - gitwords=$(echo $gitpull | wc -w) - if [ ${gitwords} -gt 0 ] + gitwords=$(echo "$gitpull" | wc -w) + if [ "${gitwords}" -gt 0 ] then getTheme pullAndBuild fi fi - if [ -d ~/${GITPROJECTNAME} ] + if [ -d ~/"${GITPROJECTNAME}" ] then startSWSifNotRunning else @@ -215,7 +154,7 @@ do exit fi - sleep ${IDLE_TIME} + sleep "${IDLE_TIME}" done diff --git a/staticzolaserver-docker.code-workspace b/staticzolaserver-docker.code-workspace new file mode 100644 index 0000000..876a149 --- /dev/null +++ b/staticzolaserver-docker.code-workspace @@ -0,0 +1,8 @@ +{ + "folders": [ + { + "path": "." + } + ], + "settings": {} +} \ No newline at end of file