dev/endpoints/endpoint-get.sh
2024-02-20 10:30:02 +02:00

182 lines
4.2 KiB
Bash
Executable File

#!/bin/bash
C="\e[0;36m"
G="\e[0;32m"
B="\e[0;34m"
W="\e[0;97m"
E="\e[00m"
SCRIPT_PATH=$(realpath $0)
SCRIPT_DIR=$(dirname ${SCRIPT_PATH})
SOURCE="/root/dev/endpoints"
BASE_DIR="/var/www/sites"
MODULES="modules/custom"
SITE=${1}
NODE=${2}
IFS=$'\n'
function quit() {
echo
echo -e "Syntax:"
echo -e "${W}./endpoint-get <drupal dir> <node type>${E}"
echo
echo -e "Example:"
echo -e "${W}./endpoint-get mysite4 events${E}"
echo
exit
}
[ "$#" -lt 2 ] && echo "Argument missing" && quit
[ ! -d ${SITE} ] && echo "Directory ${SITE} does not exist" && quit
[ -d ${SITE}/web ] && SITE=${SITE}/web
function log() {
if [ $? -eq 0 ]; then
echo -e "${G}[ OK ]${E}"
return 0
else
echo -e "${G}[FAIL]${E}"
return 1
fi
}
function copy_template() {
echo
echo -en " 📂 Copy template module dir to ${B}${SITE}${E} "
cp -r "${SOURCE}/endpoint_get_AAAAA" "${BASE_DIR}/${SITE}/${MODULES}/endpoint_get_${NODE}"
log $?
echo
cd "${BASE_DIR}/${SITE}/${MODULES}/endpoint_get_${NODE}"
}
# Rename files
function rename_info() {
echo -en " 🍓 Rename ${B}endpoint_get_${NODE}.info.yml${E} file "
mv "endpoint_get_AAAAA.info.yml" "endpoint_get_${NODE}.info.yml" &> /dev/null
log $?
}
function rename_routing() {
echo -en " 🍓 Rename ${B}endpoint_get_${NODE}.routing.yml${E} file "
mv "endpoint_get_AAAAA.routing.yml" "endpoint_get_${NODE}.routing.yml" &> /dev/null
log $?
}
# Edit files
function edit_files() {
echo
echo -en " ⭐ Edit ${B}endpoint_get_${NODE}.info.yml${E} file "
sed -i "s/AAAAA/${NODE}/g" "endpoint_get_${NODE}.info.yml"
log $?
[ $? -ne 0 ] && return 1
echo -en " ⭐ Edit ${B}endpoint_get_${NODE}.routing.yml${E} file "
sed -i "s/AAAAA/${NODE}/g" "endpoint_get_${NODE}.routing.yml"
log $?
[ $? -ne 0 ] && return 1
echo -en " ⭐ Edit ${B}src/Controller/NodeController.php${E} file "
sed -i "s/AAAAA/${NODE}/g" "src/Controller/NodeController.php"
log $?
[ $? -ne 0 ] && return 1
echo -en " ⭐ Edit ${B}src/Controller/NodesController.php${E} file "
sed -i "s/AAAAA/${NODE}/g" "src/Controller/NodesController.php"
log $?
[ $? -ne 0 ] && return 1
echo -en " ⭐ Edit ${B}src/Controller/CategoryController.php${E} file "
sed -i "s/AAAAA/${NODE}/g" "src/Controller/CategoryController.php"
log $?
[ $? -ne 0 ] && return 1
return 0
}
function file_permissions() {
echo
echo -en " 🫐 chown -R apache:apache ${B}/endpoint_get_${NODE}${E} "
chown -R apache:apache "${BASE_DIR}/${SITE}/${MODULES}/endpoint_get_${NODE}"
log $?
[ $? -ne 0 ] && return 1
return 0
}
function enable_module() {
return 0
echo
echo -en " 🌈 Enable module"
cd /var/www/sites/${SITE}
../vendor/drush/drush/drush en "endpoint_get_${NODE}"
log $?
[ $? -ne 0 ] && return 1
return 0
}
function test_module() {
BASE_URL="http://$(ls -l /var/www/ | grep ${SITE} | awk {'print $9'}).vm7"
API_URL="api/el"
NODE_URL="${BASE_URL}/${API_URL}/${NODE}"
NODES_URL="${BASE_URL}/${API_URL}/${NODE}s"
ENDPOINTS=(
api/en/${NODE}s
api/el/${NODE}s
)
for ENDPOINT in ${ENDPOINTS[@]}; do
ENDPOINT_STATUS=$(curl -sI --location --request GET $BASE_URL/$ENDPOINT?_format=json \
-b cookie.txt \
--header "Content-type: application/json" | grep HTTP | awk {'print $2'})
RESPONSE=$(curl -s --location --request GET $BASE_URL/$ENDPOINT | head -c100)
echo -e "\n 🍒 GET $BASE_URL/$ENDPOINT $ENDPOINT_STATUS"
[ "${ENDPOINT_STATUS}" == "200" ] && curl -s --location --request GET $BASE_URL/$ENDPOINT | head -c75
echo
done
echo
return 0
}
function add_fields () {
for FIELD in $(bash ${SCRIPT_DIR}/get-fields.sh ${SITE} node ${NODE});do
FIELD_NAME=$(echo $FIELD | awk '{print $1}')
FIELD_TYPE=$(echo $FIELD | awk '{print $2}')
echo
echo -en "Add field ${C}${FIELD_NAME}${E} of type ${C}${FIELD_TYPE}${E} ? [y/n] "
read -n 1 response
echo
if [[ "$response" =~ ^([yY])$ ]]
then
echo do_something
fi
done
}
if copy_template; then
if rename_info && rename_routing; then
if edit_files && file_permissions; then
if enable_module && test_module; then
add_fields
fi
fi
fi
fi
echo
echo -e "Relmember to check the settings for:"
echo -e " - Permissions"
echo -e " - Translations"
echo -e " - Aliases"
echo