Use local variables

This commit is contained in:
Aleksandar Puharic 2024-05-19 21:36:57 +02:00
parent 40dd1e2987
commit aa5e378f30
Signed by: xZero707
GPG Key ID: 3CC53DCAA9C237BB
4 changed files with 80 additions and 91 deletions

View File

@ -1,15 +1,15 @@
#!/usr/bin/env bash #!/usr/bin/env bash
function main() { function main() {
FULL_CHECK="${1:-false}" local fullCheck="${1:-false}"
if [ -f "/tmp/.wp-unhealthy" ]; then if [ -f "/tmp/.wp-unhealthy" ]; then
echo "Error: WordPress health is compromised: $(cat "/tmp/.wp-unhealthy")" echo "Error: WordPress health is compromised: $(cat "/tmp/.wp-unhealthy")"
return 1 return 1
fi fi
# If FULL_CHECK is not requested, site functionality check with curl is skipped # If fullCheck is not requested, site functionality check with curl is skipped
if [ "${FULL_CHECK}" = "false" ]; then if [ "${fullCheck}" = "false" ]; then
return 0 return 0
fi fi
@ -18,4 +18,4 @@ function main() {
} }
main "${@}" main "${@}"
exit $? exit $?

View File

@ -3,29 +3,29 @@
set -e set -e
# Applies patch for preventing WordPress updates # Applies patch for preventing WordPress updates
function main() { main() {
PATCH_FILE="${1:?PATCH_FILE is required}" local patchFile="${1:?PATCH_FILE is required}"
TARGET_FILE="${2:?TARGET_FILE is required}" local targetFile="${2:?TARGET_FILE is required}"
if [ ! -f "${PATCH_FILE}" ]; then if [ ! -f "${patchFile}" ]; then
echo "> No such file [PATCH]: ${PATCH_FILE}" echo "> No such file [PATCH]: ${patchFile}"
return 1 return 1
fi fi
if [ ! -f "${TARGET_FILE}" ]; then if [ ! -f "${targetFile}" ]; then
echo "> No such file [TARGET]: ${TARGET_FILE}" echo "> No such file [TARGET]: ${targetFile}"
return 1 return 1
fi fi
echo "> Loading patch ${PATCH_FILE}" echo "> Loading patch ${patchFile}"
echo " Patching '${TARGET_FILE}'..." echo " Patching '${targetFile}'..."
patch --verbose "${TARGET_FILE}" <"${PATCH_FILE}" patch --verbose "${targetFile}" <"${patchFile}"
MARK_READ_ONLY="${3:-true}" local markReadOnly="${3:-true}"
if [ "${MARK_READ_ONLY}" = "true" ]; then if [ "${markReadOnly}" = "true" ]; then
# This is done in order to prevent WordPress overwriting the file # This is done in order to prevent WordPress overwriting the file
echo " Marking the patched file read-only..." echo " Marking the patched file read-only..."
chmod 0440 "${TARGET_FILE}" chmod 0440 "${targetFile}"
fi fi
return 0 return 0

View File

@ -5,17 +5,16 @@
# $2 - plugin version (optional) # $2 - plugin version (optional)
# Returns 0 on success, X on failure # Returns 0 on success, X on failure
download() { download() {
PLUGIN_SLUG="${1:?download: PLUGIN_SLUG is required}" local pluginSlug="${1:?download: PLUGIN_SLUG is required}"
PLUGIN_VERSION="${2:-}" local pluginVersion="${2:-}"
local pluginFilename="${pluginSlug}.zip"
if [ -n "${PLUGIN_VERSION}" ]; then if [ -n "${pluginVersion}" ]; then
PLUGIN_FILENAME="${PLUGIN_SLUG}.${PLUGIN_VERSION}.zip" pluginFilename="${pluginSlug}.${pluginVersion}.zip"
else
PLUGIN_FILENAME="${PLUGIN_SLUG}.zip"
fi fi
curl --fail -gsO "https://downloads.wordpress.org/plugin/${PLUGIN_FILENAME}" || return $? curl --fail -gsO "https://downloads.wordpress.org/plugin/${pluginFilename}" || return $?
echo "${PLUGIN_FILENAME}" echo "${pluginFilename}"
return 0 return 0
} }
@ -25,19 +24,18 @@ download() {
# $3 - target directory (optional) # $3 - target directory (optional)
# Returns 0 on success, 1 on failure # Returns 0 on success, 1 on failure
unpack() { unpack() {
TARGET_PLUGINS_DIR="${TARGET_PLUGINS_DIR:?check: TARGET_PLUGINS_DIR is required}" local pluginsDir="${TARGET_PLUGINS_DIR:?check: TARGET_PLUGINS_DIR is required}"
PLUGIN_SLUG="${1:?unpack: PLUGIN_SLUG is required}" local pluginSlug="${1:?unpack: PLUGIN_SLUG is required}"
PLUGIN_VERSION="${2:-}" local pluginVersion="${2:-}"
local pluginFilename="${pluginSlug}.zip"
if [ -n "${PLUGIN_VERSION}" ]; then if [ -n "${pluginVersion}" ]; then
PLUGIN_FILENAME="${PLUGIN_SLUG}.${PLUGIN_VERSION}.zip" pluginFilename="${pluginSlug}.${pluginVersion}.zip"
else
PLUGIN_FILENAME="${PLUGIN_SLUG}.zip"
fi fi
if [ -f "${PLUGIN_FILENAME}" ]; then if [ -f "${pluginFilename}" ]; then
unzip -qq -d "${TARGET_PLUGINS_DIR}" "${PLUGIN_FILENAME}" || return $? unzip -qq -d "${pluginsDir}" "${pluginFilename}" || return $?
rm "${PLUGIN_FILENAME}" -f rm "${pluginFilename}" -f
return 0 return 0
fi fi
return 1 return 1
@ -47,23 +45,22 @@ unpack() {
# $1 - plugin slug # $1 - plugin slug
# Returns 0 if plugin is installed, 1 otherwise # Returns 0 if plugin is installed, 1 otherwise
check() { check() {
TARGET_PLUGINS_DIR="${TARGET_PLUGINS_DIR:?check: TARGET_PLUGINS_DIR is required}" local pluginsDir="${TARGET_PLUGINS_DIR:?check: TARGET_PLUGINS_DIR is required}"
PLUGIN_SLUG="${1:?check: PLUGIN_SLUG is required}" local pluginSlug="${1:?check: PLUGIN_SLUG is required}"
local pluginPath="${pluginsDir}/${pluginSlug}"
PLUGIN_PATH="${TARGET_PLUGINS_DIR}/${PLUGIN_SLUG}"
# Check if plugin directory exists # Check if plugin directory exists
if [ ! -d "${PLUGIN_PATH}" ]; then if [ ! -d "${pluginPath}" ]; then
return 1 return 1
fi fi
# Check if plugin file exists - if yes, plugin is probably installed successfully # Check if plugin file exists - if yes, plugin is probably installed successfully
if [ -f "${PLUGIN_PATH}/${PLUGIN_SLUG}.php" ]; then if [ -f "${pluginPath}/${pluginSlug}.php" ]; then
return 0 return 0
fi fi
# Check if plugin directory is empty - if not, plugin is probably installed successfully # Check if plugin directory is empty - if not, plugin is probably installed successfully
if [ "$(ls -A "${PLUGIN_PATH}")" ]; then if [ "$(ls -A "${pluginPath}")" ]; then
return 0 return 0
fi fi
@ -75,29 +72,28 @@ check() {
# $1 - plugin slug # $1 - plugin slug
# Returns 0 on success, X on failure # Returns 0 on success, X on failure
delete() { delete() {
TARGET_PLUGINS_DIR="${TARGET_PLUGINS_DIR:?delete: TARGET_PLUGINS_DIR is required}" local pluginsDir="${TARGET_PLUGINS_DIR:?delete: TARGET_PLUGINS_DIR is required}"
PLUGIN_SLUG="${1:?delete: PLUGIN_SLUG is required}" local pluginSlug="${1:?delete: PLUGIN_SLUG is required}"
local pluginPath="${pluginsDir}/${pluginSlug}"
PLUGIN_PATH="${TARGET_PLUGINS_DIR}/${PLUGIN_SLUG}"
# Check if plugin directory exists # Check if plugin directory exists
if [ ! -d "${PLUGIN_PATH}" ]; then if [ ! -d "${pluginPath}" ]; then
return 1 return 1
fi fi
rm -rf "${PLUGIN_PATH}" rm -rf "${pluginPath}"
return $? return $?
} }
# Main function # Main function
main() { main() {
COMMAND="${1:?COMMAND is required}"
TARGET_PLUGINS_DIR="${WP_PLUGINS_PATH:?WP_PLUGINS_PATH is required}" TARGET_PLUGINS_DIR="${WP_PLUGINS_PATH:?WP_PLUGINS_PATH is required}"
export TARGET_PLUGINS_DIR export TARGET_PLUGINS_DIR
local action="${1:?ACTION is required}"
# Execute command by calling function with the same name # Execute command by calling function with the same name
case "${COMMAND}" in case "${action}" in
download) download)
download "${@:2}" download "${@:2}"
return $? return $?
@ -115,11 +111,10 @@ main() {
return $? return $?
;; ;;
*) *)
echo "Error: Unknown command '${COMMAND}'" echo "Error: Unknown action '${action}'"
return 1 return 1
;; ;;
esac esac
} }
main "${@}" main "${@}"
exit $?

View File

@ -5,17 +5,16 @@
# $2 - theme version (optional) # $2 - theme version (optional)
# Returns 0 on success, X on failure # Returns 0 on success, X on failure
download() { download() {
THEME_SLUG="${1:?download: THEME_SLUG is required}" local themeSlug="${1:?download: THEME_SLUG is required}"
THEME_VERSION="${2:-}" local themeVersion="${2:-}"
local themeFilename="${themeSlug}.zip"
if [ -n "${THEME_VERSION}" ]; then if [ -n "${themeVersion}" ]; then
THEME_FILENAME="${THEME_SLUG}.${THEME_VERSION}.zip" themeFilename="${themeSlug}.${themeVersion}.zip"
else
THEME_FILENAME="${THEME_SLUG}.zip"
fi fi
curl --fail -gsO "https://downloads.wordpress.org/theme/${THEME_FILENAME}" || return $? curl --fail -gsO "https://downloads.wordpress.org/theme/${themeFilename}" || return $?
echo "${THEME_FILENAME}" echo "${themeFilename}"
return 0 return 0
} }
@ -24,19 +23,18 @@ download() {
# $2 - theme version (optional) # $2 - theme version (optional)
# Returns 0 on success, 1 on failure # Returns 0 on success, 1 on failure
unpack() { unpack() {
TARGET_THEMES_DIR="${TARGET_THEMES_DIR:?check: TARGET_THEMES_DIR is required}" local themesDir="${TARGET_THEMES_DIR:?check: TARGET_THEMES_DIR is required}"
THEME_SLUG="${1:?unpack: THEME_SLUG is required}" local themeSlug="${1:?unpack: THEME_SLUG is required}"
THEME_VERSION="${2:-}" local themeVersion="${2:-}"
local themeFilename="${themeSlug}.zip"
if [ -n "${THEME_VERSION}" ]; then if [ -n "${themeVersion}" ]; then
THEME_FILENAME="${THEME_SLUG}.${THEME_VERSION}.zip" themeFilename="${themeSlug}.${themeVersion}.zip"
else
THEME_FILENAME="${THEME_SLUG}.zip"
fi fi
if [ -f "${THEME_FILENAME}" ]; then if [ -f "${themeFilename}" ]; then
unzip -qq -d "${TARGET_THEMES_DIR}" "${THEME_FILENAME}" || return $? unzip -qq -d "${themesDir}" "${themeFilename}" || return $?
rm "${THEME_FILENAME}" -f rm "${themeFilename}" -f
return 0 return 0
fi fi
return 1 return 1
@ -46,28 +44,27 @@ unpack() {
# $1 - theme slug # $1 - theme slug
# Returns 0 if theme is installed, 1 otherwise # Returns 0 if theme is installed, 1 otherwise
check() { check() {
TARGET_THEMES_DIR="${TARGET_THEMES_DIR:?check: TARGET_THEMES_DIR is required}" local themesDir="${TARGET_THEMES_DIR:?check: TARGET_THEMES_DIR is required}"
THEME_SLUG="${1:?check: THEME_SLUG is required}" local themeSlug="${1:?check: THEME_SLUG is required}"
local themePath="${themesDir}/${themeSlug}"
THEME_PATH="${TARGET_THEMES_DIR}/${THEME_SLUG}"
# Check if theme directory exists # Check if theme directory exists
if [ ! -d "${THEME_PATH}" ]; then if [ ! -d "${themePath}" ]; then
return 1 return 1
fi fi
# Check if theme theme.json exists - if yes, theme is probably installed successfully # Check if theme theme.json exists - if yes, theme is probably installed successfully
if [ -f "${THEME_PATH}/theme.json" ]; then if [ -f "${themePath}/theme.json" ]; then
return 0 return 0
fi fi
# Check if theme index.php exists - if yes, theme is probably installed successfully # Check if theme index.php exists - if yes, theme is probably installed successfully
if [ -f "${THEME_PATH}/index.php" ]; then if [ -f "${themePath}/index.php" ]; then
return 0 return 0
fi fi
# Check if theme directory is empty - if not, theme is probably installed successfully # Check if theme directory is empty - if not, theme is probably installed successfully
if [ "$(ls -A "${THEME_PATH}")" ]; then if [ "$(ls -A "${themePath}")" ]; then
return 0 return 0
fi fi
@ -79,30 +76,28 @@ check() {
# $1 - theme slug # $1 - theme slug
# Returns 0 on success, X on failure # Returns 0 on success, X on failure
delete() { delete() {
TARGET_THEMES_DIR="${TARGET_THEMES_DIR:?delete: TARGET_THEMES_DIR is required}" local themesDir="${TARGET_THEMES_DIR:?delete: TARGET_THEMES_DIR is required}"
THEME_SLUG="${1:?delete: THEME_SLUG is required}" local themeSlug="${1:?delete: THEME_SLUG is required}"
local themePath="${themesDir}/${themeSlug}"
THEME_PATH="${TARGET_THEMES_DIR}/${THEME_SLUG}"
# Check if theme directory exists # Check if theme directory exists
if [ ! -d "${THEME_PATH}" ]; then if [ ! -d "${themePath}" ]; then
return 1 return 1
fi fi
rm -rf "${THEME_PATH}" rm -rf "${themePath}"
return $? return $?
} }
# Main function # Main function
main() { main() {
COMMAND="${1:?COMMAND is required}"
TARGET_THEMES_DIR="${WP_THEMES_PATH:?WP_THEMES_PATH is required}" TARGET_THEMES_DIR="${WP_THEMES_PATH:?WP_THEMES_PATH is required}"
export TARGET_THEMES_DIR export TARGET_THEMES_DIR
local action="${1:?ACTION is required}"
# Execute command by calling function with the same name # Execute command by calling function with the same name
case "${COMMAND}" in case "${action}" in
download) download)
download "${@:2}" download "${@:2}"
return $? return $?
@ -120,11 +115,10 @@ main() {
return $? return $?
;; ;;
*) *)
echo "Error: Unknown command '${COMMAND}'" echo "Error: Unknown action '${action}'"
return 1 return 1
;; ;;
esac esac
} }
main "${@}" main "${@}"
exit $?