Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shellcheck improvements #2675

Merged
merged 4 commits into from
Jul 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ permalink: /docs/en-US/changelog/
### Bug Fixes

* Fixed a vagrantfile error on Arm when the vagrant-parallels plugin is missing ( #2670 )
* Miscellanous Shellcheck linter fixes and improvements ( #2675 )
* Github action fixes for composer and an upgrade to Ubuntu 20 ( #2677 )

## 3.11.2 ( 2023 May 8th )
Expand Down
66 changes: 43 additions & 23 deletions provision/provision-site.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ VVV_CONFIG=/vagrant/config.yml
# @arg $1 string the config value to fetch
# @arg $2 string the default value
function vvv_get_site_config_value() {
local value=$(shyaml -q get-value "sites.${SITE_ESCAPED}.${1}" "${2}" < ${VVV_CONFIG})
local value
value=$(shyaml -q get-value "sites.${SITE_ESCAPED}.${1}" "${2}" < ${VVV_CONFIG})
echo "${value}"
}

Expand All @@ -47,17 +48,18 @@ function vvv_get_site_php_version() {
SITE_PHP=$(echo -n "${SITE_PHP}" | xargs | tr -d '\n' | tr -d '\r')

# Handle when php:8 instead of 8.0 or if it's parsed as a number
if [[ "${#SITE_PHP}" -eq "1" ]]; then
if [[ ${#SITE_PHP} -eq 1 ]]; then
SITE_PHP="${SITE_PHP}.0"
fi

echo -n "${SITE_PHP}"
}

vvv_validate_site_php_version() {
local SITE_PHP
SITE_PHP=$(vvv_get_site_php_version)
if [[ "${#SITE_PHP}" > "3" ]]; then
vvv_warn " ! Warning: PHP version defined is using a wrong format: '${SITE_PHP}' with length '${length}'"
if [[ ${#SITE_PHP} -gt 3 ]]; then
vvv_warn " ! Warning: PHP version defined is using a wrong format: '${SITE_PHP}' with length '${#SITE_PHP}'"
vvv_warn " If you are trying to use a more specific version of PHP such as 7.4.1 or 7.4.0 you"
vvv_warn " need to be less specific and use 7.4"
fi
Expand Down Expand Up @@ -102,7 +104,8 @@ function get_config_value() {
# @see get_hosts_list
# @stdout a space separated string of domains, defaulting to `sitename.test` if none are specified
function get_hosts() {
local value=$(shyaml -q get-values-0 "sites.${SITE_ESCAPED}.hosts" < ${VVV_CONFIG} | tr '\0' ' ' | sed 's/ *$//')
local value
value=$(shyaml -q get-values-0 "sites.${SITE_ESCAPED}.hosts" < ${VVV_CONFIG} | tr '\0' ' ' | sed 's/ *$//')
echo "${value:-"${VVV_SITE_NAME}.test"}"
}

Expand All @@ -112,7 +115,8 @@ function get_hosts() {
# @see get_hosts
# @stdout a space separated string of domains, defaulting to `sitename.test` if none are specified
function get_hosts_list() {
local value=$(shyaml -q get-values "sites.${SITE_ESCAPED}.hosts" < ${VVV_CONFIG})
local value
value=$(shyaml -q get-values "sites.${SITE_ESCAPED}.hosts" < ${VVV_CONFIG})
echo "${value:-"${VVV_SITE_NAME}.test"}"
}

Expand All @@ -123,7 +127,8 @@ function get_hosts_list() {
# @see get_hosts_list
# @stdout the first host listed in the config file for this site, defaulting to `sitename.test` if none are specified
function get_primary_host() {
local value=$(shyaml -q get-value "sites.${SITE_ESCAPED}.hosts.0" "${1}" < ${VVV_CONFIG})
local value
value=$(shyaml -q get-value "sites.${SITE_ESCAPED}.hosts.0" "${1}" < ${VVV_CONFIG})
echo "${value:-"${VVV_SITE_NAME}.test"}"
}

Expand All @@ -144,7 +149,8 @@ function vvv_provision_site_nginx_config() {
DEST_NGINX_FILE="vvv-auto-${DEST_NGINX_FILE}-$(md5sum <<< "${SITE_NGINX_FILE}" | cut -c1-32).conf"

VVV_HOSTS=$(get_hosts)
local TMPFILE=$(mktemp /tmp/vvv-site-XXXXX)
local TMPFILE
TMPFILE=$(mktemp /tmp/vvv-site-XXXXX)
cat "${SITE_NGINX_FILE}" >> "${TMPFILE}"

vvv_info " * VVV is adding an Nginx config from ${SITE_NGINX_FILE}"
Expand Down Expand Up @@ -218,7 +224,7 @@ function vvv_provision_hosts_file() {
echo " - Added ${line} from ${HOSTFILE}"
fi
fi
done < "$HOSTFILE"
done < "${HOSTFILE}"
done
}

Expand All @@ -229,7 +235,8 @@ function vvv_provision_hosts_file() {
# @noargs
function vvv_process_site_hosts() {
echo " * Adding domains to the virtual machine's /etc/hosts file..."
local hosts=$(get_hosts_list)
local hosts
hosts=$(get_hosts_list)
if [ ${#hosts[@]} -eq 0 ]; then
echo " * No hosts were found in the VVV config, falling back to vvv-hosts"
if [[ -f "${VM_DIR}/.vvv/vvv-hosts" ]]; then
Expand All @@ -243,7 +250,8 @@ function vvv_process_site_hosts() {
vvv_provision_hosts_file "${SITE}" "${VM_DIR}/vvv-hosts"
else
echo " * Searching subfolders 4 levels down for a vvv-hosts file ( this can be skipped by using ./vvv-hosts, .vvv/vvv-hosts, or provision/vvv-hosts"
local HOST_FILES=$(find "${VM_DIR}" -maxdepth 4 -name 'vvv-hosts');
local HOST_FILES
HOST_FILES=$(find "${VM_DIR}" -maxdepth 4 -name 'vvv-hosts');
if [[ -z $HOST_FILES ]] ; then
vvv_error " ! Warning: No vvv-hosts file was found, and no hosts were defined in the vvv config, this site may be inaccessible"
else
Expand All @@ -255,7 +263,7 @@ function vvv_process_site_hosts() {
else
echo " * Adding hosts for the site to the VM hosts file"
for line in $hosts; do
if [[ -z "$(grep -q "^127.0.0.1 $line$" /etc/hosts)" ]]; then
if [[ -z "$(grep -q "^127.0.0.1 ${line}$" /etc/hosts)" ]]; then
echo "127.0.0.1 ${line} # vvv-auto" >> "/etc/hosts"
echo " - Added ${line} from ${VVV_CONFIG}"
fi
Expand All @@ -276,6 +284,7 @@ function vvv_provision_site_repo() {
echo " * Any local changes not present on the server will be discarded in favor of the remote branch"
cd "${VM_DIR}"
echo " * Checking that remote origin is ${REPO}"
local CURRENTORIGIN
CURRENTORIGIN=$(noroot git remote get-url origin)
if [[ "${CURRENTORIGIN}" != "${REPO}" ]]; then
vvv_error " ! The site config said to use <b>${REPO}</b>"
Expand Down Expand Up @@ -357,12 +366,14 @@ function vvv_provision_site_script() {
SUCCESS=$?
else
vvv_warn " * Warning: A site provisioner was not found at .vvv/vvv-init.sh provision/vvv-init.sh or vvv-init.sh, searching 3 folders down, please be patient..."
local SITE_INIT_SCRIPTS=$(find "${VM_DIR}" -maxdepth 3 -name 'vvv-init.sh');
local SITE_INIT_SCRIPTS
SITE_INIT_SCRIPTS=$(find "${VM_DIR}" -maxdepth 3 -name 'vvv-init.sh');
if [[ -z $SITE_INIT_SCRIPTS ]] ; then
vvv_warn " * Warning: No site provisioner was found, VVV could not perform any scripted setup that might install software for this site"
else
for SITE_INIT_SCRIPT in $SITE_INIT_SCRIPTS; do
local DIR="$(dirname "$SITE_INIT_SCRIPT")"
local DIR
DIR="$(dirname "${SITE_INIT_SCRIPT}")"
vvv_run_site_template_script "vvv-init.sh" "${DIR}"
done
fi
Expand All @@ -387,7 +398,8 @@ function vvv_provision_site_nginx() {
vvv_warn " - ${VM_DIR}/provision/vvv-nginx.conf"
vvv_warn " - ${VM_DIR}/vvv-nginx.conf"
vvv_warn " * VVV will search 3 folders down to find an Nginx config, please be patient..."
local NGINX_CONFIGS=$(find "${VM_DIR}" -maxdepth 3 -name 'vvv-nginx.conf');
local NGINX_CONFIGS
NGINX_CONFIGS=$(find "${VM_DIR}" -maxdepth 3 -name 'vvv-nginx.conf');
if [[ -z $NGINX_CONFIGS ]] ; then
vvv_warn " * VVV did not found an Nginx config file, it will use a fallback config file."
noroot mkdir -p "${VM_DIR}/log"
Expand Down Expand Up @@ -424,7 +436,8 @@ function vvv_custom_folder_composer() {
if keys=$(shyaml keys -y -q "sites.${SITE_ESCAPED}.folders.${folder}.composer" < "${VVV_CONFIG}"); then
for key in $keys; do
cd "${folder}"
local value=$(vvv_get_site_config_value "folders.${folder}.composer.${key}" "")
local value
value=$(vvv_get_site_config_value "folders.${folder}.composer.${key}" "")
if [[ "install" == "${key}" ]]; then
if [[ "True" == "${value}" ]]; then
vvv_info " * Running composer install in ${folder}"
Expand Down Expand Up @@ -455,7 +468,8 @@ function vvv_custom_folder_npm() {
if keys=$(shyaml keys -y -q "sites.${SITE_ESCAPED}.folders.${folder}.npm" < "${VVV_CONFIG}"); then
for key in $keys; do
cd "${folder}"
local value=$(vvv_get_site_config_value "folders.${folder}.npm.${key}" "")
local value
value=$(vvv_get_site_config_value "folders.${folder}.npm.${key}" "")
if [[ "install" == "${key}" ]]; then
if [[ "True" == "${value}" ]]; then
vvv_info " * Running npm install in ${folder}"
Expand Down Expand Up @@ -483,11 +497,17 @@ function vvv_custom_folder_npm() {
# @internal
# @see vvv_clone_site_git_folder
function vvv_custom_folder_git() {
local folder="${1}"
local repo=$(vvv_get_site_config_value "folders.${folder}.git.repo" "?")
local overwrite_on_clone=$(vvv_get_site_config_value "folders.${folder}.git.overwrite_on_clone" "False")
local hard_reset=$(vvv_get_site_config_value "folders.${folder}.git.hard_reset" "False")
local pull=$(vvv_get_site_config_value "folders.${folder}.git.pull" "False")
local folder
local repo
local overwrite_on_clone
local hard_reset
local pull

folder="${1}"
repo=$(vvv_get_site_config_value "folders.${folder}.git.repo" "?")
overwrite_on_clone=$(vvv_get_site_config_value "folders.${folder}.git.overwrite_on_clone" "False")
hard_reset=$(vvv_get_site_config_value "folders.${folder}.git.hard_reset" "False")
pull=$(vvv_get_site_config_value "folders.${folder}.git.pull" "False")

if [ ! -d "${VVV_PATH_TO_SITE}/${folder}" ]; then
vvv_clone_site_git_folder "${repo}" "${folder}"
Expand All @@ -496,7 +516,7 @@ function vvv_custom_folder_git() {
if [ ! -d "${VVV_PATH_TO_SITE}/${folder}/.git" ]; then
vvv_info " - VVV was asked to clone into a folder that already exists (${folder}), but does not contain a git repo"
vvv_info " - overwrite_on_clone is turned on so VVV will purge with extreme predjudice and clone over the folders grave"
rm -rf "${VVV_PATH_TO_SITE}/${folder}"
rm -rf "${VVV_PATH_TO_SITE:?}/${folder}"
vvv_clone_site_git_folder "${repo}" "${folder}"
fi
else
Expand Down