feat: Enhance documentation and add porcelain mode support for improved automation in plex scripts

This commit is contained in:
Peter Wood
2025-06-09 06:44:06 -04:00
parent 16dd496c31
commit 11c110d729
3 changed files with 397 additions and 70 deletions

View File

@@ -79,6 +79,9 @@ readonly PLEX_SERVICE="plexmediaserver"
SCRIPT_NAME="$(basename "$0")"
readonly SCRIPT_NAME
# Global variables for command-line options
PORCELAIN_MODE=false
# 🎭 ASCII symbols for compatible output
readonly CHECKMARK="✓"
readonly CROSS="✗"
@@ -90,11 +93,11 @@ readonly SPARKLES="✦"
# 📊 Function to print fancy headers
print_header() {
if use_colors; then
if use_colors && [[ "$PORCELAIN_MODE" != "true" ]]; then
echo -e "\n${PURPLE}${BOLD}+==============================================================+${RESET}"
echo -e "${PURPLE}${BOLD}| ${SPARKLES} PLEX MEDIA SERVER ${SPARKLES} |${RESET}"
echo -e "${PURPLE}${BOLD}+==============================================================+${RESET}\n"
else
elif [[ "$PORCELAIN_MODE" != "true" ]]; then
echo ""
echo "+=============================================================="
echo "| ${SPARKLES} PLEX MEDIA SERVER ${SPARKLES} |"
@@ -105,7 +108,9 @@ print_header() {
# 🎉 Function to print completion footer
print_footer() {
if use_colors; then
if [[ "$PORCELAIN_MODE" == "true" ]]; then
return # No footer in porcelain mode
elif use_colors; then
echo -e "\n${DIM}${CYAN}\\--- Operation completed ${SPARKLES} ---/${RESET}\n"
else
echo ""
@@ -120,7 +125,10 @@ print_status() {
local message="$2"
local color="$3"
if use_colors; then
if [[ "$PORCELAIN_MODE" == "true" ]]; then
# Porcelain mode: simple, machine-readable output
echo "${status} ${message}"
elif use_colors; then
echo -e "${color}${BOLD}[${status}]${RESET} ${message}"
else
echo "[${status}] ${message}"
@@ -134,9 +142,9 @@ show_loading() {
local spin='-\|/'
local i=0
# For non-interactive terminals or when called from other scripts,
# For non-interactive terminals, porcelain mode, or when called from other scripts,
# use a simpler approach
if ! use_colors || [[ "${PLEX_SIMPLE_OUTPUT:-}" == "1" ]]; then
if ! use_colors || [[ "$PORCELAIN_MODE" == "true" ]]; then
echo "${message}..."
wait "$pid"
echo "${message}"
@@ -219,6 +227,28 @@ show_detailed_status() {
local service_status
service_status=$(systemctl is-active "$PLEX_SERVICE" 2>/dev/null || echo "inactive")
if [[ "$PORCELAIN_MODE" == "true" ]]; then
# Porcelain mode: simple output
echo "status ${service_status}"
if [[ "$service_status" == "active" ]]; then
local uptime
uptime=$(systemctl show "$PLEX_SERVICE" --property=ActiveEnterTimestamp --value | xargs -I {} date -d {} "+%Y-%m-%d %H:%M:%S" 2>/dev/null || echo "Unknown")
local memory_usage
memory_usage=$(systemctl show "$PLEX_SERVICE" --property=MemoryCurrent --value 2>/dev/null || echo "0")
if [[ "$memory_usage" != "0" ]] && [[ "$memory_usage" =~ ^[0-9]+$ ]]; then
memory_usage="$(( memory_usage / 1024 / 1024 )) MB"
else
memory_usage="Unknown"
fi
echo "started ${uptime}"
echo "memory ${memory_usage}"
echo "service ${PLEX_SERVICE}"
fi
return
fi
# Interactive mode with styled output
if use_colors; then
echo -e "\n${BOLD}${BLUE}+==============================================================+${RESET}"
echo -e "${BOLD}${BLUE}| SERVICE STATUS |${RESET}"
@@ -287,80 +317,202 @@ show_detailed_status() {
;;
esac
# Show recent logs
if use_colors; then
echo -e "\n${DIM}${CYAN}+--- Recent Service Logs (24h) ---+${RESET}"
else
echo ""
echo "+--- Recent Service Logs (24h) ---+"
fi
# Try to get logs with sudo, fall back to user permissions
local logs
if logs=$(sudo journalctl -u "$PLEX_SERVICE" --no-pager -n 5 --since "24 hours ago" --output=short 2>/dev/null); then
if [[ -n "$logs" && "$logs" != "-- No entries --" ]]; then
if use_colors; then
echo -e "${DIM}${logs}${RESET}"
# Show recent logs only in interactive mode
if [[ "$PORCELAIN_MODE" != "true" ]]; then
if use_colors; then
echo -e "\n${DIM}${CYAN}+--- Recent Service Logs (24h) ---+${RESET}"
else
echo ""
echo "+--- Recent Service Logs (24h) ---+"
fi
# Try to get logs with sudo, fall back to user permissions
local logs
if logs=$(sudo journalctl -u "$PLEX_SERVICE" --no-pager -n 5 --since "24 hours ago" --output=short 2>/dev/null); then
if [[ -n "$logs" && "$logs" != "-- No entries --" ]]; then
if use_colors; then
echo -e "${DIM}${logs}${RESET}"
else
echo "${logs}"
fi
else
echo "${logs}"
if use_colors; then
echo -e "${DIM}${YELLOW}No recent log entries found${RESET}"
else
echo "No recent log entries found"
fi
fi
else
if use_colors; then
echo -e "${DIM}${YELLOW}No recent log entries found${RESET}"
# Fallback: try without sudo
logs=$(journalctl -u "$PLEX_SERVICE" --no-pager -n 5 --since "24 hours ago" 2>/dev/null || echo "Unable to access logs")
if [[ "$logs" == "Unable to access logs" || "$logs" == "-- No entries --" ]]; then
if use_colors; then
echo -e "${DIM}${YELLOW}Unable to access recent logs (try: sudo journalctl -u ${PLEX_SERVICE})${RESET}"
else
echo "Unable to access recent logs (try: sudo journalctl -u ${PLEX_SERVICE})"
fi
else
echo "No recent log entries found"
if use_colors; then
echo -e "${DIM}${logs}${RESET}"
else
echo "${logs}"
fi
fi
fi
else
# Fallback: try without sudo
logs=$(journalctl -u "$PLEX_SERVICE" --no-pager -n 5 --since "24 hours ago" 2>/dev/null || echo "Unable to access logs")
if [[ "$logs" == "Unable to access logs" || "$logs" == "-- No entries --" ]]; then
if use_colors; then
echo -e "${DIM}${YELLOW}Unable to access recent logs (try: sudo journalctl -u ${PLEX_SERVICE})${RESET}"
else
echo "Unable to access recent logs (try: sudo journalctl -u ${PLEX_SERVICE})"
fi
if use_colors; then
echo -e "${DIM}${CYAN}+----------------------------------+${RESET}"
else
if use_colors; then
echo -e "${DIM}${logs}${RESET}"
else
echo "${logs}"
fi
echo "+----------------------------------+"
fi
fi
}
# 📋 Enhanced logs function
show_logs() {
local lines=100
local follow=false
if use_colors; then
echo -e "${DIM}${CYAN}+----------------------------------+${RESET}"
# Parse arguments for logs command
while [[ $# -gt 0 ]]; do
case $1 in
-f|--follow)
follow=true
shift
;;
-[0-9]*|[0-9]*)
# Extract number from argument like -50 or 50
lines="${1#-}"
shift
;;
*)
# Assume it's a number of lines
if [[ "$1" =~ ^[0-9]+$ ]]; then
lines="$1"
fi
shift
;;
esac
done
if [[ "$PORCELAIN_MODE" == "true" ]]; then
# Porcelain mode: simple output without decorations
if [[ "$follow" == "true" ]]; then
sudo journalctl -u "$PLEX_SERVICE" --no-pager -f --output=short-iso 2>/dev/null || \
journalctl -u "$PLEX_SERVICE" --no-pager -f --output=short-iso 2>/dev/null || \
echo "Unable to access logs"
else
sudo journalctl -u "$PLEX_SERVICE" --no-pager -n "$lines" --output=short-iso 2>/dev/null || \
journalctl -u "$PLEX_SERVICE" --no-pager -n "$lines" --output=short-iso 2>/dev/null || \
echo "Unable to access logs"
fi
return
fi
# Interactive mode with styled output
if [[ "$follow" == "true" ]]; then
if use_colors; then
echo -e "${BOLD}${CYAN}Following Plex Media Server logs (Ctrl+C to stop)...${RESET}\n"
else
echo "Following Plex Media Server logs (Ctrl+C to stop)..."
echo ""
fi
sudo journalctl -u "$PLEX_SERVICE" --no-pager -f --output=short 2>/dev/null || \
journalctl -u "$PLEX_SERVICE" --no-pager -f --output=short 2>/dev/null || {
if use_colors; then
echo -e "${RED}Unable to access logs. Try: sudo journalctl -u ${PLEX_SERVICE} -f${RESET}"
else
echo "Unable to access logs. Try: sudo journalctl -u ${PLEX_SERVICE} -f"
fi
}
else
echo "+----------------------------------+"
if use_colors; then
echo -e "${BOLD}${CYAN}Recent Plex Media Server logs (last ${lines} lines):${RESET}\n"
else
echo "Recent Plex Media Server logs (last ${lines} lines):"
echo ""
fi
local logs
if logs=$(sudo journalctl -u "$PLEX_SERVICE" --no-pager -n "$lines" --output=short 2>/dev/null); then
if [[ -n "$logs" && "$logs" != "-- No entries --" ]]; then
if use_colors; then
echo -e "${DIM}${logs}${RESET}"
else
echo "${logs}"
fi
else
if use_colors; then
echo -e "${YELLOW}No log entries found${RESET}"
else
echo "No log entries found"
fi
fi
else
# Fallback: try without sudo
logs=$(journalctl -u "$PLEX_SERVICE" --no-pager -n "$lines" --output=short 2>/dev/null || echo "Unable to access logs")
if [[ "$logs" == "Unable to access logs" || "$logs" == "-- No entries --" ]]; then
if use_colors; then
echo -e "${YELLOW}Unable to access logs. Try: ${WHITE}sudo journalctl -u ${PLEX_SERVICE} -n ${lines}${RESET}"
else
echo "Unable to access logs. Try: sudo journalctl -u ${PLEX_SERVICE} -n ${lines}"
fi
else
if use_colors; then
echo -e "${DIM}${logs}${RESET}"
else
echo "${logs}"
fi
fi
fi
fi
}
# 🔧 Show available commands
show_help() {
if use_colors; then
echo -e "${BOLD}${WHITE}Usage:${RESET} ${CYAN}${SCRIPT_NAME}${RESET} ${YELLOW}<command>${RESET}"
echo -e "${BOLD}${WHITE}Usage:${RESET} ${CYAN}${SCRIPT_NAME}${RESET} ${YELLOW}[OPTIONS] <command>${RESET}"
echo ""
echo -e "${BOLD}${WHITE}Available Commands:${RESET}"
echo -e " ${GREEN}${BOLD}start${RESET} ${ROCKET} Start Plex Media Server"
echo -e " ${YELLOW}${BOLD}stop${RESET} ${STOP_SIGN} Stop Plex Media Server"
echo -e " ${BLUE}${BOLD}restart${RESET} ${RECYCLE} Restart Plex Media Server"
echo -e " ${CYAN}${BOLD}status${RESET} ${INFO} Show detailed service status"
echo -e " ${PURPLE}${BOLD}logs${RESET} 📋 Show recent service logs"
echo -e " ${PURPLE}${BOLD}help${RESET} ${SPARKLES} Show this help message"
echo ""
echo -e "${BOLD}${WHITE}Options:${RESET}"
echo -e " ${WHITE}-p, --porcelain${RESET} Simple, machine-readable output"
echo ""
echo -e "${BOLD}${WHITE}Logs Command Usage:${RESET}"
echo -e " ${DIM}${SCRIPT_NAME} logs${RESET} Show last 100 log lines"
echo -e " ${DIM}${SCRIPT_NAME} logs 50${RESET} Show last 50 log lines"
echo -e " ${DIM}${SCRIPT_NAME} logs -f${RESET} Follow logs in real-time"
echo ""
echo -e "${DIM}${WHITE}Examples:${RESET}"
echo -e " ${DIM}${SCRIPT_NAME} start # Start the Plex service${RESET}"
echo -e " ${DIM}${SCRIPT_NAME} status # Show current status${RESET}"
echo -e " ${DIM}${SCRIPT_NAME} start # Start the Plex service${RESET}"
echo -e " ${DIM}${SCRIPT_NAME} status --porcelain # Machine-readable status${RESET}"
echo -e " ${DIM}${SCRIPT_NAME} logs -f # Follow logs in real-time${RESET}"
else
echo "Usage: ${SCRIPT_NAME} <command>"
echo "Usage: ${SCRIPT_NAME} [OPTIONS] <command>"
echo ""
echo "Available Commands:"
echo " start ${ROCKET} Start Plex Media Server"
echo " stop ${STOP_SIGN} Stop Plex Media Server"
echo " restart ${RECYCLE} Restart Plex Media Server"
echo " status ${INFO} Show detailed service status"
echo " logs 📋 Show recent service logs"
echo " help ${SPARKLES} Show this help message"
echo ""
echo "Options:"
echo " -p, --porcelain Simple, machine-readable output"
echo ""
echo "Logs Command Usage:"
echo " ${SCRIPT_NAME} logs Show last 100 log lines"
echo " ${SCRIPT_NAME} logs 50 Show last 50 log lines"
echo " ${SCRIPT_NAME} logs -f Follow logs in real-time"
echo ""
echo "Examples:"
echo " ${SCRIPT_NAME} start # Start the Plex service"
echo " ${SCRIPT_NAME} status # Show current status"
@@ -377,19 +529,47 @@ main() {
exit 1
fi
# Check if no arguments provided
if [[ $# -eq 0 ]]; then
# Parse command line arguments
local command=""
local args=()
while [[ $# -gt 0 ]]; do
case $1 in
-p|--porcelain)
PORCELAIN_MODE=true
shift
;;
-h|--help|help)
command="help"
shift
;;
start|stop|restart|reload|status|info|logs)
command="${1,,}" # Convert to lowercase
shift
# Collect remaining arguments for the command (especially for logs)
args=("$@")
break
;;
*)
echo "Unknown option or command: $1" >&2
exit 3
;;
esac
done
# Check if no command provided
if [[ -z "$command" ]]; then
print_header
show_help
exit 1
fi
# Show header for all operations except help
if [[ "${1,,}" != "help" ]] && [[ "${1,,}" != "--help" ]] && [[ "${1,,}" != "-h" ]]; then
if [[ "$command" != "help" ]]; then
print_header
fi
case "${1,,}" in # Convert to lowercase
case "$command" in
"start")
start_plex
;;
@@ -402,12 +582,15 @@ main() {
"status"|"info")
show_detailed_status
;;
"help"|"--help"|"-h")
"logs")
show_logs "${args[@]}"
;;
"help")
print_header
show_help
;;
*)
print_status "${CROSS}" "Unknown command: ${RED}${BOLD}$1${RESET}" "${RED}"
print_status "${CROSS}" "Unknown command: ${RED}${BOLD}$command${RESET}" "${RED}"
echo ""
show_help
exit 1