Bash Automation Script to Clone and Bulk Modify Files for Commit

I have this habit of writing scripts for simple but repetitive tasks.

Most of the time, I do this because I’m afraid of introducing a certain degree of human error depending on how many files I’m modifying.

In this case, the scripts purpose was to modify many environmental files at a time and insert the same properties into each file if they matched whitelist conditions – and that is what the below does.

#!/bin/bash
SCRIPT_DIRECTORY=$(pwd)

# Target Directory to Modify Properties Files
DIRECTORY_DIST="$SCRIPT_DIRECTORY/path/to/folder/with/files/to/modify/specifically"

# Target SVN Path to Checkout and Modify
SVN_CHECKOUT_URL="https://subversion.mycompany.com/svn/path/to/folder/with/files/to/modify/specifically"

# Environments we want to target
declare -a ENVIRONMENT_TO_TARGET_LIST
ENVIRONMENT_TO_TARGET_LIST+=("DEV")
ENVIRONMENT_TO_TARGET_LIST+=("TEST")
ENVIRONMENT_TO_TARGET_LIST+=("PROD")

# File we want to modify minus the environment string
DEV_FILE_PATTERN_TO_FIND_PREFIX="file-dev-prefix-"
DEV_FILE_PATTERN_TO_FIND_SUFFIX=".yaml"

TEST_FILE_PATTERN_TO_FIND_PREFIX="file-test-prefix-"
TEST_FILE_PATTERN_TO_FIND_SUFFIX=".yaml"

PROD_CONFIG_FILE_PATTERN_TO_FIND_PREFIX="file-prod-prefix-"
PROD_CONFIG_FILE_PATTERN_TO_FIND_SUFFIX=".yaml"

function make_change()
{
    FILE_TO_MODIFY_PATH="$1"

    echo '' >> "$FILE_TO_MODIFY_PATH"
    echo '' >> "$FILE_TO_MODIFY_PATH"
    echo '# -- My-Change-Tracking-ID -- BEGIN --' >> "$FILE_TO_MODIFY_PATH"
    echo 'AdditionalPropertyToAppend=Property' >> "$FILE_TO_MODIFY_PATH"
    echo "# -- My-Change-Tracking-ID -- END --" >> "$FILE_TO_MODIFY_PATH"
    echo '' >> "$FILE_TO_MODIFY_PATH"
}

function identify_files_and_make_change()
{
    PROPERTY_FILE_TO_MODIFY_LIST=$(find "$DIRECTORY_DIST" \
                                         \( -iname "$DEV_FILE_PATTERN_TO_FIND_PREFIX*$DEV_FILE_PATTERN_TO_FIND_SUFFIX" \
                                            -o \
                                            -iname "$TEST_FILE_PATTERN_TO_FIND_PREFIX*$TEST_FILE_PATTERN_TO_FIND_SUFFIX" \
                                            -o \
                                            -iname "$PROD_CONFIG_FILE_PATTERN_TO_FIND_PREFIX*$PROD_CONFIG_FILE_PATTERN_TO_FIND_SUFFIX" \)) 

    #Spaces in a String will mess up a for loop - https://askubuntu.com/questions/344407/how-to-read-complete-line-in-for-loop-with-spaces
    IFS=$'\n' 

    for PROPERTY_FILE_TO_MODIFY in $PROPERTY_FILE_TO_MODIFY_LIST
    do
        PROPERTY_FILE_TO_MODIFY_JUST_FILENAME=$(performConditionalActionOnFile "GET_JUST_THE_FILENAME_WITH_EXTENSION" "$PROPERTY_FILE_TO_MODIFY")

        IS_TARGETTED_ENVIRONMENT="FALSE"

        for ENVIRONMENT_TO_TARGET in "${ENVIRONMENT_TO_TARGET_LIST[@]}"
        do
            IS_TARGETTED_ENVIRONMENT=$(misc_contains_string_insensitive "$ENVIRONMENT_TO_TARGET" "$PROPERTY_FILE_TO_MODIFY_JUST_FILENAME")

            if [ "$IS_TARGETTED_ENVIRONMENT" == "TRUE" ]; then
                echo "[INFO] [Modifying] $PROPERTY_FILE_TO_MODIFY_JUST_FILENAME - Entry Detected in the ENVIRONMENT_TO_TARGET_LIST ($ENVIRONMENT_TO_TARGET) ..."
                
                make_change "$PROPERTY_FILE_TO_MODIFY"
                break
            fi
        done

        if [ "$IS_TARGETTED_ENVIRONMENT" == "FALSE" ]; then
            echo "[INFO] [Skipping] $PROPERTY_FILE_TO_MODIFY_JUST_FILENAME - Entry Not Detected in the ENVIRONMENT_TO_TARGET_LIST ... "
        fi
    done

    #Turn it off after - https://askubuntu.com/questions/344407/how-to-read-complete-line-in-for-loop-with-spaces
    unset IFS
}

function performConditionalActionOnFile()
{
    STRING_MODE="$1"

    FILE_NAME=$(basename -- "$2")
    FILE_EXTENSION="${FILE_NAME##*.}"
    FILE_NAME="${FILE_NAME%.*}"

    ARCHIVE_DIRECTORY=$(dirname "$2")
    ARCHIVE_OUTPUT_DIRECTORY="$ARCHIVE_DIRECTORY/$FILE_NAME"

    if [ "$STRING_MODE" == "GET_JUST_THE_FILENAME_WITH_EXTENSION" ]; then
        echo "$FILE_NAME.$FILE_EXTENSION"
    fi

    if [ "$STRING_MODE" == "GET_JUST_THE_FILENAME_NO_EXTENSION" ]; then
        echo "$FILE_NAME"
    fi

    if [ "$STRING_MODE" == "GET_JUST_THE_PRECEDING_DIRECTORY" ]; then
        echo "$ARCHIVE_DIRECTORY"
    fi
}

function misc_contains_string_insensitive()
{
    PATTERN_TO_FIND="$1"
    STRING_TO_PROCESS="$2"

    echo "$STRING_TO_PROCESS" | grep -iq "$PATTERN_TO_FIND"

    EXIT_CODE=$?

    #Check to see if the git operation failed or not
    if [ "$EXIT_CODE" == "0" ]; then
        echo "TRUE"
    else
        echo "FALSE"
    fi
}

function checkout()
{
    rm -Rf "$DIRECTORY_DIST"
    mkdir -p "$DIRECTORY_DIST"
    svn checkout "$SVN_CHECKOUT_URL" "$DIRECTORY_DIST"
}

function main()
{
    checkout
    identify_files_and_make_change
}

main

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s