2025-05-07 10:03:24 +08:00

72 lines
1.7 KiB
Bash

#!/bin/bash
# Configuration - Replace these with your actual values
TARGET_USER="fmc"
TARGET_HOST="macmini"
TARGET_PATH="/Users/fmc/docker/fmc-writer" # Use absolute path instead of ~ to avoid expansion issues
# Colors for output
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color
# Error handling function
handle_error() {
echo -e "${RED}ERROR: $1${NC}"
exit 1
}
# Process command line arguments
FOLLOW=false
SERVICE=""
TAIL=""
# Parse arguments
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-f|--follow)
FOLLOW=true
shift
;;
-t|--tail)
TAIL="--tail $2"
shift 2
;;
*)
# Assume any other argument is a service name
SERVICE="$1"
shift
;;
esac
done
FOLLOW_FLAG=""
if [ "$FOLLOW" = true ]; then
FOLLOW_FLAG="-f"
fi
trap 'echo -e "${YELLOW}Log viewing interrupted by user.${NC}"; exit 0' SIGINT
echo -e "${GREEN}Connecting to ${TARGET_HOST} to view logs...${NC}"
# Connect to the remote server and display logs
ssh -T "${TARGET_USER}@${TARGET_HOST}" << EOF || handle_error "Failed to connect or retrieve logs"
set -e # Exit immediately if a command exits with a non-zero status
echo "Changing to target directory: ${TARGET_PATH}"
cd "${TARGET_PATH}" || { echo "Failed to change to target directory"; exit 1; }
# Check if docker-compose.yml exists
if [ ! -f "docker-compose.yml" ]; then
echo "docker-compose.yml not found in ${TARGET_PATH}"
exit 1
fi
# Show logs with optional parameters
echo "Displaying logs..."
docker-compose logs ${FOLLOW_FLAG} ${TAIL} ${SERVICE}
EOF
echo -e "${GREEN}Log session ended${NC}"