From 3a3ec7d9b5329967f64a0fda4dc1c8f2b289d85b Mon Sep 17 00:00:00 2001 From: Maniack Crudelis Date: Sun, 17 Dec 2017 20:26:52 +0100 Subject: [PATCH] [fix/enh] Use local variables --- data/helpers.d/backend | 10 +++++----- data/helpers.d/filesystem | 10 +++++----- data/helpers.d/ip | 10 +++++----- data/helpers.d/mysql | 6 +++--- data/helpers.d/network | 4 ++-- data/helpers.d/package | 18 +++++++++--------- data/helpers.d/print | 4 ++-- data/helpers.d/string | 12 ++++++------ data/helpers.d/system | 2 +- data/helpers.d/user | 4 ++-- data/helpers.d/utils | 29 +++++++++++++++-------------- 11 files changed, 55 insertions(+), 54 deletions(-) diff --git a/data/helpers.d/backend b/data/helpers.d/backend index c32ef02ac..8fef412cf 100644 --- a/data/helpers.d/backend +++ b/data/helpers.d/backend @@ -22,12 +22,12 @@ ynh_use_logrotate () { fi if [ $# -gt 0 ]; then if [ "$(echo ${1##*.})" == "log" ]; then # Keep only the extension to check if it's a logfile - logfile=$1 # In this case, focus logrotate on the logfile + local logfile=$1 # In this case, focus logrotate on the logfile else - logfile=$1/*.log # Else, uses the directory and all logfile into it. + local logfile=$1/*.log # Else, uses the directory and all logfile into it. fi else - logfile="/var/log/${app}/*.log" # Without argument, use a defaut directory in /var/log + local logfile="/var/log/${app}/*.log" # Without argument, use a defaut directory in /var/log fi cat > ./${app}-logrotate << EOF # Build a config file for logrotate $logfile { @@ -97,7 +97,7 @@ ynh_add_systemd_config () { # # usage: ynh_remove_systemd_config ynh_remove_systemd_config () { - finalsystemdconf="/etc/systemd/system/$app.service" + local finalsystemdconf="/etc/systemd/system/$app.service" if [ -e "$finalsystemdconf" ]; then sudo systemctl stop $app sudo systemctl disable $app @@ -124,7 +124,7 @@ ynh_add_nginx_config () { # Substitute in a nginx config file only if the variable is not empty if test -n "${path_url:-}"; then # path_url_slash_less is path_url, or a blank value if path_url is only '/' - path_url_slash_less=${path_url%/} + local path_url_slash_less=${path_url%/} ynh_replace_string "__PATH__/" "$path_url_slash_less/" "$finalnginxconf" ynh_replace_string "__PATH__" "$path_url" "$finalnginxconf" fi diff --git a/data/helpers.d/filesystem b/data/helpers.d/filesystem index 6fb073e06..6361d278e 100644 --- a/data/helpers.d/filesystem +++ b/data/helpers.d/filesystem @@ -180,12 +180,12 @@ ynh_restore_file () { local ARCHIVE_PATH="$YNH_CWD${ORIGIN_PATH}" # Default value for DEST_PATH = /$ORIGIN_PATH local DEST_PATH="${2:-$ORIGIN_PATH}" - + # If ARCHIVE_PATH doesn't exist, search for a corresponding path in CSV if [ ! -d "$ARCHIVE_PATH" ] && [ ! -f "$ARCHIVE_PATH" ] && [ ! -L "$ARCHIVE_PATH" ]; then ARCHIVE_PATH="$YNH_BACKUP_DIR/$(_get_archive_path \"$ORIGIN_PATH\")" fi - + # Restore ORIGIN_PATH into DEST_PATH mkdir -p $(dirname "$DEST_PATH") @@ -258,7 +258,7 @@ ynh_backup_if_checksum_is_different () { then # Proceed only if a value was stored into the app settings if ! echo "$checksum_value $file" | sudo md5sum -c --status then # If the checksum is now different - backup_file="/home/yunohost.conf/backup/$file.backup.$(date '+%Y%m%d.%H%M%S')" + local backup_file="/home/yunohost.conf/backup/$file.backup.$(date '+%Y%m%d.%H%M%S')" sudo mkdir -p "$(dirname "$backup_file")" sudo cp -a "$file" "$backup_file" # Backup the current file echo "File $file has been manually modified since the installation or last upgrade. So it has been duplicated in $backup_file" >&2 @@ -272,8 +272,8 @@ ynh_backup_if_checksum_is_different () { # usage: ynh_secure_remove path_to_remove # | arg: path_to_remove - File or directory to remove ynh_secure_remove () { - path_to_remove=$1 - forbidden_path=" \ + local path_to_remove=$1 + local forbidden_path=" \ /var/www \ /home/yunohost.app" diff --git a/data/helpers.d/ip b/data/helpers.d/ip index cb507b35a..874675c9d 100644 --- a/data/helpers.d/ip +++ b/data/helpers.d/ip @@ -8,12 +8,12 @@ ynh_validate_ip() { # http://stackoverflow.com/questions/319279/how-to-validate-ip-address-in-python#319298 - - IP_ADDRESS_FAMILY=$1 - IP_ADDRESS=$2 - + + local IP_ADDRESS_FAMILY=$1 + local IP_ADDRESS=$2 + [ "$IP_ADDRESS_FAMILY" == "4" ] || [ "$IP_ADDRESS_FAMILY" == "6" ] || return 1 - + python /dev/stdin << EOF import socket import sys diff --git a/data/helpers.d/mysql b/data/helpers.d/mysql index 42c204f95..56741ec0e 100644 --- a/data/helpers.d/mysql +++ b/data/helpers.d/mysql @@ -40,9 +40,9 @@ ynh_mysql_execute_file_as_root() { # | arg: user - the user to grant privilegies # | arg: pwd - the password to identify user by ynh_mysql_create_db() { - db=$1 + local db=$1 - sql="CREATE DATABASE ${db};" + local sql="CREATE DATABASE ${db};" # grant all privilegies to user if [[ $# -gt 1 ]]; then @@ -159,6 +159,6 @@ ynh_mysql_remove_db () { # | arg: name - name to correct/sanitize # | ret: the corrected name ynh_sanitize_dbid () { - dbid=${1//[-.]/_} # We should avoid having - and . in the name of databases. They are replaced by _ + local dbid=${1//[-.]/_} # We should avoid having - and . in the name of databases. They are replaced by _ echo $dbid } diff --git a/data/helpers.d/network b/data/helpers.d/network index c6764c1f5..f9e37e6cc 100644 --- a/data/helpers.d/network +++ b/data/helpers.d/network @@ -11,7 +11,7 @@ # usage: ynh_normalize_url_path path_to_normalize # | arg: url_path_to_normalize - URL path to normalize before using it ynh_normalize_url_path () { - path_url=$1 + local path_url=$1 test -n "$path_url" || ynh_die "ynh_normalize_url_path expect a URL path as first argument and received nothing." if [ "${path_url:0:1}" != "/" ]; then # If the first character is not a / path_url="/$path_url" # Add / at begin of path variable @@ -29,7 +29,7 @@ ynh_normalize_url_path () { # usage: ynh_find_port begin_port # | arg: begin_port - port to start to search ynh_find_port () { - port=$1 + local port=$1 test -n "$port" || ynh_die "The argument of ynh_find_port must be a valid port." while netcat -z 127.0.0.1 $port # Check if the port is free do diff --git a/data/helpers.d/package b/data/helpers.d/package index f28691579..ccb0c44d0 100644 --- a/data/helpers.d/package +++ b/data/helpers.d/package @@ -80,15 +80,15 @@ ynh_package_autopurge() { # usage: ynh_package_install_from_equivs controlfile # | arg: controlfile - path of the equivs control file ynh_package_install_from_equivs () { - controlfile=$1 + local controlfile=$1 # Check if the equivs package is installed. Or install it. ynh_package_is_installed 'equivs' \ || ynh_package_install equivs # retrieve package information - pkgname=$(grep '^Package: ' $controlfile | cut -d' ' -f 2) # Retrieve the name of the debian package - pkgversion=$(grep '^Version: ' $controlfile | cut -d' ' -f 2) # And its version number + local pkgname=$(grep '^Package: ' $controlfile | cut -d' ' -f 2) # Retrieve the name of the debian package + local pkgversion=$(grep '^Version: ' $controlfile | cut -d' ' -f 2) # And its version number [[ -z "$pkgname" || -z "$pkgversion" ]] \ && echo "Invalid control file" && exit 1 # Check if this 2 variables aren't empty. @@ -96,7 +96,7 @@ ynh_package_install_from_equivs () { ynh_package_update # Build and install the package - TMPDIR=$(mktemp -d) + local TMPDIR=$(mktemp -d) # Note that the cd executes into a sub shell # Create a fake deb package with equivs-build and the given control file # Install the fake package without its dependencies with dpkg @@ -118,13 +118,13 @@ ynh_package_install_from_equivs () { # usage: ynh_install_app_dependencies dep [dep [...]] # | arg: dep - the package name to install in dependence ynh_install_app_dependencies () { - dependencies=$@ - manifest_path="../manifest.json" + local dependencies=$@ + local manifest_path="../manifest.json" if [ ! -e "$manifest_path" ]; then manifest_path="../settings/manifest.json" # Into the restore script, the manifest is not at the same place fi - version=$(grep '\"version\": ' "$manifest_path" | cut -d '"' -f 4) # Retrieve the version number in the manifest file. - dep_app=${app//_/-} # Replace all '_' by '-' + local version=$(grep '\"version\": ' "$manifest_path" | cut -d '"' -f 4) # Retrieve the version number in the manifest file. + local dep_app=${app//_/-} # Replace all '_' by '-' cat > /tmp/${dep_app}-ynh-deps.control << EOF # Make a control file for equivs-build Section: misc @@ -148,6 +148,6 @@ EOF # # usage: ynh_remove_app_dependencies ynh_remove_app_dependencies () { - dep_app=${app//_/-} # Replace all '_' by '-' + local dep_app=${app//_/-} # Replace all '_' by '-' ynh_package_autopurge ${dep_app}-ynh-deps # Remove the fake package and its dependencies if they not still used. } diff --git a/data/helpers.d/print b/data/helpers.d/print index 36f4a120e..d9c8f1ec4 100644 --- a/data/helpers.d/print +++ b/data/helpers.d/print @@ -10,10 +10,10 @@ ynh_die() { # Simply duplicate the log, execute the yunohost command and replace the log without the result of this command # It's a very badly hack... ynh_no_log() { - ynh_cli_log=/var/log/yunohost/yunohost-cli.log + local ynh_cli_log=/var/log/yunohost/yunohost-cli.log sudo cp -a ${ynh_cli_log} ${ynh_cli_log}-move eval $@ - exit_code=$? + local exit_code=$? sudo mv ${ynh_cli_log}-move ${ynh_cli_log} return $? } diff --git a/data/helpers.d/string b/data/helpers.d/string index fbf598738..80fae8cf7 100644 --- a/data/helpers.d/string +++ b/data/helpers.d/string @@ -26,13 +26,13 @@ ynh_replace_string () { local replace_string=$2 local workfile=$3 - # Escape any backslash to preserve them as simple backslash. - match_string=${match_string//\\/"\\\\"} - replace_string=${replace_string//\\/"\\\\"} + # Escape any backslash to preserve them as simple backslash. + match_string=${match_string//\\/"\\\\"} + replace_string=${replace_string//\\/"\\\\"} - # Escape the & character, who has a special function in sed. - match_string=${match_string//&/"\&"} - replace_string=${replace_string//&/"\&"} + # Escape the & character, who has a special function in sed. + match_string=${match_string//&/"\&"} + replace_string=${replace_string//&/"\&"} # Escape the delimiter if it's in the string. match_string=${match_string//${delimit}/"\\${delimit}"} diff --git a/data/helpers.d/system b/data/helpers.d/system index 5f2ad385b..4bb941b7d 100644 --- a/data/helpers.d/system +++ b/data/helpers.d/system @@ -14,7 +14,7 @@ # Usage: ynh_exit_properly is used only by the helper ynh_abort_if_errors. # You must not use it directly. ynh_exit_properly () { - exit_code=$? + local exit_code=$? if [ "$exit_code" -eq 0 ]; then exit 0 # Exit without error if the script ended correctly fi diff --git a/data/helpers.d/user b/data/helpers.d/user index 0bb0736af..8e214691c 100644 --- a/data/helpers.d/user +++ b/data/helpers.d/user @@ -48,9 +48,9 @@ ynh_system_user_create () { if ! ynh_system_user_exists "$1" # Check if the user exists on the system then # If the user doesn't exist if [ $# -ge 2 ]; then # If a home dir is mentioned - user_home_dir="-d $2" + local user_home_dir="-d $2" else - user_home_dir="--no-create-home" + local user_home_dir="--no-create-home" fi sudo useradd $user_home_dir --system --user-group $1 --shell /usr/sbin/nologin || ynh_die "Unable to create $1 system account" fi diff --git a/data/helpers.d/utils b/data/helpers.d/utils index 2cb18c5c0..030fddcde 100644 --- a/data/helpers.d/utils +++ b/data/helpers.d/utils @@ -5,9 +5,9 @@ # usage: ynh_get_plain_key key [subkey [subsubkey ...]] # | ret: string - the key's value ynh_get_plain_key() { - prefix="#" - founded=0 - key=$1 + local prefix="#" + local founded=0 + local key=$1 shift while read line; do if [[ "$founded" == "1" ]] ; then @@ -36,7 +36,7 @@ ynh_get_plain_key() { # ynh_restore_upgradebackup () { echo "Upgrade failed." >&2 - app_bck=${app//_/-} # Replace all '_' by '-' + local app_bck=${app//_/-} # Replace all '_' by '-' # Check if an existing backup can be found before removing and restoring the application. if sudo yunohost backup list | grep -q $app_bck-pre-upgrade$backup_number @@ -64,9 +64,9 @@ ynh_backup_before_upgrade () { echo "This app doesn't have any backup script." >&2 return fi - backup_number=1 - old_backup_number=2 - app_bck=${app//_/-} # Replace all '_' by '-' + local backup_number=1 + local old_backup_number=2 + local app_bck=${app//_/-} # Replace all '_' by '-' # Check if a backup already exists with the prefix 1 if sudo yunohost backup list | grep -q $app_bck-pre-upgrade1 @@ -94,7 +94,7 @@ ynh_backup_before_upgrade () { # Download, check integrity, uncompress and patch the source from app.src # # The file conf/app.src need to contains: -# +# # SOURCE_URL=Address to download the app archive # SOURCE_SUM=Control sum # # (Optional) Program to check the integrity (sha256sum, md5sum...) @@ -113,9 +113,9 @@ ynh_backup_before_upgrade () { # Details: # This helper downloads sources from SOURCE_URL if there is no local source # archive in /opt/yunohost-apps-src/APP_ID/SOURCE_FILENAME -# +# # Next, it checks the integrity with "SOURCE_SUM_PRG -c --status" command. -# +# # If it's ok, the source archive will be uncompressed in $dest_dir. If the # SOURCE_IN_SUBDIR is true, the first level directory of the archive will be # removed. @@ -130,7 +130,7 @@ ynh_backup_before_upgrade () { ynh_setup_source () { local dest_dir=$1 local src_id=${2:-app} # If the argument is not given, source_id equals "app" - + # Load value from configuration file (see above for a small doc about this file # format) local src_url=$(grep 'SOURCE_URL=' "$YNH_CWD/../conf/${src_id}.src" | cut -d= -f2-) @@ -139,7 +139,7 @@ ynh_setup_source () { local src_format=$(grep 'SOURCE_FORMAT=' "$YNH_CWD/../conf/${src_id}.src" | cut -d= -f2-) local src_in_subdir=$(grep 'SOURCE_IN_SUBDIR=' "$YNH_CWD/../conf/${src_id}.src" | cut -d= -f2-) local src_filename=$(grep 'SOURCE_FILENAME=' "$YNH_CWD/../conf/${src_id}.src" | cut -d= -f2-) - + # Default value src_sumprg=${src_sumprg:-sha256sum} src_in_subdir=${src_in_subdir:-true} @@ -216,10 +216,11 @@ ynh_setup_source () { # | arg: ... - (Optionnal) More POST keys and values ynh_local_curl () { # Define url of page to curl - full_page_url=https://localhost$path_url$1 + local full_page_url=https://localhost$path_url$1 # Concatenate all other arguments with '&' to prepare POST data - POST_data="" + local POST_data="" + local arg="" for arg in "${@:2}" do POST_data="${POST_data}${arg}&"