script-management-cloudpanel/Testing/test.sh
2024-11-25 22:17:17 +05:30

174 lines
No EOL
4.4 KiB
Bash

#!/bin/bash
#-----------------------------------#
# VARIABLES #
#-----------------------------------#
this_script_url="https://git.hhf.technology/hhf/script-management-cloudpanel/raw/branch/main/Testing/test.sh"
this_script_name="Test Script"
formatter_url="https://git.hhf.technology/hhf/TaskFormatter/raw/branch/main/bash_task_formatter/task_formatter.sh"
scriptname=$0
# Initialize success flag
success=0
# Determine the user (use the first argument if provided, otherwise fallback)
USER_TO_RUN_AS="${1:-$SUDO_USER}"
USER_HOME=$(eval echo ~$USER_TO_RUN_AS)
#-----------------------------------#
# FORMATTER #
#-----------------------------------#
# 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
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
source ./task_formatter.sh || { echo "Error: Failed to source task_formatter.sh"; exit 1; }
# Check if print_header is available after sourcing
if ! declare -f print_header > /dev/null; then
echo "Error: print_header function not found after sourcing."
exit 1
fi
}
# Call the download_formatter function
download_formatter
#-----------------------------------#
# FUNCTIONS #
#-----------------------------------#
# Function to update packages
update_upgrade_packages() {
sudo apt-get update > /dev/null 2>&1
echo -e "Packages Updated $CHECK_MARK"
sudo apt-get upgrade -y > /dev/null 2>&1
echo -e "Packaged Upgraded $CHECK_MARK"
sudo apt-get autoremove -y > /dev/null 2>&1
echo -e "Packages Cleaned $CHECK_MARK"
}
example_function_1() {
echo -e "This is example function 1. $CHECK_MARK"
sleep 2
return 0
}
# Example function 2
example_function_2() {
echo "This is example function 2."
sleep 2
echo -e "Done $CHECK_MARK"
sleep 2
return 0
}
# Example function 3 with an error
example_function_3() {
echo -e "This is example function 3 and it will fail. $CROSS_MARK"
sleep 3
return 1
}
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
}
# remove artifacts
remove_script() {
if [ -f "$0" ]; then
echo "Deleted master script..."
rm -- "$0"
fi
if [ -f "task_formatter.sh" ]; then
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 #
#-----------------------------------#
# 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
# 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
success=1
fi
if ! format_output example_function_2 "Example Function 2"; then
cleanup_files
success=1
fi
if ! format_output example_function_3 "Example Function 3"; then
cleanup_files
success=1
fi
if ! format_output_with_input ask_reconfigure "Test Reconfiguring"; then
cleanup_files
success=1
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() {
local script_name=$1
local success=$2
if [[ $success -eq 0 ]]; then
log "${CHECK_MARK} $script_name completed successfully."
else
log "${CROSS_MARK} $script_name encountered errors."
fi
}
# Exit with appropriate status
exit $success