#!/bin/bash # # example: # # add-fields.sh klimatologia4/web/modules/custom/src/Nodes.php manager string # # C="\e[0;36m" R="\e[0;31m" G="\e[0;32m" B="\e[0;34m" W="\e[0;97m" E="\e[00m" # Check parameters [ $# -ne 3 ] && echo 'Parameter missing' && exit # Check Target file PHP="${1}" [ ! -f ${PHP} ] && echo 'File ${PHP} does not exist' && exit # Field name NAME="${2}" # Field source files TYPE="${3}" FIELD_DIR="/root/dev/endpoints/endpoint_get_fields/" ls -1 $FIELD_DIR | grep -E "${TYPE}_(extractor|response|splitter)" > /dev/null [ "$?" -ne 0 ] && echo -e "File for field ${R}${TYPE}${E} not found" # # Iterate source files # for FILE in `ls -1 $FIELD_DIR | grep -E "^${TYPE}_(extractor|response|splitter)"`; do COMMENT=$(echo ${FILE} | awk -F_ '{print $2}') COMMENT=$(echo ${FILE} | awk -F_ '{print $2}') echo Adding 🍓 ${FILE} to 📦 $(basename ${PHP}) done exit comment="// FIELDS " ${} exit new_code=$(cat << 'EOF' EOF ) # Find the line number of the comment comment_line=$(grep -n "$comment" "$php_file" | cut -d ":" -f 1) if [[ -n "$comment_line" ]]; then # Calculate the line number to insert the new code insert_line=$((comment_line + 1)) # Insert the new code into the PHP file awk -v insert="$insert_line" -v text="$new_code" 'NR == insert {print text} {print}' "$php_file" > tmpfile && mv tm pfile "$php_file" echo "New code added successfully!" else echo "Comment not found!" fi