Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add colorized output and improved error handling for script execution and fixed GIT_DISCOVERY_ACROSS_FILESYSTEM #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 82 additions & 23 deletions daily_updates.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,61 +1,120 @@
#!/usr/bin/env bash
reset && clear

if [ "$EUID" -ne 0 ]
then
# Function to display error messages with failure reason and not exit
log_error() {
local msg="$1"
local cmd="$2"
tput bold
echo "Please run as root"
echo -e "\033[31m[ ERROR ]: $msg \033[0m" # Red color for errors
tput sgr0
exit
echo "$(date) - ERROR: $msg. Command: $cmd" >> "$LOG_FILE"
}

# Function to display success messages
log_success() {
local msg="$1"
local cmd="$2"
tput bold
echo -e "\033[32m[ SUCCESS ]: $msg \033[0m" # Green color for success
tput sgr0
echo "$(date) - SUCCESS: $msg. Command: $cmd" >> "$LOG_FILE"
}

# Ensure script is run as root
if [ "$EUID" -ne 0 ]; then
tput bold
echo "Please run as root."
tput sgr0
exit 1
fi

# Log file to capture script execution details
LOG_FILE="/var/log/port_update.log"
echo "$(date) - Starting script execution" > "$LOG_FILE"

# Function to run commands with logging and error checking without exiting
run_command() {
local cmd="$1"
echo "Running: $cmd"
echo "$(date) - Running: $cmd" >> "$LOG_FILE"

# Execute the command using eval to handle complex commands
output=$(eval "$cmd" 2>&1)
result=$?

# Print output to terminal and log it
echo "$output"
echo "$output" >> "$LOG_FILE"

# If the command succeeded, log as success
if [ $result -eq 0 ]; then
log_success "$cmd was successful" "$cmd"
else
log_error "Command failed with error" "$cmd"
log_error "Error output: $output" "$cmd"
fi
}

# Function to mark directories as safe for Git
mark_safe_directory() {
local dir="$1"
run_command "git config --global --add safe.directory $dir"
}

# Mark the important directories as safe
tput bold
echo "[ Marking Git directories as safe... ]"
tput sgr0

mark_safe_directory "/usr/ports"
mark_safe_directory "/usr/src"

# Update /usr/ports git tree
tput bold
echo
echo "[ Updating /usr/ports git tree... ]"
echo
tput sgr0
cd /usr/ports && git pull
run_command "(cd /usr/ports && git pull)"

# Update /usr/src git tree
tput bold
echo
echo "[ Updating /usr/src git tree... ]"
echo
tput sgr0
cd /usr/src && git pull
run_command "(cd /usr/src && git pull)"

# Update FreeBSD pkg repositories
tput bold
echo
echo "[ Updating FreeBSD pkg repos... ]"
echo
tput sgr0
pkg update
echo
run_command "pkg update"

# Clean port distfiles
tput bold
echo "[ Cleaning port distfiles... ]"
echo
tput sgr0
/usr/local/sbin/portmaster -y --clean-distfiles
run_command "/usr/local/sbin/portmaster -y --clean-distfiles"

# Clean stale port packages
tput bold
echo "[ Cleaning stale port packages... ]"
echo
tput sgr0
/usr/local/sbin/portmaster -y --clean-packages
run_command "/usr/local/sbin/portmaster -y --clean-packages"

# Check for stale entries in /var/db/ports
tput bold
echo "[ Checking for stale entries in /var/db/ports... ]"
echo
tput sgr0
/usr/local/sbin/portmaster -v --check-port-dbdir
run_command "/usr/local/sbin/portmaster -v --check-port-dbdir"

# Check for portmaster upgrades
tput bold
echo "[ Checking portmaster for any upgrades... ]"
echo
tput sgr0
/usr/local/sbin/portmaster -adyG
run_command "/usr/local/sbin/portmaster -adyG"

tput bold
echo "[ All done! ]"
echo
tput sgr0

# End of script logging
echo "$(date) - Script execution completed." >> "$LOG_FILE"