Add exit handler and cleanup the code

This commit is contained in:
Aleksandar Puharic 2022-07-26 21:38:26 +02:00
parent f0786988ef
commit 8a8b5eb02e
Signed by: xZero707
GPG Key ID: 3CC53DCAA9C237BB

View File

@ -1,5 +1,19 @@
#!/usr/bin/with-contenv bash #!/usr/bin/with-contenv bash
# Register exit handler
trap scriptExitHandler EXIT
function scriptExitHandler() {
LAST_EXIT_CODE=$?
if [ "${LAST_EXIT_CODE}" = "0" ]; then
echo "> Script finished successfully"
exit "${LAST_EXIT_CODE}"
fi
echo "> Script finished with an error"
exit "${LAST_EXIT_CODE}"
}
# Check if plugin installed. This is very basic check that doesn't involve database # Check if plugin installed. This is very basic check that doesn't involve database
function isPluginInstalled() { function isPluginInstalled() {
if [ -d "${PLUGIN_PATH}" ] || [ -f "${PLUGIN_PATH}.php" ]; then if [ -d "${PLUGIN_PATH}" ] || [ -f "${PLUGIN_PATH}.php" ]; then
@ -14,6 +28,8 @@ function installPlugin() {
sleep 0.5 sleep 0.5
} }
# Main function
function main() {
PLUGIN_LIST="${WORDPRESS_PLUGIN_LIST:-}" PLUGIN_LIST="${WORDPRESS_PLUGIN_LIST:-}"
PLUGIN_STRICT_INSTALL="${WORDPRESS_PLUGIN_INSTALL_STRICT:-false}" PLUGIN_STRICT_INSTALL="${WORDPRESS_PLUGIN_INSTALL_STRICT:-false}"
WP_CONTENT_PATH="/var/www/${WEB_ROOT}/wp-content" WP_CONTENT_PATH="/var/www/${WEB_ROOT}/wp-content"
@ -21,7 +37,7 @@ WP_CONTENT_PATH="/var/www/${WEB_ROOT}/wp-content"
echo "> Automated WordPress Plugin Installer" echo "> Automated WordPress Plugin Installer"
if [ -z "${PLUGIN_LIST}" ]; then if [ -z "${PLUGIN_LIST}" ]; then
echo "> No plugins defined. Skipping installation." echo "> No plugins defined. Skipping installation."
exit 0 return 0
fi fi
echo "> About to install defined plugins" echo "> About to install defined plugins"
@ -71,7 +87,9 @@ echo "> Total of ${FAILED_COUNT} plugins failed to install"
if [ "${PLUGIN_STRICT_INSTALL}" = "true" ] && [ ${FAILED_COUNT} != "0" ]; then if [ "${PLUGIN_STRICT_INSTALL}" = "true" ] && [ ${FAILED_COUNT} != "0" ]; then
echo "> WORDPRESS_PLUGIN_INSTALL_STRICT is set to true. Terminating with non-zero exit code" echo "> WORDPRESS_PLUGIN_INSTALL_STRICT is set to true. Terminating with non-zero exit code"
exit 1 return 1
fi fi
}
exit 0 main "${@}"
exit $?