#!/bin/bash C="\e[1;36m" G="\e[1;32m" R="\e[1;31m" B="\e[1;34m" W="\e[1;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} SITE_COMPOSER=${1} NODE=${2} IFS=$'\n' function quit() { echo echo -e "Syntax:" echo -e "${W}./endpoint-get ${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 "${R}[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() { 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'}) if [ "${ENDPOINT_STATUS}" == "200" ];then echo -e "\n 🍒 GET $BASE_URL/$ENDPOINT $ENDPOINT_STATUS" curl -s --location --request GET $BASE_URL/$ENDPOINT | head -c75 echo -e "${G}[ OK ]${E}" else echo -e "\n 🍒 GET $BASE_URL/$ENDPOINT $ENDPOINT_STATUS ${R}[FAIL]${E}" return 1 fi done return 0 } function add_fields () { for FIELD in $(bash ${SCRIPT_DIR}/get-fields.sh ${SITE_COMPOSER} node ${NODE});do FIELD_NAME=$(echo $FIELD | awk '{print $1}') FIELD_TYPE=$(echo $FIELD | awk '{print $2}') DST_FILES=("NodesController.php" "NodeController.php") DST_PATH="${BASE_DIR}/${SITE}/${MODULES}/endpoint_get_${NODE}/src/Controller" for DST_FILE in ${DST_FILES}; do echo echo -en " 🍄 ${W}${DST_FILE}${E} : Add field ${B}${FIELD_TYPE}${E} with name ${B}${FIELD_NAME}${E}? [y/n] " read -n 1 response echo if [[ "$response" =~ ^([yY])$ ]]; then echo ${DST_PATH}/${DST_FILE} echo ${FIELD_TYPE} echo ${FIELD_NAME} ${SCRIPT_DIR}/add-fields.sh "${DST_PATH}/${DST_FILE}" ${FIELD_TYPE} ${FIELD_NAME} fi done done } function main() { 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 } main