File: //bigscoots/wpo/cloudflare/prefetch.sh
#!/bin/bash
source /bigscoots/includes/common.sh
# Function to display help
function show_help {
echo "Usage: $0 {generate} --domain DOMAIN"
echo ""
echo "Commands:"
echo "generate Generate maxnifest.txt with posts,pages,categories. You must provide --domain."
echo " Example: $0 generate --domain example.com"
echo ""
}
generate_manifest() {
local domain="$1"
local status=true
local err_output=""
# Check if all parameters are provided
if [[ -z "$domain" ]]
then
echo "{\"status\":\"fail\",\"msg\":\"Missing $domain, unable to proceed. Support has been alerted!\"}"
exit 1
fi
local manifest_txt="$docroot/manifest.txt"
err_output=$(wpcli post list --field=url --post_type=page,post --post_status=publish --path=$docroot 2>/dev/null)
exit_status=$?
if [ $exit_status -eq 0 ]
then
echo "$err_output" > "$manifest_txt"
else
status=false
err_output+=" wpcli post list"
fi
if [ "$status" = true ]
then
err_output=$(wpcli term list category --field=url --path=$docroot 2>/dev/null)
exit_status=$?
if [ $exit_status -eq 0 ]
then
local wpurl=$(wpcli option get siteurl --path=$docroot 2>/dev/null)
echo "$err_output" >> "$manifest_txt"
echo "{\"status\":\"success\",\"url\":\""$wpurl/manifest.txt"\"}"
else
status=false
err_output+=" wpcli term list category"
fi
fi
if [ "$status" = false ]
then
send_slack_alert "#wpo-alerts" ":warning:" "Generating Cloudflare Prefetch Manifest.txt - Function: \`generate_manifest\`" "$domain" "\`\`\`$err_output\`\`\`"
fi
}
# Check command line arguments
if [[ $# -lt 2 ]]
then
show_help
exit 1
fi
# Parse command line arguments
command=$1
shift
while (( "$#" ))
do
case "$1" in
--domain)
domain=$2
shift 2
;;
*)
echo "Invalid command"
show_help
exit 1
;;
esac
done
docroot="/home/nginx/domains/$domain/public"
case $command in
generate)
generate_manifest "${domain}"
exit 0
;;
*)
echo "Invalid command"
show_help
exit 1
;;
esac