This commit is contained in:
hhftechnologies 2024-11-25 16:05:44 +05:30
parent a0a80b8365
commit c72b22b0fa

View file

@ -1,9 +1,5 @@
#!/bin/bash
# Run this script with:
# wget https://raw.githubusercontent.com/pitterpatter22/Script-Repo/main/Testing/test.sh --no-check-certificate && sudo bash script-template.sh
#-----------------------------------#
# VARIABLES #
#-----------------------------------#
@ -20,17 +16,36 @@ success=0
USER_TO_RUN_AS="${1:-$SUDO_USER}"
USER_HOME=$(eval echo ~$USER_TO_RUN_AS)
#-----------------------------------#
# FORMATTER #
#-----------------------------------#
if [ ! -f "task_formatter.sh" ]; then
wget $formatter_url --no-check-certificate -O task_formatter.sh > /dev/null 2>&1
fi
# Download and source the formatter with error handling
download_formatter() {
if [ ! -f "task_formatter.sh" ]; then
if ! wget "$formatter_url" --no-check-certificate -O task_formatter.sh > /dev/null 2>&1; then
echo "Error: Failed to download task_formatter.sh"
exit 1
fi
fi
source ./task_formatter.sh
if [ ! -f "task_formatter.sh" ]; then
echo "Error: task_formatter.sh not found after download attempt"
exit 1
fi
# Make the formatter executable
chmod +x task_formatter.sh
# Source the formatter with error checking
if ! source ./task_formatter.sh; then
echo "Error: Failed to source task_formatter.sh"
exit 1
fi
}
# Call the download_formatter function
download_formatter
#-----------------------------------#
# FUNCTIONS #
@ -49,6 +64,7 @@ update_upgrade_packages() {
example_function_1() {
echo -e "This is example function 1. $CHECK_MARK"
sleep 2
return 0
}
# Example function 2
@ -57,6 +73,7 @@ example_function_2() {
sleep 2
echo -e "Done $CHECK_MARK"
sleep 2
return 0
}
# Example function 3 with an error
@ -67,12 +84,12 @@ example_function_3() {
}
ask_reconfigure() {
read -p "Question? (y/n): " choice
case "$choice" in
y|Y ) return 0;;
n|N ) return 1;;
* ) echo "Invalid choice."; ask_reconfigure;;
esac
read -p "Question? (y/n): " choice
case "$choice" in
y|Y ) return 0;;
n|N ) return 1;;
* ) echo "Invalid choice."; ask_reconfigure;;
esac
}
# remove artifacts
@ -85,48 +102,30 @@ remove_script() {
rm task_formatter.sh
fi
echo -e "Cleaned up $CHECK_MARK"
return 0
}
# Remove created files on Failure
cleanup_files() {
echo -e "Cleaned up $CHECK_MARK"
return 0
}
#-----------------------------------#
# MAIN LOGIC #
#-----------------------------------#
'''
Run functions with this format:
format_output {function_name} "Function Description"
Example:
format_output update_upgrade_packages "Update and Upgrade Packages"
To add in success monitoring when script is run from master.sh, run like this:
if ! format_output {function_name} "Function Description"; then
cleanup_files
success=1
# Check if print_header function exists
if ! command -v print_header >/dev/null 2>&1; then
echo "Error: print_header function not found. Formatter may not be properly sourced."
exit 1
fi
if ! format_output {function_name} "Function Description"; then
cleanup_files
success=1
fi
'''
# Print header
print_header "$this_script_name" "$this_script_url"
echo -e "Running as User: $USER_TO_RUN_AS\nUser Home: $USER_HOME\n"
# Run the functions with formatted output
if ! format_output example_function_1 "Example Function 1"; then
cleanup_files
@ -150,6 +149,12 @@ fi
format_output remove_script "Cleaning up"
# Check if final_message function exists
if ! command -v final_message >/dev/null 2>&1; then
echo "Error: final_message function not found. Formatter may not be properly sourced."
exit 1
fi
# Print final message
final_message "$this_script_name" $success