captaincore/lib/remote-scripts/launch
2023-11-15 14:55:10 -05:00

83 lines
2.3 KiB
Bash

#!/usr/bin/env bash
#
# Launches site - updates url from dev to live, enable search engine and clears cache
#
# `launch --domain=<domain>`
#
COLOR_RED="\033[31m"
COLOR_NORMAL="\033[39m"
# Loop through arguments and separate regular arguments from flags
for arg in "$@"; do
# Add to arguments array. (Does not starts with "--")
if [[ $arg != --* ]]; then
count=1+${#arguments[*]}
arguments[$count]=$arg
continue
fi
# Remove leading "--"
flag_name=$( echo $arg | cut -c 3- )
# Add to flags array
count=1+${#flags[*]}
flags[$count]=$arg
# Process flags without data (Assign to variable)
if [[ $arg != *"="* ]]; then
flag_name=${flag_name//-/_}
declare "$flag_name"=true
fi
# Process flags with data (Assign to variable)
if [[ $arg == *"="* ]]; then
flag_value=$( echo $flag_name | perl -n -e '/.+?=(.+)/&& print $1' ) # extract value
flag_name=$( echo $flag_name | perl -n -e '/(.+?)=.+/&& print $1' ) # extract name
flag_name=${flag_name/-/_}
# Remove first and last quote if found
flag_value="${flag_value%\"}"
flag_value="${flag_value#\"}"
declare "$flag_name"="$flag_value"
continue
fi
done
run_command() {
if [[ "$domain" == "" ]]; then
echo -e "${COLOR_RED}Error:${COLOR_NORMAL} Please specify --domain=<domain>."
exit 1
fi
current_domain=$( wp option get home --skip-plugins --skip-themes )
current_domain=${current_domain/http:\/\//} # removes https://
current_domain=${current_domain/https:\/\//} # removes http://
current_domain=$( echo $current_domain | awk '{$1=$1};1' ) # Trims whitespace
if [[ "$current_domain" == "" ]] || [[ "$current_domain" != *"."* ]]; then
echo -e "${COLOR_RED}Error:${COLOR_NORMAL} Could not find existing domain."
exit 1
fi
echo "Search and replace //${current_domain} to //$domain"
wp search-replace "//${current_domain}" "//$domain" --all-tables --skip-plugins --skip-themes --report-changed-only
echo "Search and replace \/\/${current_domain} to \/\/$domain"
wp search-replace "\/\/${current_domain}" "\/\/$domain" --all-tables --skip-plugins --skip-themes --report-changed-only
wp option update blog_public 1 --skip-plugins --skip-themes
wp cache flush
if [[ "$current_domain" == *"kinsta"* ]]; then
wp kinsta cache purge --all
fi
}
run_command