captaincore/app/quicksave/sync
2022-09-13 19:27:07 -04:00

142 lines
3.2 KiB
Bash

#!/usr/bin/env bash
#
# Store Quicksave data to CaptainCore API
#
# `captaincore quicksave-store`
#
# [<site>...]
# One or more sites.
#
# [@<target>]
# Target groups of sites like @all @production or @staging.
#
# [--debug]
# Debug mode
#
# Load configuration
root_path="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"; root_path=${root_path%app*}
source ${root_path}lib/arguments
run_command() {
echo "Stores quicksaves for $# sites"
INDEX=0
for website in "$@"; do
let INDEX=${INDEX}+1
# Extract environment
if [[ "$site" == *"-staging"* ]]; then
environment=staging
else
environment=production
fi
# Load site configs
while read site_configs; do declare "$site_configs"; done <<< "$(captaincore site get $website --bash --captain_id=$captain_id)"
echo "${INDEX}/$# - $domain"
# Return error if domain not found
if [[ "$domain" == "" ]] || [[ "$site" == "" ]]; then
echo "Can't locate website for $site"
continue
fi
cd $path/${site}_${site_id}/${environment}/quicksave/
# capture all git commit into array
git_commits=($(git log --pretty=format:"%H"))
quicksaves="["
# loop through and build out info
for git_commit in ${git_commits[@]}; do
core=$(git show ${git_commit}:versions/core.json)
themes=$(git show ${git_commit}:versions/themes.json)
plugins=$(git show ${git_commit}:versions/plugins.json)
date=$(git show -s --pretty=format:"%ct" $git_commit) # Get date of last commit (UNIX timestamp)
git_status=$(git show $git_commit --shortstat --format=)
# filter out git commits missing version info
if [[ $core != "[{"* ]] && [[ $themes == "[{"* ]] && [[ $plugins == "[{"* ]]; then
read -r -d '' quicksave << EOM
{
"git_commit":"$git_commit",
"git_status":"$git_status",
"date":"$date",
"core":"$core",
"themes": $themes,
"plugins": $plugins
},
EOM
quicksaves+=$quicksave
fi
done
quicksaves=${quicksaves%?} # removes ending comma
quicksaves+="]" # finishes json
if [[ "$debug" == "true" ]]; then
read -r -d '' VAR << EOM
{
"command": "import-quicksaves",
"environment":"$environment",
"site_id":"$site_id",
"data": $quicksaves,
"token":"$token"
}
EOM
echo $VAR
continue
fi
# Adds quicksave to CaptainCore GUI
if [[ "$captaincore_dev" == true ]]; then
curl_argument="-k"
fi
# Adds quicksave to CaptainCore GUI
curl ${curl_argument} --request POST "$captaincore_api" --header "Content-Type: application/json" --data @- << EOM
{
"command": "import-quicksaves",
"environment":"$environment",
"site_id":"$site_id",
"data": $quicksaves,
"token":"$token"
}
EOM
# Clear out variables
site=''
domain=''
home_directory=''
subsite=''
response=''
response_parsed=''
done
}
# See if any sites are specifed
if [ ${#arguments[*]} -gt 0 ]; then
# Runs on specifed sites
run_command ${arguments[*]}
fi
# Runs on targeted sites
if [ ${#targets[*]} -gt 0 ]; then
run_command $(captaincore site list ${targets[*]/targets=/@} --captain_id=$captain_id)
fi
# Error if no sites specifed
if [ ${#targets[*]} -eq 0 ] && [ ${#arguments[*]} -eq 0 ]; then
echo -e "${COLOR_RED}Error:${COLOR_NORMAL} Please specify one or more sites, or use a target @all, @production or @staging."
fi